org.apache.activemq
activemq-all
5.11.2
package com.demo;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by luwan on 2018/4/14.
*/
public class App2 {
public static void main(String[] args) throws JMSException {
// 发送一个队列模式的消息
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.223.131:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 消息对象
Topic topic = session.createTopic("office-topic");
// 消息内容
TextMessage textMessage = session.createTextMessage("为中华民族伟大复兴而努力奋斗...");
// 发送端
MessageProducer producer = session.createProducer(topic);
// 发送消息
producer.send(textMessage);
// 关闭连接
producer.close();
session.close();
connection.close();
}
}
package ma。comsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by luwan on 2018/4/14.
*/
public class App3 {
public static void main(String[] args) throws Exception {
xl();
}
public static void xl() throws Exception {
// 创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.223.131:61616");
Connection connection = connectionFactory.createConnection();
connection.setClientID("1");
connection.start();
// 创建 会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建消息对象
Topic topic = session.createTopic("office-topic");
// 接收端
MessageConsumer consumer = session.createDurableSubscriber(topic, "1");
// 接收消息
consumer.setMessageListener(new MessageListener() {// 消息监听器,监听mq服务器的变化
@Override
public void onMessage(Message message) {
// 打印结果
TextMessage textMessage = (TextMessage) message;
String text = "";
try {
text = textMessage.getText();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println("读者,听到了" + text + ",背起了炸药包");
}
});
// 等待接收消息
System.in.read();
}
}
package macomsumer;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by luwan on 2018/4/14.
*/
public class App3 {
public static void main(String[] args) throws Exception {
xl();
}
public static void xl() throws Exception {
// 创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.223.131:61616");
Connection connection = connectionFactory.createConnection();
connection.setClientID("1");
connection.start();
// 创建 会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建消息对象
Topic topic = session.createTopic("office-topic");
// 接收端
MessageConsumer consumer = session.createDurableSubscriber(topic, "1");
// 接收消息
consumer.setMessageListener(new MessageListener() {// 消息监听器,监听mq服务器的变化
@Override
public void onMessage(Message message) {
// 打印结果
TextMessage textMessage = (TextMessage) message;
String text = "";
try {
text = textMessage.getText();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.err.println("雷锋,听到了" + text + ",复活了");
}
});
// 等待接收消息
System.in.read();
}
}
挂起的消息 在监听的人数 入队的消息 消费的消息 |
1 队列模式的消息,默认永久保存
2 订阅模式的消息,默认不永久保存,如果需要永久保存,需要为consumer在服务器上初始化一个永久保存的存储空间