Sunday, October 5, 2014

Embedding ActiveMQ in Spring Web Apps

Notes: I won't recommend this approach. It is for prove of concept. Run stand alone instance of your ActiveMQ. May the force be with you.

Problem:


I have a process that need to do calculation on an array of data. The thing is it takes very very very long to complete the calculation. As the result, the user click on something, then they need to wait for the browser to load and think very hard before it show something back to the use, whereas the client doesn't really cares about the outcome of the calculation.

Design:

I initially try make an asynchronous function call but the system requires the data been processed in sequence, one after one on an order. So I went explore if Messaging can help solve my problem.

Environment:

activemq-broker-5.10.0.jar
activemq-client-5.10.0.jar
activemq-spring-5.10.0.jar
javax.jms.jar
javax.management.j2ee.jar
spring-core-4.1.0.RELEASE.jar
spring-jms-4.1.0.RELEASE.jar
spring-messaging-4.1.0.RELEASE.jar
(and others... sorry, I can't list them all)

Implementation:

In applicationContext.xml, add namespace for amq and jms.



Found this somewhere online, create a embedded dummy broker with spring.
Define a queue.

Create a spring connectionFactory to be used later.

JMS Template - for sending message to the broker.

As in the comment, settings for annotated JmsMessageListener in Spring 4.1 (again, must be 4.1)

And always remember to turn it on.

And here is the java code.

After everything works, I expand the settings to include more queues and also set up a proper ActiveMQ broker running by itself (removing the tag and have pointing to my ActiveMQ url). 

No comments: