This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Spring ApplicationEvent over AMQP

However, I am looking for an example of passing application events from one application instance through a rabbit queue and on the end a consumer reading them from the queue and firing those events in the other application instance.

Or

Can anyone suggest a better way of solving the sharing of application events ?

Comment

Thank you Mark for the suggestion,
I created two applications
1) Producer
- An event is fired,
- The event is picked up the event inbound channel adapter
- The event is passed to a events Queue (rabbit queue)

2) Consumer
- Reads the event from the queue
- Passes it to the event outbound channel adapter

However, the event is not fired at the consumer end even though I can print the contents of the event. Upon debugging, I realized that the source of the event on the consumer side is null (as its a transient field). Can that be the cause ?

Where the rootObject of evaluationContext is entire ApplicationEvent.
So, to the Rabbit queue will be send just the source of your event.

On the other side in the Consumer <int-event:outbound-channel-adapter/> picks up the message from Rabbit queue and publish into ApplicationContext the instance of org.springframework.integration.event.core.Messagi ngEvent with entire Message as event source.

Comment

But don't send to the external system any Java object. Where is a guaranty, that other side is Java too and the more it has in its CLASSPATH the same class for deserialization?
Messaging is just middleware. It doesn't have any ability to work with buisiness objects. It just allows you to transport data between different systems via many existing protocols. To connect these protocols with messaging system there are some specific components - channel adapters.
One of them is <int-event:inbound-channel-adapter/> which allows to listen an ApplicationEvent from the context and sends a Message into Spring Integration Bus. Inside this Bus Messages can contain in their payload any Object including your business object even without the implements Serializable. Because your classes and Spring Integration components are inside the same CLASSPATH of your application. But sending Message to the external system is a bit different and more complex. Here we should think about protocol, framework abilities and the more about data. The last one isn't important for messaging system, but the data has to be understandable for system, which will read this data from messaging system on the other side. So, here we should use some common types of data presentation: XML, JSON or simple String, it can be byte[] too. They can be transported on wires to
messaging system (AMQP) and can be stored until consumer reads it from messaging system.
Spring Integration architecture & design allow easy build the messaging application which can connect to the external systems by the same way as internally between business services. It sometimes confuses developers. Please, take a look into my presentation on the slide 11: http://www.slideshare.net/artembilan...n-but-not-only to figure out how it works inside the Spring Integration application.

2.

how does connecting the
Channel Adapters for both ApplicationEvents and RabbitMQ
manage that

Manage what? It doesn't matter: <int-event:inbound-channel-adapter/> receives event and send a Message into a <channel>. This chennel has subscriber in the face of <int-amqp:outbound-channel-adapter/>. The last one connects to the AMQP-Broker and sends AMQP-Message to the external queue. And that's all: everithing works at the same thread.
On the Consumer application, via your use-case, components are working as unmirrored.

For a better understanding of how it works, please, invoke send() & receive() methods independently: call send() and take a look how it is on Rabbit via his console. Then call receive() and you'll see that ApplicationEvent is received and message disappeared from queue on Broker.