activemq測試類

1057718341_h發表於2017-09-06

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();
}
}
}
}

相關文章