http://wangxinchun.iteye.com/blog/2146172
http://wangxinchun.iteye.com/blog/2146120
http://wangxinchun.iteye.com/blog/2145998
http://wangxinchun.iteye.com/blog/2145958
http://wangxinchun.iteye.com/blog/2147335
发布/订阅模式:针对一个发布者,多个订阅者都能接受到消息。
非持久订阅:只有当客户端处于激活状态,也就是和JMS Provider 保持连接状态才能收到发送到某个主题的消息,而当客户端处于离线状态,这个时间段发到主题的消息将会丢失,永远不会收到。
Publisher
/**
* 消息发布*/
public class Publisher {
private static final String BROKER_URL = "tcp://localhost:61616";
private static final Boolean NON_TRANSACTED = false;
private static final int NUM_MESSAGES_TO_SEND = 100;
private static final long DELAY = 100;
public static void main(String[] args) {
String url = BROKER_URL;
if (args.length > 0) {
url = args[0].trim();
}
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
Connection connection = null;
try {
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("test-topic");
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < NUM_MESSAGES_TO_SEND; i++) {
TextMessage message = session.createTextMessage("Message #" + i);
System.out.println("Sending message #" + i);
producer.send(message);
Thread.sleep(DELAY);
}
// tell the subscribers we're done
producer.send(session.createTextMessage("END"));
producer.close();
session.close();
} catch (Exception e) {
System.out.println("Caught exception!");
}
finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
System.out.println("Could not close an open connection...");
}
}
}
}
}
Subscriber
/**
* 消息订阅*/
public class Subscriber implements MessageListener {
private static final String BROKER_URL = "tcp://localhost:61616";
private static final Boolean NON_TRANSACTED = false;
private final CountDownLatch countDownLatch;
public Subscriber(CountDownLatch latch) {
countDownLatch = latch;
}
public static void main(String[] args) {
String url = BROKER_URL;
if (args.length > 0) {
url = args[0].trim();
}
System.out.println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit");
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url);
Connection connection = null;
final CountDownLatch latch = new CountDownLatch(1);
try {
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("test-topic");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new Subscriber(latch));
latch.await();
consumer.close();
session.close();
} catch (Exception e) {
System.out.println("Caught exception!");
}
finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
System.out.println("Could not close an open connection...");
}
}
}
}
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
String text = ((TextMessage) message).getText();
if ("END".equalsIgnoreCase(text)) {
System.out.println("Received END message!");
countDownLatch.countDown();
}
else {
System.out.println("Received message:" +text);
}
}
} catch (JMSException e) {
System.out.println("Got a JMS Exception!");
}
}
}
测试:
1、启动两个 Subscriber 然后启动一个 Publisher
结果:两个Subscriber 都收到了消息。
2、启动Publisher ,然后再启动 Subscriber
结果:Publisher发的消息,在 Publisher启动结束和 Subscriber启动结束之间的发送,全部丢失
分享到:
相关推荐
ActiveMQ-Topic订阅发布模式:参考博文:http://blog.csdn.net/ABAP_Brave/article/details/71211334
SpringBoot+ActiveMQ-发布订阅 Topic 主题 * 消息消费者(订阅方式)消费该消息 * 消费生产者将发布到topic中,同时有多个消息消费者(订阅)消费该消息 * 这种方式和点对点方式不同,发布到topic的消息会被所有...
1.一个是admin,用来显示和管理所有的queue、topic、connection等等。 2.一个是demo,有一些使用jms和activemq的简单例子。 3.还有一个fileserver,用来支持通过activemq发送文件时的中转服务器。blob message时配置...
MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。...2、一对多(topic)
activemq的Java依赖包,课构建Queue/topic 生产、消费
spring boot activemq集成示例,包含queue和topic消息的发送、接收,连接池的支持。
Apache ActiveMQ Queue Topic 详解 教程 加入代码解释说明
基于SpringBoot开发的ActiveMQ虚拟主题客户端,达到消费者多点消费、负载均衡和故障转移的目的。 详情可以参考这里: https://my.oschina.net/noryar/blog/1573047 https://my.oschina.net/noryar/blog/1575003
activemq 虚拟topic配置,可以将一个 topic转发为多个队列和多个topic或者将一个队列转发为多个topic和多个队列
一个jms activemq Topic 消息实例 关于jms JMS 是接口,相当于jdbc ,要真正使用它需要某些厂商进行实现 ,即jms provider 常见的jms provider 有 ActiveMQ JBoss 社区所研发的 HornetQ (在jboss6 中默认即可以...
在网上找了很多的topic持久化的Demo做了很多的测试,现把熟肉呈上。
activemq的topic队列模式的maven,spring的demo
欢迎下载ActiveMQ Topic 实例!
NULL 博文链接:https://768992698.iteye.com/blog/2343328
spring +activemq topic消息持久化订阅实例,整个项目中有activemq和spring的整合的所有实例,topic的持久化配置是在ApplicationContext3C、ApplicationContext3C2以及ApplicationContext3P三个中,消息生产者:...
详细描述了ActiveMQ消息过期-时间设置和自动清除解决方案。
activeMQ JMS 3种创建方式 公共方式 QUEUE TOPIC
ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS ...activemq的queue和topic JMS中定义了两种消息模型: 点对点(point to point, queue) 发布/订阅(publish/subscribe,topic)。
并且配置添加到服务里面方便管理,并且配置添加到服务里面方便管理 详细配置请访问:https://blog.csdn.net/Joe192/article/details/81215188
文章目录ActiveMQ简介1、ActiveMQ简介2、ActiveMQ下载SpringBoot整合ActiveMQ1、新建SpringBoot项目2、项目结构3、相关配置信息4、ActiveMQ配置类Queue队列模式1、队列生产者2、队列消费者3、测试效果Topic模式1、...