activemq測試類
1、建立一個maven專案,pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.mq.maven</groupId>
<artifactId>activeMQ_helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、建立測試類:
1、生產者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
public class Product {
public static void main(String[] args) {
try {
// 建立mq工程類
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 獲取連結
Connection connection = connectionFactory.createConnection();
// 獲取session
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 建立一個訊息佇列
Queue queue = session.createQueue("helloworld");
// 建立生產者
MessageProducer producer = session.createProducer(queue);
System.out.println("生產者。。。。。。。。。。。");
for (int i = 0; i < 10; i++) {
TextMessage message = session.createTextMessage("hello world !" + i);
System.out.println(message.getText());
// 傳送訊息
producer.send(message);
}
// 提交事物
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
@Test
public void testQueueSender() {
try {
// 建立mq工程類
QueueConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 獲取連結
QueueConnection connection = connectionFactory.createQueueConnection();
connection.start();
// 獲取session
QueueSession queueSession = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 建立一個訊息佇列
Queue queue = queueSession.createQueue("helloworld2");
// 建立生產者
QueueSender createSender = queueSession.createSender(queue);
// 設定持久化模式
// createSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < 10; i++) {
String message = "傳送訊息第" + (i + 1) + "條";
MapMessage map = queueSession.createMapMessage();
map.setString("text", message);
map.setLong("time", System.currentTimeMillis());
System.out.println(map);
createSender.send(map);
}
// 提交事物
queueSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
// 傳送次數
public static final int SEND_NUM = 5;
// tcp 地址
public static final String BROKER_URL = "tcp://localhost:61616";
// 目標,在ActiveMQ管理員控制檯建立 http://localhost:8161/admin/queues.jsp
public static final String DESTINATION = "hoo.mq.topic";
@Test
public void testPublisherTopic() throws Exception {
TopicConnection connection = null;
TopicSession session = null;
try {
// 建立連結工廠
TopicConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
// 通過工廠建立一個連線
connection = factory.createTopicConnection();
// 啟動連線
connection.start();
// 建立一個session會話
session = connection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 建立一個訊息佇列
Topic topic = session.createTopic(DESTINATION);
// 建立訊息傳送者
TopicPublisher publisher = session.createPublisher(topic);
// 設定持久化模式
publisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < SEND_NUM; i++) {
String message = "傳送訊息第" + (i + 1) + "條";
MapMessage map = session.createMapMessage();
map.setString("text", message);
map.setLong("time", System.currentTimeMillis());
System.out.println(map);
publisher.send(map);
}
// 提交會話
session.commit();
} catch (Exception e) {
throw e;
} finally {
// 關閉釋放資源
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
}
2、消費者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
public class Customer {
public static void main(String[] args) throws Exception {
ConnectionFactory conFactory = new ActiveMQConnectionFactory();
// 建立連結
Connection connection = conFactory.createConnection();
// 開啟連線
connection.start();
// 建立sesssion
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 建立訊息佇列
Queue queue = session.createQueue("helloworld");
// 建立消費者
MessageConsumer consumer = session.createConsumer(queue);
System.out.println("消費者。。。。。。。。。。。");
while (true) {
TextMessage message = (TextMessage) consumer.receive(1000);
if (message != null) {
String str = message.getText();
System.out.println(str);
} else {
break;
}
}
}
@Test
public void customerLinster() throws Exception {
ConnectionFactory conFactory = new ActiveMQConnectionFactory();
// 建立連結
Connection connection = conFactory.createConnection();
// 開啟連線
connection.start();
// 建立sesssion
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 建立訊息佇列
Queue queue = session.createQueue("helloworld");
// 建立消費者
MessageConsumer consumer = session.createConsumer(queue);
System.out.println("消費者。。。。。。。。。。。");
// 設定監聽器,實時監控,只要有生產,消費者馬上消費
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage msgMessage = (TextMessage) message;
String text = msgMessage.getText();
System.out.println(text);
} else if (message instanceof MapMessage) {
MapMessage msgMessage = (MapMessage) message;
System.out.println(msgMessage.getString("text"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
while (true) {
// 不能讓junit執行緒死掉
}
}
@Test
public void testQueueReceiver() {
try {
// 建立mq工程類
QueueConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 獲取連結
QueueConnection connection = connectionFactory.createQueueConnection();
// 啟動連結
connection.start();
// 獲取session
QueueSession queueSession = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 建立一個訊息佇列
Queue queue = queueSession.createQueue("helloworld2");
// 建立生產者
QueueReceiver receiver = queueSession.createReceiver(queue);
receiver.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
MapMessage map = (MapMessage) message;
try {
System.out.println(map.getString("text"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
// 提交會話
queueSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
// tcp 地址
public static final String BROKER_URL = "tcp://localhost:61616";
// 目標,在ActiveMQ管理員控制檯建立 http://localhost:8161/admin/queues.jsp
public static final String TARGET = "hoo.mq.topic";
@Test
public void testCreateSubscriber() throws Exception {
TopicConnection connection = null;
TopicSession session = null;
try {
// 建立連結工廠
TopicConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
// 通過工廠建立一個連線
connection = factory.createTopicConnection();
// 啟動連線
connection.start();
// 建立一個session會話
session = connection.createTopicSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 建立一個訊息佇列
Topic topic = session.createTopic(TARGET);
// 建立訊息製作者
TopicSubscriber subscriber = session.createSubscriber(topic);
subscriber.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
if (msg != null) {
MapMessage map = (MapMessage) msg;
try {
System.out.println(map.getLong("time") + "接收#" + map.getString("text"));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
// 提交會話
session.commit();
} catch (Exception e) {
throw e;
} finally {
// 關閉釋放資源
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
}
相關文章
- Spring+ActiveMQ整合測試SpringMQ
- ActiveMQ壓力測試工具(emqtt_benchmark和jmeter)MQQTJMeter
- 測試random類random
- 軟體測試之測試分類_1.4
- Web效能測試種類與全面測試模型Web模型
- jmeter之——jms壓測(activemq)JMeterMQ
- 軟體測試分類
- 代理類測試程式碼
- 軟體測試——三、軟體測試的分類
- 責任鏈測試java類Java
- activeMQ主要的幾類叢集部署方式MQ
- Color類和測試記憶體的類MemoryMonitor記憶體
- 軟體測試主要種類大全
- 軟體測試專項分類
- junit執行多個測試類
- 黑盒測試---等價類劃分法
- 測試測試測試測試測試測試
- 軟體測試探秘:從各類軟體測試入門,領略測試的奧秘
- 效能測試學習(1)-效能測試分類與常見術語
- 自動化測試 RobotFramework自定義靜態測試類庫總結Framework
- Service層的怎麼寫測試類
- C++ 巢狀類簡單測試C++巢狀
- python+pytest介面自動化(11)-測試函式、測試類/測試方法的封裝Python函式封裝
- python+pytest介面自動化之測試函式、測試類/測試方法的封裝Python函式封裝
- ActiveMQMQ
- 測試人必看:五大維度解讀軟體測試分類
- oracle分割槽表的分類及測試Oracle
- 物件導向的類測試技術研究物件
- 軟體測試工具的分類和使用
- c# stream類相關測試續(二)C#
- 1個月5次發版:測試人的模組測試策略分類歸納
- Java測試內部類和巢狀類位置的合法性Java巢狀
- java設計學生類並進行測試Java
- 黑盒測試技術之等價類劃分
- 測試類啟動報錯缺失servletWebServerApplicationContextServletWebServerAPPContext
- App測試、Web測試和介面測試一般測試流程APPWeb
- ActiveMQ入門MQ
- 安裝activemqMQ