Control Bus Pattern with Spring Integration and JMS
Join the DZone community and get the full member experience.
Join For Freefor people in hurry, refer the steps and the demo .
introduction
control bus pattern is a enterprise integration pattern is used to control distributed systems in spring integration . in this blog, i will show you how a control bus can control your application or a component to start or stop listening to jms message . in this example, we are using jms queue to start and stop the jms inbound-channel-adapter , we can also do this with jdbc inbound-channel-adapter and control this thru an external application. the other way to do the same is by using mbean as in this example .
in this use case, there is a spring integration flow. this spring integration flow can be controlled by sending start / stop message to inbound-channel-adapter from a activemq jms queue.
details control bus with spring integration
to start implementing this use case, we write the junit test 1st. if you notice once the inboundadapter is started the message is received from the adapteroutchannel. once the inboundadapter is stopped no message is received. this is demonstrated as below,
@test public void democontrolbus() { assertnull(adapteroutputchanel.receive(1000)); controlchannel.send(new genericmessage<string>("@inboundadapter.start()")); assertnotnull(adapteroutputchanel.receive(1000)); controlchannel.send(new genericmessage<string>("@inboundadapter.stop()")); assertnull(adapteroutputchanel.receive(1000)); }
the test configuration looks as below,
<int:inbound-channel-adapter id="inboundadapter" channel="controlbus-managed-p2p-pollable-channel" expression="'hello'" auto-startup="false"> <int:poller fixed-rate="6000" /> </int:inbound-channel-adapter>
if you run the “mvn test” the tests work. in the main configuration, we will be configuring actual queues and jms inbound-channel-adapter as below,
<int-jms:inbound-channel-adapter id="inboundadapter" channel="controlbus-managed-p2p-pollable-channel" jms-template="jmstemplate"> <int:poller fixed-rate="6000" /> </int-jms:inbound-channel-adapter> <int-jms:inbound-channel-adapter id="controlbusadapter" channel="control-channel" jms-template="controlbusjmstemplate"> <int:poller fixed-rate="6000" /> </int-jms:inbound-channel-adapter>
now when you start the component as “run on server” in sts ide and post a message on
myqueue, you can see the subscribers received the messages on the
console. you can issue “@inboundadapter.stop()” on the controlbusqueue,
it will stop the inbound-channel-adapter, it will also throw
java.lang.interruptedexception, it looks like a false alarm. to test if
the inbound-channel-adapter is stopped, post a message on to myqueue,
the component will not process the message. now issue
“@inboundadapter.start()” on the controlbusqueue, it will process the
earlier message and start listening for new messages.
conclusion
if you notice in this blog, we can control the component to listen to message using control bus. the other way to do the same is by using mbean as in this example .
Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments