string componentname = "myreceiver"; // the name of the receiving component.
string transformers = null; // a comma-separated list of transformers
// to apply to the result message.
string payload = "a test event"; // the payload of the event.
java.util.map messageproperties = null; // any properties to be associated
// with the payload.
muleclient client = new muleclient();
umomessage message = client.senddirect(componentname,
transformers,
payload,
messageproperties);
system.out.println("event result: " + message.getpayloadasstring());
import org.mule.umo.lifecycle.callable;
public class echocomponent
implements callable
{
public object oncall(umoeventcontext context) throws exception
{
string msg = context.getmessageasstring();
// print message to system.out
system.out.println("received synchronous message: " + msg);
// echo transformed message back to sender
return context.gettransformedmessage();
}
}
connector=org.mule.providers.vm.vmconnector
dispatcher.factory=org.mule.providers.vm.vmmessagedispatcherfactory
message.receiver=org.mule.providers.vm.vmmessagereceiver
message.adapter=org.mule.providers.vm.vmmessageadapter
endpoint.builder=org.mule.impl.endpoint.resourcenameendpointbuilder
package com.jeffhanson.mule;
import org.mule.umo.futuremessageresult;
public interface eventmanager
{
/**
* sends an event message synchronously to a given service.
*
* @param servicename the name of the service to which the event
* message is to be sent.
* @param payload the content of the event message.
* @return object the result, if any.
* @throws eventexception on error
*/
public object sendsynchronousevent(string servicename,
object payload)
throws eventexception;
/**
* sends an event message asynchronously to a given service.
*
* @param servicename the name of the service to which the event
* message is to be sent.
* @param payload the content of the event message.
* @return futuremessageresult the result, if any.
* @throws eventexception on error
*/
public futuremessageresult sendasynchronousevent(string servicename,
object payload)
throws eventexception;
/**
* starts this event manager.
*/
public void start();
/**
* stops this event manager.
*/
public void stop();
/**
* retrieves the protocol this event manager uses.
* @return
*/
public string getprotocol();
/**
* registers a service to receive event messages.
*
* @param servicename the name to associate with the service.
* @param implementation either a container reference to the service
* or a fully-qualified class name.
*/
public void registerservice(string servicename,
string implementation)
throws eventexception;
/**
* unregisters a service from receiving event messages.
*
* @param servicename the name associated with the service to unregister.
*/
public void unregisterservice(string servicename)
throws eventexception;
}
package com.jeffhanson.mule;
import org.mule.umo.*;
import org.mule.extras.client.muleclient;
import org.mule.impl.endpoint.muleendpoint;
import org.mule.config.quickconfigurationbuilder;
import java.util.hashmap;
import java.util.map;
public class eventmanagerfactory
{
private static hashmap instances = new hashmap();
/**
* retrieves the event manager instance for a given protocol.
*
* @param protocol the protocol to use.
* @return eventmanager the event manager instance.
*/
public static eventmanager getinstance(string protocol)
{
eventmanager instance = (eventmanager)instances.get(protocol);
if (instance == null)
{
instance = new eventmanagerimpl(protocol);
instances.put(protocol, instance);
}
return instance;
}
/**
* a concrete implementation for a simple event manager.
*/
private static class eventmanagerimpl
implements eventmanager
{
private umomanager manager = null;
private quickconfigurationbuilder builder = null;
private muleclient eventclient = null;
private string protocol = null;
private muleendpoint receiveendpoint = null;
private muleendpoint sendendpoint = null;
private eventmanagerimpl(string protocol)
{
this.protocol = protocol;
}
/**
* starts this event manager.
*/
public void start()
{
try
{
builder = new quickconfigurationbuilder();
manager = builder.createstartedmanager(true,
protocol + "tmp/events");
eventclient = new muleclient();
receiveendpoint = new muleendpoint(protocol
+ "tmp/events/receive");
sendendpoint = new muleendpoint(protocol + "tmp/events/send");
}
catch (umoexception e)
{
system.err.println(e);
}
}
/**
* stops this event manager.
*/
public void stop()
{
try
{
manager.stop();
}
catch (umoexception e)
{
system.err.println(e);
}
}
/**
* retrieves the protocol this event manager uses.
* @return
*/
public string getprotocol()
{
return protocol;
}
/**
* registers a service to receive event messages.
*
* @param servicename the name to associate with the service.
* @param implementation either a container reference to the service
* or a fully-qualified class name
* to use as the component implementation.
*/
public void registerservice(string servicename,
string implementation)
throws eventexception
{
if (!manager.getmodel().iscomponentregistered(servicename))
{
try
{
builder.registercomponent(implementation,
servicename,
receiveendpoint,
sendendpoint);
}
catch (umoexception e)
{
throw new eventexception(e.tostring());
}
}
}
/**
* unregisters a service from receiving event messages.
*
* @param servicename the name associated with the service to unregister.
*/
public void unregisterservice(string servicename)
throws eventexception
{
try
{
builder.unregistercomponent(servicename);
}
catch (umoexception e)
{
throw new eventexception(e.tostring());
}
}
/**
* sends an event message synchronously to a given service.
*
* @param servicename the name of the service to which the event
* message is to be sent.
* @param payload the content of the event message
* @return object the result, if any.
* @throws eventexception on error
*/
public object sendsynchronousevent(string servicename,
object payload)
throws eventexception
{
try
{
if (!manager.getmodel().iscomponentregistered(servicename))
{
throw new eventexception("service: " + servicename
+ " is not registered.");
}
string transformers = null;
map messageproperties = null;
umomessage result = eventclient.senddirect(servicename,
transformers,
payload,
messageproperties);
if (result == null)
{
return null;
}
return result.getpayload();
}
catch (umoexception e)
{
throw new eventexception(e.tostring());
}
catch (exception e)
{
throw new eventexception(e.tostring());
}
}
/**
* sends an event message asynchronously.
*
* @param servicename the name of the service to which the event
* message is to be sent.
* @param payload the content of the event message.
* @return futuremessageresult the result, if any
* @throws eventexception on error
*/
public futuremessageresult sendasynchronousevent(string servicename,
object payload)
throws eventexception
{
futuremessageresult result = null;
try
{
if (!manager.getmodel().iscomponentregistered(servicename))
{
throw new eventexception("service: " + servicename
+ " is not registered.");
}
string transformers = null;
map messageproperties = null;
result = eventclient.senddirectasync(servicename,
transformers,
payload,
messageproperties);
}
catch (umoexception e)
{
throw new eventexception(e.tostring());
}
return result;
}
}
}
package com.jeffhanson.mule;
import java.util.date;
public class testservice
{
public void receiveevent(string eventmessage)
{
system.out.println("\n\ntestservice.receiveevent(string) received "
+ "event message: " + eventmessage + "\n\n");
}
public void receiveevent(integer eventmessage)
{
system.out.println("\n\ntestservice.receiveevent(integer) received "
+"event message: " + eventmessage + "\n\n");
}
public void receiveevent(date eventmessage)
{
system.out.println("\n\ntestservice.receiveevent(date) received "
+ "event message: " + eventmessage + "\n\n");
}
}
package com.jeffhanson.mule;
import org.apache.log4j.logger;
import org.apache.log4j.level;
import org.apache.log4j.basicconfigurator;
import java.util.date;
public class eventclient
{
static logger logger = logger.getlogger(eventclient.class);
public static void main(string[] args)
{
// set up a simple configuration that logs on the console.
basicconfigurator.configure();
logger.setlevel(level.all);
try
{
eventmanager eventmanager =
eventmanagerfactory.getinstance("vm://");
eventmanager.start();
string servicename = testservice.class.getname();
string implementation = servicename;
eventmanager.registerservice(servicename, implementation);
object result =
eventmanager.sendsynchronousevent(servicename, "a test message");
if (result != null)
{
system.out.println("event result: " + result.tostring());
}
result =
eventmanager.sendsynchronousevent(servicename, new integer(23456));
if (result != null)
{
system.out.println("event result: " + result.tostring());
}
result =
eventmanager.sendsynchronousevent(servicename, new date());
if (result != null)
{
system.out.println("event result: " + result.tostring());
}
eventmanager.stop();
}
catch (eventexception e)
{
system.err.println(e.tostring());
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器