?
Solved

JMS using ActiveMQ in Java, a single Producer-Consumer communication

Posted on 2011-05-11
1
Medium Priority
?
1,193 Views
Last Modified: 2012-05-11
I wrote the simple application using ActiveMQ.   See the code below.  Please suggest me how to modify the program to perform the following task
1.) Producer is running during some time T.
2.) During this time T, Producer sends x messages.
3.) Producer sends 1 message, sleeps during time p, and sends another message.
3.) Consumer is running to consume all messages.  The user will stops Consumer that all messages are received according to the ActiveMQ admin console (http://localhost:8161/admin/queues.jsp).
I am using Java 1.6 and Apache ActiveMQ 5.5.0
========================================
Consumer:
package consumerDriver;


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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;



public class Consumer {

       private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

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


      public static void main(String[] args) {
             // Getting JMS connection from the server
        ConnectionFactory connectionFactory
            = new ActiveMQConnectionFactory(url);
        Connection connection = null;
            try {
                  connection = connectionFactory.createConnection();
                  connection.start();
            
                  
                  Session session = null;
            
                  session = connection.createSession(false,
                      Session.AUTO_ACKNOWLEDGE);
            
                  // Getting the queue 'TESTQUEUE'
                  Destination destination = null;
            
                  destination = session.createQueue(subject);
            

                  // MessageConsumer is used for receiving (consuming) messages
                  MessageConsumer consumer = null;
                  consumer = session.createConsumer(destination);
            
                  // Here we receive the message.
                  Message message = null;
                  message = consumer.receive();
            
                  
                  if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                  System.out.println("Received message '"
                            + textMessage.getText() + "'");
                   }
       
                  connection.close();
            } catch (JMSException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
    }
}
====================================
Producer:
/**
 *
 */
package producerDriver;


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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class Producer {

      private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

          // Name of the queue we will be sending messages to
          private static String subject = "TESTQUEUE";

      public static void main(String[] args) {
            // Getting JMS connection from the server and starting it
        ConnectionFactory connectionFactory =
            new ActiveMQConnectionFactory(url);
        Connection connection = null;
            try {
                  connection = connectionFactory.createConnection();
                  connection.start();

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

                  Destination destination = null;
                  destination = session.createQueue(subject);
            
                  MessageProducer producer = null;
                  producer = session.createProducer(destination);
            
                  TextMessage message = null;
                  message = session.createTextMessage();
                  
                  for (int i = 1; i < 10; i++)
                  {
                        message.setText("This is message " + i);
                        // Here we are sending the message!
                        producer.send(message);
                        System.out.println("Sent message '" + message.getText() + "'");
                  }
            
                  //connection.close();
            } catch (JMSException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
            finally{
                  if( connection != null){
                        try{
                              connection.close();
                        } catch (JMSException e){
                              e.printStackTrace();
                        }
                  }//if( connection != null)
            }//finally
      }

}
0
Comment
Question by:lina06
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 35738438
You should only need to do 2 things...

 - Put the consumer.receive()/message printing lines into a loop

 - Add a delay (of p seconds) inside the for loop in the producer

I may be missing something, but it seems that easy.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question