-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJmsConsumerOnTopic.java
More file actions
36 lines (32 loc) · 1.22 KB
/
JmsConsumerOnTopic.java
File metadata and controls
36 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package frame.mq.active;
import org.apache.activemq.ActiveMQConnectionFactory;
import cn.qingweico.io.Print;
import javax.jms.*;
/**
* @author zqw
* @date 2023/10/12
*/
public class JmsConsumerOnTopic {
private static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
private static final String TOPIC_NAME = "topic";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(TOPIC_NAME);
MessageConsumer messageConsumer = session.createConsumer(topic);
messageConsumer.setMessageListener(message -> {
if (message instanceof TextMessage textMessage) {
try {
System.out.println("topic MessageListener " + textMessage.getText());
} catch (JMSException e) {
Print.err(e.getMessage());
}
}
});
messageConsumer.close();
session.close();
connection.close();
}
}