• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1265
  • Last Modified:

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

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
lina06
Asked:
lina06
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now