Publish-subscribe messages: a message is consumed multiple times (group sending)
Receive messages in a blocking manner
public class TopicProducter {
// Send times
public static final int SEND_NUM = 10;
// tcp address server 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 can be queried
in the ActiveMQ administrator console to create http://localhost:8161/admin/topics.jsp 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
/**
* 消息发送端
* @param session
* @param publisher
* @throws Exception
*/
public static void sendMessage(TopicSession session, TopicPublisher publisher) throws Exception {
for (int i = 0; i < SEND_NUM; i++) {
String message = "发送消息第" + (i + 1) + "条";
TextMessage textMessage = session.createTextMessage(message);
System.out.println(textMessage.getText());
//发送 Topic消息
publisher.send(textMessage);
}
}
public void run() throws Exception {
//Topic连接
TopicConnection connection = null;
//Topic会话
TopicSession session = null;
try {
// 1. Create a link factory
TopicConnectionFactory factory = new ActiveMQConnectionFactory(TopicProducter.DEFAULT_USER, TopicProducter.DEFAULT_PASSWORD, TopicProducter.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 sender
TopicPublisher publisher = session.createPublisher(topic);
// set persistence mode
publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, publisher);
// commit session
session.commit();
} catch (Exception e) {
throw e;
} finally {
// close release resource
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
public static void main(String[] args) throws Exception {
new TopicProducter(). run();
}
}
public class TopicReceiver {
// tcp address server 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"; // Destination address, you can query the sent mq message
in the ActiveMQ administrator console to create http://localhost:8161/admin/topics.jsp
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;//默认为null
public static void run() throws Exception {
TopicConnection connection = null;
TopicSession session = null;
try {
// 1. Create a link factory
TopicConnectionFactory factory = new ActiveMQConnectionFactory(TopicReceiver.DEFAULT_USER, TopicReceiver.DEFAULT_PASSWORD, TopicReceiver.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 message producer
final TopicSubscriber subscriber = session.createSubscriber(topic);
//Receive the message sent by the Topic producer
//It should be noted that a new thread needs to be started to deal with the problem
new Thread(){
public void run(){
TextMessage textMessage = null;
try {
while(true){ //Continue to receive messages
textMessage = (TextMessage) subscriber.receive();
if(textMessage==null)
break;
System.out.println("Receive#" + textMessage.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}.start();
// Sleep for 100s 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 TopicReceiver_Receive class goes to sleep, and the receiver The new thread just started by the .start method 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 {
TopicReceiver.run();
}
}