Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Event-driven services in SOA

Design an event-driven and service-oriented platform with Mule

  • Print
  • Feedback

Page 6 of 7

  1. Asynchronously:A given component can simultaneously process multiple events sent and received by different threads.
  2. Synchronously:A single event must complete processing before a component can resume execution. In other words, a component that produces an event sends the event and then blocks until the call returns, thereby allowing only one event at a time to be processed.
  3. Request-response:A component specifically requests an event and waits for a specified time to receive a response.


The org.mule.impl.MuleComponent implementation class provides a concrete component class that includes all the functionality needed to send and receive data and create events.

Objects that execute synchronously are encouraged to implement the org.mule.umo.lifecycle.Callable interface, which defines a single method, Object onCall(UMOEventContext eventContext). The Callable interface provides UMOs with an interface that supports event calls. Although not mandatory, the interface provides a lifecycle method that executes when the implementing component receives an event. The following illustrates a simple implementation of this interface:

                    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(); } }


The object returned from the onCall() method can be anything. When the UMOLifecycleAdapter for the component receives this object, it will first see if the object is a UMOMessage; if the object is neither a UMOMessagenor null, a new message will be created using the returned object as the payload. This new event will then publish via the configured outbound router, if one has been configured for the UMO and the setStopFurtherProcessing(true)method wasn't called on the UMOEventContext instance.

A simple event framework using Mule

Let's put the pieces of Mule together to construct a simple event framework. The framework consists of an event manager responsible for registering and deregistering services that can receive events, and for synchronously and asynchronously routing messages to these services.

The Mule "vm" protocol requires that a configuration file be located at a directory named META-INF/services/org/mule/providers/vm, relative to the event manager's working directory. This file defines numerous components for the protocol, such as the connector and dispatcher factory. The file's contents are as follows:

                    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
               


A simple interface defines the event manager's public view:

                    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; }


The event-manager implementation class is encapsulated within a factory class, thereby allowing the implementation to change as needed without affecting the event manager's clients. The event-manager implementation is shown below:

  • Print
  • Feedback

Resources