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.ConnectionFactor y;
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.Active MQConnecti on;
import org.apache.activemq.Active MQConnecti onFactory;
public class Consumer {
private static String url = ActiveMQConnection.DEFAULT _BROKER_UR L;
// 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.createCo nnection() ;
connection.start();
Session session = null;
session = connection.createSession(f alse,
Session.AUTO_ACKNOWLEDGE);
// Getting the queue 'TESTQUEUE'
Destination destination = null;
destination = session.createQueue(subjec t);
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = null;
consumer = session.createConsumer(des tination);
// Here we receive the message.
Message message = null;
message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Receiv ed 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.ConnectionFactor y;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.Active MQConnecti on;
import org.apache.activemq.Active MQConnecti onFactory;
public class Producer {
private static String url = ActiveMQConnection.DEFAULT _BROKER_UR L;
// 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.createCo nnection() ;
connection.start();
Session session = null;
session = connection.createSession(f alse,
Session.AUTO_ACKNOWLEDGE);
Destination destination = null;
destination = session.createQueue(subjec t);
MessageProducer producer = null;
producer = session.createProducer(des tination);
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
}
}
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.ConnectionFactor
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.Active
import org.apache.activemq.Active
public class Consumer {
private static String url = ActiveMQConnection.DEFAULT
// 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(
Connection connection = null;
try {
connection = connectionFactory.createCo
connection.start();
Session session = null;
session = connection.createSession(f
Session.AUTO_ACKNOWLEDGE);
// Getting the queue 'TESTQUEUE'
Destination destination = null;
destination = session.createQueue(subjec
// MessageConsumer is used for receiving (consuming) messages
MessageConsumer consumer = null;
consumer = session.createConsumer(des
// Here we receive the message.
Message message = null;
message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Receiv
+ textMessage.getText() + "'");
}
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
==========================
Producer:
/**
*
*/
package producerDriver;
import javax.jms.Connection;
import javax.jms.ConnectionFactor
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.Active
import org.apache.activemq.Active
public class Producer {
private static String url = ActiveMQConnection.DEFAULT
// 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(
Connection connection = null;
try {
connection = connectionFactory.createCo
connection.start();
Session session = null;
session = connection.createSession(f
Session.AUTO_ACKNOWLEDGE);
Destination destination = null;
destination = session.createQueue(subjec
MessageProducer producer = null;
producer = session.createProducer(des
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.