AMQP Backed Spring Integration using vFabric RabbitMQ
Join the DZone community and get the full member experience.
Join For Free
introduction
combining vfabric rabbitmq and spring integration we can create loosely coupled enterprise class message-based workflows using spring amqp . refer to the spring integration, amqp backed message channels documentation.
in this demo, we will design a spring integration workflow, wherein a message is published on to a p2p-pollable-channel and a publish-subscribe-channel is listening to it, it will pickup that message and pass it to 2 different service activators to further process it. i will also demonstrate that, both these message channels are on their own, listening to an external applications and when an external application publishes a message to these queues they will process from there on in the workflow. a typical use case of this is in an enterprise it environment where we can create robust workflows, wherein if a message is not processed in a particular step, we can message the data and process it from there on. since spring amqp is a wire protocol the external application can be used by any network protocol to publish the message to the vfabric rabbitmq queue and the spring integration flow is triggered.
amqp backed message channel in spring integration flow
as always in my blog, as per
tdd
, i will be writing the test first as seen below,
public class publishersubscribertest { @test public void testintegration() { try { string request = streamtostring(getclass().getresourceasstream( "/data/payload.xml")); message<string> message = messagebuilder.withpayload(request) .build(); channel.send(message); //assert various messages } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
the spring integration configuration flow:
<int:bridge input-channel="p2p-pollable-channel" output-channel="pub-sub-channel" /> <int:service-activator input-channel="pub-sub-channel" id="serviceactivator1" ref="serviceactivator1bean" method="logxml" /> <int:service-activator input-channel="pub-sub-channel" id="serviceactivator2" ref="serviceactivator2bean" method="logxml" /> <bean id="serviceactivator1bean" class="com.gosmarter.amqp.service.subscriber1serviceactivator"> </bean> <bean id="serviceactivator2bean" class="com.gosmarter.amqp.service.subscriber2serviceactivator"> </bean>
test channel definition:
<int:poller default="true" fixed-rate="1000" /> <int:channel id="p2p-pollable-channel" /> <int:publish-subscribe-channel id="pub-sub-channel" />
actual channel definition used as a part of web.xml is seen here
<!-- a reference to the org.springframework.amqp.rabbit.connection.connectionfactory --> <rabbit:connection-factory id="connectionfactory" /> <!-- creates a org.springframework.amqp.rabbit.core.rabbitadmin to manage exchanges, queues and bindings --> <rabbit:admin connection-factory="connectionfactory" /> <int-amqp:channel id="p2p-pollable-channel" connection-factory="connectionfactory" /> <int-amqp:publish-subscribe-channel id="pub-sub-channel" connection-factory="connectionfactory" />
when you run this application in sts as “run on server” for the 1st time, you will notice in the vfabric rabbitmq admin console will have one queue, p2p-pollable-channel and exchange called si.fanout.pub-sub-channel. you'll notice the spring integration framework appended “si.fanout” in front of pub-sub-channel. if you want to test if the end to end is working, you can post a message on either the queue or exchange , it will continue the workflow from that point onwards.
if you are using amqp backed channels, you can reply a payload from any channels to continue the workflow, this will be useful in error handling and payload retry.
conclusion
in this sample, we have demonstrated that with a single component backed by amqp, we can build a loosely coupled workflow. based on the context, there can be 2 different ways of building publish subscriber workflow, one is as described above and another one is one publisher component will have several subscriber components.
i hope this blog helped you.
Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Integrate Microsoft Team With Cypress Cloud
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
Creating Scalable OpenAI GPT Applications in Java
-
Develop Hands-Free Weather Alerts To Ensure Safe Backpacking
Comments