32. Spring ApplicationEvent Support

Spring Integration provides support for inbound and outbound ApplicationEvents as defined by the underlying Spring Framework. For more information about the events and listeners, refer to the Spring Reference Manual.

32.1 Receiving Spring ApplicationEvents

To receive events and send them to a channel, simply define an instance of Spring Integration's ApplicationEventListeningChannelAdapter. This class is an implementation of Spring's ApplicationListener interface. By default it will pass all received events as Spring Integration Messages. To limit based on the type of event, configure the list of event types that you want to receive with the 'eventTypes' property.

For convenience namespace support was provided to configure ApplicationEventListeningChannelAdapter via inbound-channel-adapter

<int-event:inbound-channel-adapter channel="input" event-types="foo.bar.FooApplicationEvent, foo.bar.BarApplicationEvent"/>

<int:publish-subscribe-channel id="sampleEventChannel"/>

In the above sample, all Application Context events that are of type specified by the 'event-types' (optional) attribute will be delivered as Spring Integration Messages to 'sampleEventChannel'.

32.2 Sending Spring ApplicationEvents

To send Spring ApplicationEvents, create an instance of the ApplicationEventPublishingMessageHandler and register it within an endpoint. This implementation of the MessageHandler interface also implements Spring's ApplicationEventPublisherAware interface and thus acts as a bridge between Spring Integration Messages and ApplicationEvents.

For convenience namespace support was provided to configure ApplicationEventPublishingMessageHandler via outbound-channel-adapter element

<int:channel id="input"/>

<int-event:outbound-channel-adapter channel="input"/>

If you are using PollableChannel (e.g., Queue), you can also provide poller as sub-element of outbound-channel-adapter, optionally providing task-executor

<int:channel id="input">

<int-event:outbound-channel-adapter channel="input">
  <int:poller max-messages-per-poll="1" task-executor="executor" fixed-rate="100"/>

<task:executor id="executor" pool-size="5"/>

In the above sample, all messages sent to an 'input' channel will be published as ApplicationEvents to Spring Application sContext