Java Messaging Service- Publisher/Producer using Active MQ

Active MQ is a message broker which implements the JMS (Java Messaging Service) API. It enables a developer to implement queues or topics with publisher/producer who sends a message on a particular queue/ topic whilst subscriber who consumes the message by subscribing to a particular queue/ topic.

Difference between topic and queue?

When a message is published to a TOPIC, this message goes to all the active consumers subscribed to that topic at that point of time.

However, a single message sent to queue will be received by exactly a single consumer. If there are is no consumer available at that time subscribed to the queue, the message will be retained in the queue until processed by the consumer.

Step 1: A very basic example of Producer Class using Spring:


public class Publisher {

// JmsTemplate class is used for message production and synchronous message reception.


JmsTemplate jmsTemplate

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


String destinationQueue;

// in order to set the queue name from the code without using URI

public void setDestinationQueue (String destinationQueue) {

this.destinationQueue = destinationQueue;


// sending the message to the queue

public void send (String msg) {

jmsTemplate.convertAndSend(destinationQueue, msg);


} // end of Producer Class

Step 2: Spring Producer Controller Class 


public class ActiveMqProducerController {


JmsProducerClient jmsProducerClient;


public String producer(@RequestParam(“msg”) String msg) {


return “sent”;



Step 3: Spring class




public class JmsProducerApplication extends SpringBootServletInitializer {

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



Step 5: file


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



#queue name where the message will be sent by producer.


Step 6: Producer Test Class 


@SpringBootTest(classes = JmsProducerApplication.class)


public class ProducerTest {


Publisher publisher;


public void testSendBinaryInformation() {

String message = “010111001100”;





  1. ActiveMQ is enabled by executing the following “./activemq console” at the bin location of the ActiveMQ installed package.
  2. Check if the queue is up and running by hitting the following URL: http://localhost:8161/admin/queues.jsp. It might ask you for user name and password, enter the corresponding values that you mentioned. Also note 8161 is the default port configured for active mq in jetty.xml configuration of activemq.
  3. Messages can be sent to the queue either by calling the send() method as shown in test class or by hitting the URL http://localhost:9000/producer?msg=0101010101. Do make sure the spring application is run before sending data through this URL.
  4. The “producer” in URL is mapped to the controller code in ActiveMqProducerController class.
  5. Message status can be viewed in the activemq console and it would be shown corresponding to the queue name whether the messages are in enqueued, pending etc.

One thought on “Java Messaging Service- Publisher/Producer using Active MQ

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