Sunday 5 August 2018

ActiveMQ: Hello World Application

This is continuation to my previous post. If you are not set up ActiveMQ in the system, I would recommend you go through my previous post and set it up.

My ActiveMQ instance is running at http://127.0.0.1:5050

Below step-by-step explains how to define a JMS producer that send messages to ActiveMQ, and define a JMS receiver that receives messages from ActiveMQ.

Step 1: Create new maven project in Eclipse.

Step 2: Add below maven dependencies to the pom.xml.
                  <dependency>
                           <groupId>org.apache.activemq</groupId>
                           <artifactId>activemq-client</artifactId>
                           <version>5.15.3</version>
                  </dependency>

Step 3: Create new package ‘com.sample.producer’ and define the class JMSProducer.java like below.

JMSProducer.java
package com.sample.producer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String[] args) throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = session.createQueue(QUEUE);

  MessageProducer producer = session.createProducer(destination);

  TextMessage message = session.createTextMessage("Hello, this is my first message.");

  producer.send(message);

  connection.close();
 }
}

As you see above snippet, I am connecting the url "tcp://127.0.0.1:5050",

ActiveMQ provides number of transport protocols to communicate with JMS provider.

You can get the information from {ACTIVE_MQ_INSTALL_DIR}/conf/activemq.xml file
 <transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:5050?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

Step 4: Create new package ‘com.sample.consumer’ and define a class JMSConsumer.java like below.

JMSConsumer.java
package com.sample.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSConsumer {
 private static final String JMS_PROVIDER_URL = "tcp://127.0.0.1:5050";
 private static final String QUEUE = "DEMO_QUEUE";

 public static void main(String args[]) throws JMSException {
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_PROVIDER_URL);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  Destination destination = session.createQueue(QUEUE);

  MessageConsumer consumer = session.createConsumer(destination);

  Message message = consumer.receive();

  if (message instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) message;
   System.out.println("Received message '" + textMessage.getText() + "'");
  }

  connection.close();
 }
}


Step 5: Run the application JMSProducer.java.

Open the url ‘http://127.0.0.1:8161/admin/queues.jsp’, you can able to see DEMO_QUEUE and one message is enqueued to it.


Step 6: Run ‘JMSConsumer’ application.

You can able to see below messages.
Received message 'Hello, this is my first message.'

Open the url ‘http://127.0.0.1:8161/admin/queues.jsp’, you can able to see DEMO_QUEUE is empty (Number Of Pending Messages 0).


That’s it, you are done with first application.



Previous                                                 Next                                                 Home

No comments:

Post a Comment