Saturday, October 6, 2018

ActiveMQ Consumer and producer

This tutorial will help you create simple consumer and producer app that uses ActiveMQ

Start AciveMQ server by executing activemq.bat which located installation path

Path:C:\Software\apache-activemq-5.15.6-bin\apache-activemq-5.15.6\bin\win64\activemq.bat


 

Folder structure

 

Files used

i). jndi.properties
ii).Producer1.java
iii.)Consumer1.java

JNDI.properties

# START SNIPPET: jndi

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector

java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory

# should appear as.

#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form

# queue.[jndiName] = [physicalName]

queue.MyQueue = example.TESTQueue

# register some topics in JNDI using the form

# topic.[jndiName] = [physicalName]

topic.MyTopic = example.MyTopic

# END SNIPPET: jndi
 

 Producer1.java

package com.activemqjndi;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.BasicConfigurator;

public class Producer1 {

       public Producer1() throws JMSException, NamingException, IOException {

             // Obtain a JNDI connection
             Properties jndiParameters = new Properties();
             jndiParameters.load(new FileInputStream("C:/Learning/MQQUEQE/src/com/activemqjndi/jndi.properties"));
             jndiParameters.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
             jndiParameters.put(Context.PROVIDER_URL, "tcp://localhost:61616");
             InitialContext jndi = new InitialContext(jndiParameters);

             // Look up a JMS connection factory
             ConnectionFactory conFactory = (ConnectionFactory) jndi

                           .lookup("ConnectionFactory");

             Connection connection;
             // Getting JMS connection from the server and starting it
             connection = conFactory.createConnection();
             try {

                    connection.start();

                    // JMS messages are sent and received using a Session. We will
                    // create here a non-transactional session object. If you want
                    // to use transactions you should set the first parameter to 'true'

                    Session session = connection.createSession(false,

                                 Session.AUTO_ACKNOWLEDGE);

                    Destination destination = (Destination) jndi.lookup("MyQueue");
                    // MessageProducer is used for sending messages (as opposed
                    // to MessageConsumer which is used for receiving them)

                    MessageProducer producer = session.createProducer(destination);

                    // We will send a small text message
                    Properties prop = new Properties();
                    prop.load(new FileInputStream("C:/Learning/MQQUEQE/src/com/message/message.txt"));
                    String message = prop.getProperty("msg");
                    TextMessage txtmessage = session.createTextMessage(message);

                    // Here we are sending the message!

                    producer.send(txtmessage);

                    System.out.println("Sent message '" + txtmessage.getText() + "'");

             } finally {

                    connection.close();

             }

       }

       public static void main(String[] args) throws JMSException, IOException {

             try {

                    BasicConfigurator.configure();

                    new Producer1();

             } catch (NamingException e) {

                    e.printStackTrace();

             }

       }

}
 



 You can find message sent by Producer1

Link:
http://localhost:8161/admin/queues.jsp
Go to Queues

 



 Message Details:

 




 Now consume message by Consumer1 program


 Consumer1.java

package com.activemqjndi;

import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.BasicConfigurator;

public class Consumer1 {

 // URL of the JMS server
 private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

 // Name of the queue we will receive messages from
 private static String subject = "example.TESTQueue";

 public static void main(String[] args) throws JMSException {

  BasicConfigurator.configure();
  // Getting JMS connection from the server
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  // Creating session for seding messages
  Session session = connection.createSession(false,
  Session.AUTO_ACKNOWLEDGE);

  // Getting the queue
  Destination destination = session.createQueue(subject);

  // MessageConsumer is used for receiving (consuming) messages
  MessageConsumer consumer = session.createConsumer(destination);

  // Here we receive the message.
  // By default this call is blocking, which means it will wait
  // for a message to arrive on the queue.

  Message message = consumer.receive();

  // There are many types of Message and TextMessage
  // is just one of them. Producer sent us a TextMessage
  // so we must cast to it to get access to its .getText()
  // method.

  if (message instanceof TextMessage) {

   TextMessage textMessage = (TextMessage) message;

   System.out.println("Received message '" + textMessage.getText()

     + "'");

  }

  connection.close();

 }

}




 
 














0 comments:

Post a Comment