Apache Active MQ 之Topic

本文通过示例介绍了如何使用Apache ActiveMQ实现发布/订阅模式的消息传递。包括生产者发送消息到Topic,以及多个消费者接收同一份消息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apache Active MQ 之Topic:

Pre-Condition:安装好Apache ActiveMQ并启动服务

消费者:

package com.wx.jms.topic;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

/**
* @Class name: Topic_Test.java
*
* Short description on the purpose of the program.
*
* @author: wangxiang
* @modified: Mar 22, 2011
*
*/

public class Topic_Test {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection conn = factory.createConnection();
conn.start();

Topic topic = new ActiveMQTopic("mqtopic");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer1 = session.createConsumer(topic);
consumer1.setMessageListener(new MessageListener(){
public void onMessage(Message msg) {
try {
System.out.println("consumer1:" + ((TextMessage)msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

MessageConsumer consumer2 = session.createConsumer(topic);
consumer2.setMessageListener(new MessageListener(){
public void onMessage(Message msg) {
try {
System.out.println("consumer2:" + ((TextMessage)msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

}

}


生产者:

package com.wx.jms.topic;

import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;

/**
* @Class name: TopicProducer_Main.java
*
* Short description on the purpose of the program.
*
* @author: wangxiang
* @modified: Mar 22, 2011
*
*/

public class TopicProducer_Main {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
Connection conn = factory.createConnection();
conn.start();

Topic topic = new ActiveMQTopic("mqtopic");
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(topic);
for (int i = 1; i < 12; i++) {
producer.send(session.createTextMessage("msg " + i));
}
conn.stop();
}

}


结果:
[b]consumer1:msg 1
consumer2:msg 1
consumer1:msg 2
consumer2:msg 2
consumer1:msg 3
consumer2:msg 3
consumer1:msg 4
consumer2:msg 4
consumer1:msg 5
consumer2:msg 5
consumer1:msg 6
consumer2:msg 6
consumer1:msg 7
consumer2:msg 7
consumer1:msg 8
consumer2:msg 8
consumer1:msg 9
consumer2:msg 9[/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值