1、建立pom項目添加pom依賴
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.11.2</version>
</dependency>
2、創建man方法向MQ寫入隊列消息
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();
}
}
3、查看MQ的消息
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();
}
}
備註:注意第二種讀取,由於topic默認是不永久保存的,所以在producter中創建了一塊空間,producter發送消息後,即便是服務沒有開啓也會在重啓的時候讀到producter的信息
4、另一個comsumer接受MQ消息
4.客戶端表格的字段含義
掛起的消息 在監聽的人數 入隊的消息 消費的消息 |
1 隊列模式的消息,默認永久保存
2 訂閱模式的消息,默認不永久保存,如果需要永久保存,需要爲consumer在服務器上初始化一個永久保存的存儲空間