/**
* 测试一个topic上有多人在发送消息,和多人同时接收消息的情况
*
* @author sunjun
*/
public class TopicTest1 extends JMSBase {
public static void main(String[] args) throws InterruptedException {
TopicTest1 topic = new TopicTest1();
// 接收消息
for (int i = 1; i <= 2; i++)
new Thread(topic.new Receiver("consumer" + i)).start();
// 发送消息
for (int i = 0; i < 2; i++)
new Thread(topic.new Sender()).start();
}
private Destination getDestination(Session session) throws JMSException {
return session.createTopic("my.topic");
}
class Sender extends JMSBase implements Runnable {
@Override
public void run() {
try {
Connection con = getConnection();
Session session = con.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = getDestination(session);
MessageProducer producer = session.createProducer(destination);
// 发送的消息进行持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 5; i++) {
Thread.sleep(2000);
TextMessage message = session
.createTextMessage("text message with thread-"
+ Thread.currentThread() + " index-" + i);
producer.send(message);
}
close(session, con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Receiver extends JMSBase implements Runnable {
private String name;
public Receiver(String name) {
this.name = name;
}
@Override
public void run() {
Connection con;
try {
con = getConnection();
con.start();
Session session = con.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = getDestination(session);
MessageConsumer consumer = session.createConsumer(destination);
while (true) {
Message message = consumer.receive();
if (message != null) {
System.out.println(name + " message:"
+ ((TextMessage) message).getText());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
结论:
1,对于topic,consumer只能接收到它注册到topic上以后别人发送到topic的消息
2,每个consumer都会收到topic上的每个消息
测试输出:
consumer2 message:text message with thread-Thread[Thread-1,5,main] index-0
consumer1 message:text message with thread-Thread[Thread-1,5,main] index-0
consumer1 message:text message with thread-Thread[Thread-0,5,main] index-0
consumer2 message:text message with thread-Thread[Thread-0,5,main] index-0
consumer1 message:text message with thread-Thread[Thread-1,5,main] index-1
consumer1 message:text message with thread-Thread[Thread-0,5,main] index-1
consumer2 message:text message with thread-Thread[Thread-1,5,main] index-1
consumer2 message:text message with thread-Thread[Thread-0,5,main] index-1
consumer2 message:text message with thread-Thread[Thread-1,5,main] index-2
consumer1 message:text message with thread-Thread[Thread-1,5,main] index-2
consumer2 message:text message with thread-Thread[Thread-0,5,main] index-2
consumer1 message:text message with thread-Thread[Thread-0,5,main] index-2
consumer1 message:text message with thread-Thread[Thread-1,5,main] index-3
consumer2 message:text message with thread-Thread[Thread-1,5,main] index-3
consumer2 message:text message with thread-Thread[Thread-0,5,main] index-3
consumer1 message:text message with thread-Thread[Thread-0,5,main] index-3
consumer1 message:text message with thread-Thread[Thread-1,5,main] index-4
consumer2 message:text message with thread-Thread[Thread-1,5,main] index-4
consumer1 message:text message with thread-Thread[Thread-0,5,main] index-4
consumer2 message:text message with thread-Thread[Thread-0,5,main] index-4