{{theTime}}

Search This Blog

Total Pageviews

ActiveMQ JMS Example using Eclipse

- Install Open Source ActiveMQ software.
- Write a Sender and Receiver Program.
- Run the messaging server.
- Run the Sender and Receiver program in Eclipse.

Step1:

- Extract to folder and start Active MQ Server (bin\activemq.bat or activemq.sh).
- Server starts in port 8161.  Test the server http://localhost:8161.
- Default tcp port is 61616 (This port is required to connect using Java Program)

Step2:

- Create a Java project in Eclipse and write Sender and Receiver Program.
- Add activemq-all-5.4.3.jar file to the class path (Available in ActiveMQ Downloaded folder).
- Write Sender Program

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;

import java.util.Properties;

import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Sender
{
    public static void main(String[] mqargs) throws Exception {
        Properties mqproperties = new Properties();
        mqproperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        mqproperties.put(Context.PROVIDER_URL, "tcp://localhost:61616");
        mqproperties.put("queue.testSampleQueue", "TestQueue");
        InitialContext ctx = new InitialContext(mqproperties);
        Queue queue = (Queue) ctx.lookup("testSampleQueue");
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        QueueConnection queueConn = connFactory.createQueueConnection();
        QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);
        QueueMesssageSender queueMesssageSender = queueSession.createMesssageSender(queue);
        queueMesssageSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage message = queueSession.createTextMessage("My First MQ Program");
        queueMesssageSender.send(message);
        System.out.println("sent: " + message.getText());
        queueConn.close();
    }
}

- Run the program in Eclipse.
- Logon to localhost:8161 and verify the Messages Enqueued count.

Step3:

Write Receiver Program 

import javax.naming.Context;
import javax.naming.InitialContext;

import java.util.Properties;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

/**Receiver Program **/

public class Receiver
{
    public static void main(String mqargs[]) throws Exception {
        Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        properties.put(Context.PROVIDER_URL, "tcp://localhost:61616");
        properties.put("queue.testSampleQueue","TestQueue");
        InitialContext ctx = new InitialContext(properties);
        Queue queue = (Queue) ctx.lookup("testSampleQueue");
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        QueueConnection queueConn = connFactory.createQueueConnection();
        QueueSession queueSession = queueConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConn.start();
        TextMessage message = (TextMessage) queueReceiver.receive();
        System.out.println("received: " + message.getText());
        queueConn.close();
}
}

- Run the program in Eclipse
- Review the results in Console
- Logon to localhost:8161 and verify the Messages Dequeued count.

No comments:

Cucumber Eclipse sample program

Cucumber Eclipse SetupCreate Eclipse Maven Project Open pom.xml Goto Dependencies Add GroupId: info.cukes, Artifact Id: cucumber-java, Versi...