public void onEvent(ApplicationEvent event);
Sending and Receiving Mule Events in Spring
You can configure Spring beans to publish events to Mule and configure Spring event listeners to receive Mule events. This page describes how to set up the configuration.
Spring Events Overview
Spring provides a simple mechanism for sending and receiving events between beans. To receive an event, a bean implements ApplicationListener, which has a single method:
ApplicationListener.java
To publish events to listeners, you call the publishEvent() method on the ApplicationContext. This will publish the same event to every listener in the context. You can also plug in custom event handlers to the application context.
Mule Events in Spring
To start receiving Mule events, you create a bean based on MuleEventMulticaster in your Mule configuration file. This class is an Application Event Multicaster that enables Spring beans to send and receive Mule events. You also add one or more endpoints on which to receive events:
xmlns:spring="http://www.springframework.org/schema/beans"
...
<spring:beans>
<spring:bean id="applicationEventMulticaster "class="org.mule.module.spring.events.MuleEventMulticaster">
<spring:property name="subscriptions">
<spring:list>
<spring:value>jms://my.queue</value>
<spring:value>pop3://ross:secret@mail.muleumo.org</value>
</spring:list>
</spring:property>
</spring:bean>
</spring:beans>
With this configuration, any emails received for ross@muleumo.org
or any JMS messages sent to my.queue
will be received by all Spring event listeners. Note that the MuleEventMulticaster does not interfere with normal Spring event behavior. If a non-Mule applicationEvent is sent via the ApplicationContext, all beans registered to receive events will still get the event.
The inbound endpoints can be any valid Mule Endpoint, so you can receive JMS messages, SOAP requests, files, HTTP and servlet requests, TCP, multicast, and more.
Adding Bean Subscriptions
You can have beans subscribe only to relevant events. The MuleSubscriptionEventListener includes two methods for getting and setting an array of endpoints on which the bean will receive events.
TestSubscriptionBean.java
package org.mule.module.spring.events;
public class TestSubscriptionEventBean extends TestMuleEventBean implements MuleSubscriptionEventListener
{
private String[] subscriptions;
public void setSubscriptions(String[] subscriptions)
{
this.subscriptions = subscriptions;
}
public String[] getSubscriptions()
{
return subscriptions;
}
}
You configure this bean like any other bean:
xmlns:spring="http://www.springframework.org/schema/beans"
...
<spring:beans>
<spring:bean id="subscriptionBean" class="org.mule.module.spring.events.TestSubscriptionEventBean">
<spring:property name="subscriptions">
<spring:list>
<spring:value>vm://event.*</value>
</spring:list>
</spring:property>
</spring:bean>
</spring:beans>
Publishing Events to Mule
Publishing events is just as easy as receiving them. You use the standard publishEvent() method on the application context. Your bean can get a reference to the application context by implementing ApplicationContextAwareor by querying MuleApplicationEvent.
//Create a new MuleEvent.
Object message = new String("This is a test message");
MuleApplicationEvent muleEvent = new MuleApplicationEvent(
message, "jms://processed.queue");
//Call publishEvent on the application context, and Mule does the rest
applicationContext.publishEvent(muleEvent);