Queue producer 2 (consumer reply message):
package mq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * Queue (point-to-point) mode survivor Producer * @author donald * */ public class QueueProducer2 { private static String user = ActiveMQConnection.DEFAULT_USER; private static String password =ActiveMQConnection.DEFAULT_PASSWORD; private static String url = "tcp://192.168.126.128:61616"; private static String qname = "testQueue"; private static String replyQueueName = "replyQueue"; static { } public static void main(String[] args)throws Exception { // ConnectionFactory : The connection factory that JMS uses to create connections ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url); // Connection : the connection from the JMS client to the JMS Provider Connection connection = connectionFactory.createConnection(); // Connection starts connection.start(); System.out.println("Connection is start..."); // Session: A thread that sends or receives messages Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE); // Queue : The destination of the message; to whom the message is sent. Queue destination = session.createQueue(qname); //The consumer accepts the message and replies to the replyQueue queue Queue replyQueue = session.createQueue(replyQueueName); // MessageProducer: message sender MessageProducer producer = session.createProducer(destination); // Set persistence, learn here, actually decide according to the project producer.setDeliveryMode(DeliveryMode.PERSISTENT); // Construct the message, hard-coded here, the item is the parameter, or the method gets sendMessage(session, producer,replyQueue); session.commit(); connection.close(); System.out.println("send text ok."); } public static void sendMessage(Session session, MessageProducer producer,Queue replyQueue) throws Exception { for (int i = 1; i <= 5; i++) {//There is a limit, it will not work if it reaches 1000 TextMessage message = session.createTextMessage("Queue message sent to ActiveMq" + i); message.setJMSReplyTo(replyQueue); // send message to destination System.out.println("Send message:" + "Queue message sent by ActiveMq" + i); producer.send(message); } } }
Queue consumer 3 consumes messages and replies to messages:
package mq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * Queue (point-to-point) way to consume this Consumer * @author donald * */ public class QueueConsumer3 { private static String user = ActiveMQConnection.DEFAULT_USER; private static String password =ActiveMQConnection.DEFAULT_PASSWORD; private static String url = "tcp://192.168.126.128:61616"; private static String qname = "testQueue"; public static void main(String[] args) throws Exception{ // ConnectionFactory : The connection factory that JMS uses to create connections ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url); // Connection : the connection from the JMS client to the JMS Provider Connection connection = connectionFactory.createConnection(); connection.start(); // Session: A thread that sends or receives messages final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // Destination : The destination of the message; to whom the message is sent. Queue destination=session.createQueue(qname); // consumer, message receiver MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener(){//There are transaction restrictions @Override public void onMessage(Message message) { try { TextMessage textMessage=(TextMessage)message; System.out.println("3 Consumption message: "+textMessage.getText()); MessageProducer producer = session.createProducer(message.getJMSReplyTo()); TextMessage replyMessage = session.createTextMessage(textMessage.getText()); producer.send(replyMessage); } catch (JMSException e1) { e1.printStackTrace(); } try { session.commit(); } catch (JMSException e) { e.printStackTrace (); } } }); /* Another way to accept * while (true) { //Set the time for the receiver to receive the message, for the convenience of testing, who set it as 100s here TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("Received message" + message.getText()); } else { break; } }*/ } }
Consumer 4, consumer consumer reply message
package mq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; /** * Queue (point-to-point) way to consume this Consumer * @author donald * */ public class QueueConsumer4 { private static String user = ActiveMQConnection.DEFAULT_USER; private static String password =ActiveMQConnection.DEFAULT_PASSWORD; private static String url = "tcp://192.168.126.128:61616"; private static String qname = "replyQueue"; public static void main(String[] args) throws Exception{ // ConnectionFactory : The connection factory that JMS uses to create connections ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url); // Connection : the connection from the JMS client to the JMS Provider Connection connection = connectionFactory.createConnection(); connection.start(); // Session: A thread that sends or receives messages final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // Destination : The destination of the message; to whom the message is sent. Queue destination=session.createQueue(qname); // consumer, message receiver MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener(){//There are transaction restrictions @Override public void onMessage(Message message) { try { TextMessage textMessage=(TextMessage)message; System.out.println("4 Consumer reply message: "+textMessage.getText()); } catch (JMSException e1) { e1.printStackTrace(); } try { session.commit(); } catch (JMSException e) { e.printStackTrace (); } } }); /* Another way to accept * while (true) { //Set the time for the receiver to receive the message, for the convenience of testing, who set it as 100s here TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("Received message" + message.getText()); } else { break; } }*/ } }
Turn on consumption, listen to 3,4, and then start producer 2;
console output
Producer 2:
Connection is start...
Send message: Queue message sent by ActiveMq 1
Send message: Queue message sent by ActiveMq 2
Send message: ActiveMq send Queue message 3
send message: Queue message sent by ActiveMq 4
send message: Queue message sent by ActiveMq 5
send text ok.
Consumer 3:
3 consuming message: Queue message sent to ActiveMq 1
3 consuming message: Queue sent to ActiveMq Message 2
3 Consuming message: Queue message sent to ActiveMq 3
3 Consuming message: Queue message sent to ActiveMq 4
3 Consuming message: Queue message sent to ActiveMq 5
Consumer 4:
4 Consumer reply message: Queue sent to ActiveMq Message 1
4 Consumer reply message: Queue message sent to ActiveMq 2
4 Consumer reply message: Queue message sent to ActiveMq 3
4 Consumer reply message: Queue message sent to ActiveMq 4
4 Consumer reply message: Sent to ActiveMq Queue message 5