Mule : Spring Events Example
This page last changed on Aug 26, 2005 by rossmason.
The folloing example configures a OrderManager bean that recieves Orders via different endpoints, processes them, then sends the result to a processed.queue. The example assumes you have read the Mule Events in Spring introduction. We want our OrderManager to receive orders over -
To enable a spring bean to receive Mule Events it needs to implement a MuleEventListener or MuleSubscriptionEventListener. Lets start with the code for our OrderManager and then describe what its doing. The OrderManager service exposes a single method for processing orders. public interface OrderManager { public String processOrder(Order order); } Our OrderManagerBean implements both OrderManager and MuleSubscriptionEventListener. public class OrderManagerBean implements MuleSubscriptionEventListener, OrderManager { private String[] subscriptions; public void onApplicationEvent(ApplicationEvent orderEvent) { //Get the order Order order = (Order) orderEvent.getSource(); String result = processOrder(order); //Cast the event to a Mule event, we'll use this to get the AppContext MuleApplicationEvent muleEvent = (MuleApplicationEvent) orderEvent; //Create a new MuleEvent. This will be sent to the replyTo //address MuleApplicationEvent returnEvent = new MuleApplicationEvent( result, "jms://processed.queue"); //Call publish on the application context, Mule will do the rest muleEvent.getApplicationContext().publishEvent(returnEvent); } public String processOrder(Order order) { //Do some processing... return "Order '" + order.getOrder() + "' Processed"; } public String[] getSubscriptions() { return subscriptions; } public void setSubscriptions(String[] subscriptions) { this.subscriptions = subscriptions; } } Notice the get and set scubscription methods, these are part of the MuleSubscriptionEventListener interface and they determine what events the OrderManager will receive. <bean id="orderManager" class="org.mule.extras.spring.events.OrderManagerBean"> <property name="subscriptions"> <list> <value> jms://orders.queue </value> <value> pop3://orders:secret@mail.myrestaurant.com?transformers=EmailMessageToString,StringToOrder </value> <value> glue:http://localhost:44444/mule/OrderManager </value> </list> </property> </bean> This bean will now receive email messages sent to orders@myrestaurant.com and jms messages sent to order.queue. The third soap subscription may not be so obvious. When used as a subscription the mule soap provider will automatically expose the OrderManager as a web service! Soap clients can invoke the OrderManagerBean as a web service over http.
Configuring Mule and SpringTo hook Mule into the Spring event mechanism you need to declare the MuleEventMulticaster bean in the application Context. This will now be used to send and receive events in Spring. <bean id="applicationEventMulticaster" class="org.mule.extras.spring.events.MuleEventMulticaster"/> <property name="asynchronous"><value>false</value></property> </bean>
See the Xml configuration for the Spring events example test case for an example of how to configure your Mule instance. Handling TransformationsSo far we have configured the OrderManagerBean to receive events over jms, pop3 and soap, but we haven't determined how we get an Order object from each of these transports; this is done with Mule transformers. Lets look at each transport individlually. JmsBy default, Mule will handle transformations to and from Jms Message automatically using standard Jms transformers, these can be overridden, but for many cases the defaults are fine. Pop3There has to be some custom transformation to turn the message body into an Order object. You can see on the pop3 endpoint that there is a transfromers parameter. This references a comma-separated list of transformers to apply to the email message when it is received. The transformer names refer to the name of transformer beans in the application context. So we need to add two new beans, one of reach transfromer - <bean id="EmailMessageToString" class="org.mule.providers.email.transformers.EmailMessageToString"/> <bean id="StringToOrder" class="org.mule.extras.spring.events.StringToOrder"> <property name="returnClass"><value>org.mule.extras.spring.events.Order</value></property> </bean> SoapAll type conversion is handled by the underlying soap server. Mule configures the servers to use automatic type mapping where possible. For more information see the Mule Soap Provider. Receiving EventsWhen the OrderManagerbean receives events over jms or pop3 the onApplicationEvent will get invoked with an Order source object; Mule transforms the events before your beans receive them. If the the OrderManagerBean is invoked as a web service the processOrder method will get called directly as this is the method exposed by the OrderManager interface. Publishing EventsYou can see from the code for the OrderManagerBean that MuleApplicationEvents are published via the application context. This means that at any point you beans can publish Mule events over any Mule transport using a Mule Endpoint. This provides a very convenient way for your objects to tal over http, tcp, jms, soap, etc. In the example we get the Spring application context from the event, while this is convenient, it is not mandatory. Your bean could implement ApplicationContextAware and send mule events that way without having to receive an event first. |
![]() |
Document generated by Confluence on Oct 03, 2006 09:23 |