In continuation with the previous post on Producer of Active MQ:

This post will detail about how the consumer could connect to the queue on which the producer has sent the messages and retrieve them.

Step1: A very basic example of Consumer class using Spring.



public class Subscriber {


JmsTemplate jmsTemplate;

// in order to read the queue name from the URI


String destinationQueue;

private Subscriber() {}

public static Subscriber newInstance() {

return new Subscriber();


// in order to read the queue name independent of the URI

public void setReceivingQueue(String destinationQueue) {

this.destinationQueue = destinationQueue;


// receiving the message from the mentioned queue

public String receiveString() {

String dataReceived = (String) jmsTemplate.receiveAndConvert(destinationQueue);

return dataReceived;


public Object receiveObject() {

Object dataReceived = jmsTemplate.receiveAndConvert(destinationQueue);

return dataReceived;



Step2: Spring Receiver Controller Class


public class ActiveMqReceiverController {


Subscriber subscriber;


public Object receive() {

return subscriber.receiveObject();



Step3: Spring Boot Initializer




public class JmsReceiverApplication extends SpringBootServletInitializer {

public static void main(String[] args) {, args);



Step4: file


#user name & password in order to connect to the activemq on the port specified



#queue name where the message will be consumed by consumer.


Step5: Consumer Test class


@SpringBootTest(classes = JmsReceiverApplication.class)


public class ConsumerTest {

private static final Logger LOGGER = Logger.getLogger(ConsumerTest.class);


Subscriber subscriber;


public void testReadBinaryDataFromQueue() {

String messageReceived = subscriber.receiveString();“Message Received is” + messageReceived)




  1. Messages can be received from the queue by directly using receiveAndConvert() method of JmsTemplate class as shown in Test class above or by hitting the URL: http://localhost:9000/receive. Do ensure that Spring application is run before hitting this URL.
  2. The “receive” in the URL is mapped to the controller code in ActiveMqReceiverController class.
  3. Messages once consumed will appear in the Active MQ console as Dequeued and the remaining would appear as Pending and in Enqueued status.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s