Link to home
Start Free TrialLog in
Avatar of lina06
lina06

asked on

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
      }

}
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial