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:

@Component

public class Publisher {

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

@Autowired

JmsTemplate jmsTemplate

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

@Value(“${jms.queue.destination}”)

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 

@RestController

public class ActiveMqProducerController {

@Autowired

JmsProducerClient jmsProducerClient;

@RequestMapping(value=”/producer”)

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

jmsProducerClient.send(msg);

return “sent”;

}

}

Step 3: Spring class

@Configuration

@EnableAutoConfiguration

@ComponentScan

public class JmsProducerApplication extends SpringBootServletInitializer {

public static void main(String[] args) {

SpringApplication.run(JmsProducerApplication.class, args);

}

}

Step 5: application.properties file

spring.activemq.broker-url=failover://tcp://localhost:61616

server.port=9000

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

spring.activemq.user=****

spring.activemq.password=****

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

jms.queue.destination=activemqdestination

Step 6: Producer Test Class 

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes = JmsProducerApplication.class)

@WebAppConfiguration

public class ProducerTest {

@Autowired

Publisher publisher;

@Test

public void testSendBinaryInformation() {

String message = “010111001100”;

publisher.send(message);

}

}

Note:

  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.
Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s