//Peer-to-peer - use the listener to receive messages
public class ConsumerListener {
// tcp address server-side address
//public static final String BROKER_URL =ActiveMQConnection.DEFAULT_BROKER_URL; // Its value is "tcp://localhost:61616";
public static final String BROKER_URL = "tcp://192.168.191.12:61616"; // Target address, the sent mq message can be queried
in the ActiveMQ administrator console to create http://localhost:8161/admin/queues.jsp
public static final String DESTINATION = "Jaycekon-MQ";
//The test connection uses the default user name
public static final String DEFAULT_USER = ActiveMQConnection.DEFAULT_USER;//The default is null
//The test connection uses the default password
public static final String DEFAULT_PASSWORD = ActiveMQConnection .DEFAULT_PASSWORD;//The default is null
public static void run() throws Exception {
QueueConnection connection = null;
QueueSession session = null;
try {
// 1. Create a connection factory
QueueConnectionFactory factory = new ActiveMQConnectionFactory(ConsumerListener.DEFAULT_USER, ConsumerListener.DEFAULT_PASSWORD, ConsumerListener.BROKER_URL);
// 2. Create a connection through the factory
connection = factory .createQueueConnection();
// 3. Start the connection
connection.start();
// 4. Create a session session
session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 5. Create a message queue
Queue queue = session.createQueue(DESTINATION);
// create message receiver
javax.jms.QueueReceiver receiver = session.createReceiver(queue);
//Use the inner class to load the corresponding Listener for the message receiver to monitor
receiver.setMessageListener(new MessageListener() {
//Rewrite the onMessage method
public void onMessage(Message msg) {
if (msg != null) {
TextMessage textMessage = (TextMessage) msg;
try {
System.out.println("Receive#" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// Sleep for 10s and then close all 10 messages sent by the producer
// It should be noted that using sleep here will make the currently executing thread go to sleep
// That is, the class QueueReceiver_Listener goes to sleep, The receiver's listener will still continue to execute.
Thread.sleep(1000 * 10);
// Commit session
session.commit();
} catch (Exception e) {
throw e;
} finally {
// Close release resources
if (session != null) {
session.close() ;
}
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws Exception {
ConsumerListener.run();
}
}
//Subscribing to messages using a listener
public class TopicListener {
// tcp address server-side address
//public static final String BROKER_URL =ActiveMQConnection.DEFAULT_BROKER_URL; // Its value is "tcp://localhost:61616";
public static final String BROKER_URL = "tcp://192.168.191.12:61616";
// The destination address, created in the ActiveMQ administrator console http://localhost:8161/admin/topics.jsp can query the sent mq message
public static final String DESTINATION = "jd.mq.topic";
//The test connection uses the default user name
public static final String DEFAULT_USER = ActiveMQConnection.DEFAULT_USER;//The default is null
//The test connection uses the default password
public static final String DEFAULT_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//The default is null
public static void run() throws Exception {
TopicConnection connection = null;
TopicSession session = null;
try {
// 1. Create a connection factory
TopicConnectionFactory factory = new ActiveMQConnectionFactory(TopicListener.DEFAULT_USER, TopicListener.DEFAULT_PASSWORD, TopicListener.BROKER_URL);
// 2. Create a connection through the factory
connection = factory .createTopicConnection();
// 3. Start the connection
connection.start();
// 4. Create a session session
session = connection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 5. Create a message queue
topic topic = session.createTopic(DESTINATION);
// 6. Create a message producer
TopicSubscriber subscriber = session.createSubscriber(topic);
//Subscribe to messages using a listener
subscriber.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
if (msg != null) {
TextMessage textMessage = (TextMessage) msg;
try {
System.out.println("Receive#" + textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
// Sleep for 100s and then close the receive producer All 10 messages sent
// It should be noted that using sleep here will make the currently executing thread go to sleep
// That is, the TopicReceiver_Listener class goes to sleep, and the receiver's listener will continue to execute.
Thread.sleep(1000 *100);
// Commit session
session.commit();
} catch (Exception e) {
throw e;
} finally {
// Close release resources
if (session != null) {
session.close() ;
}
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws Exception {
TopicListener.run();
}
}