Sunday, 10 November 2013

Core JMS using Apache ActiveMQ

Java Messaging Service(JMS) using ActiveMQ


In the below example, we have no need to create the queue in the ActiveMQ, after the successful execution of the producer will automatically create the Queue in the Apache ActiveMQ.



System Requirements:-

  •       Eclipse Editor or any other.

  •       JDK 1.5 or higher(I am using jdk 1.7.0_03)

  •       Required jars(activemq-all-5.4.3.jar) as referenced library.       

  •       Apache-activemq-5.4.3


Note: - Apache Active MQ Setup is required for the execution of this example. For doing the Active MQ Setup please follow the below link:-


Steps for creating Eclipse java project for implementing Core JMS using Apache ActiveMQ:-

  • Create a java project named JMSUsingActiveMQ

  • Create a package names com.gaurav.jms.activemq in the src directory
  • Project Structure is as below:- 





  • Create an ActiveMQMessageProducer.javain the above specified package.

package com.gaurav.jms.activemq;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

importorg.apache.activemq.ActiveMQConnectionFactory;

public classActiveMQMessageProducer {
      publicstatic void main(String args[]) {
            try{
                  // Creating a connection factory for ActiveMQ
                  ActiveMQConnectionFactory activeMQConFactory = new ActiveMQConnectionFactory(
                              "tcp://localhost:61616");

                  // Creating a connection
                  Connection con = activeMQConFactory.createConnection();
                  con.start();

                  // Creating a session;
                  Session session = con
                              .createSession(false, Session.AUTO_ACKNOWLEDGE);

                  // Creating a destination using Topic or Queue
                  Destination dest = session.createQueue("TestWelcomeActiveMQQueue");

                  // creating a MessageProducer using the session to the topic or

                  // queue.
                  MessageProducer msgProducer = session.createProducer(dest);
                  msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

                  // Creating a message for sending in the queue
                  String strMessage = "WELCOME GAURAV BY "
                              + Thread.currentThread().getName();

                  //Creating a text message using the session.
                  TextMessage txtMessage = session.createTextMessage(strMessage);

                  System.out.println("Sent message : " + strMessage.hashCode()
                              + " : " + Thread.currentThread().getName());
                  msgProducer.send(txtMessage);

                  // closing the resources
                  msgProducer.close();
                  session.close();
                  con.close();

            } catch (Exception e) {
                  System.out.println("Exception thrown : " + e);
                  e.printStackTrace();
            }
      }

}

/* Note:- NON_PERSISTENT means no need for database specific persistent */


  •        Create an ActiveMQMessageConsumer.javain the above specified package.
  

package com.gaurav.jms.activemq;

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

importorg.apache.activemq.ActiveMQConnectionFactory;

public classActiveMQMessageConsumer {
      publicstatic void main(String args[]) {
            try{
                  // Creating a connection factory for ActiveMQ
                  ActiveMQConnectionFactory activeMQConFactory = new ActiveMQConnectionFactory(
                              "tcp://localhost:61616");

                  // Creating a connection
                  Connection con = activeMQConFactory.createConnection();
                  con.start();

                  // Creating a session;
                  Session session = con
                              .createSession(false, Session.AUTO_ACKNOWLEDGE);

                  // Creating a destination using Topic or Queue
                  Destination dest = session.createQueue("TestWelcomeActiveMQQueue");

                  // creating a MessageConsumer using the session for the topic or

                  // queue.
                  MessageConsumer msgConsumer = session.createConsumer(dest);

                  Message message = msgConsumer.receive(5000);

                  //Checking the message is an instance of textMessage or not.
                  if(message instanceof TextMessage){
                        TextMessage txtMessage = (TextMessage)message;
                        String strMessage = txtMessage.getText();
                        System.out.println("Received Message from queue is : "+strMessage);
                  }else{
                        System.out.println("Received : "+message);
                  }

                  // closing the resources
                  msgConsumer.close();
                  session.close();
                  con.close();

            } catch (Exception e) {
                  System.out.println("Exception thrown : " + e);
                  e.printStackTrace();
            }
      }
}



  • Execute the ActiveMQMessageProducer.java  first and then ActiveMQMessageConsumer.java by selecting the option Run as Java Application.

Result of  ActiveMQMessageProducer.java

Message Producer output :

Sent message : 707288012 : main

Result of  ActiveMQMessageConsumer.java

Message Consumer output:

Received Message from queue is: WELCOME GAURAV BY main


Result In the ActiveMQ console

URL for opening activemq admin console: - http://localhost:8161



No comments:

Post a Comment