JMS学习6 - 一个topic上多个consumer

/**
 * 测试一个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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值