Publisher/Subscriber(发布/订阅者)消息模式开发流程
该模式的作用是发布者和订阅者 可以相互发送消息
发布者和订阅者都充当 生产者和消费者
发布者
package publisher.to.subscriber;
import java.awt.font.TextMeasurer;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
public class Publisher implements MessageListener {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
Connection connection = factory.createConnection();
connection.start();
// 第一个参数设置是否需要事务支持
Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
// 发送消息 而 订阅者接受消息必须有个topic 名字也叫 msg.send
Destination sendTopic = new ActiveMQTopic("msg.message");
// 接受消息 而 订阅者发送消息必须有个topic 名字也叫 msg.receive
Destination sendReceive = new ActiveMQTopic("msg.control");
MessageProducer producer = session.createProducer(sendTopic);
MessageConsumer consumer = session.createConsumer(sendReceive);
Rec rec = new Rec(consumer,session,connection,producer);
rec.start();
}
public void onMessage(Message msg) {
// TODO Auto-generated method stub
}
}
class Rec extends Thread {
private MessageConsumer consumer = null;
private Session session = null;
private Connection connection=null;
MessageProducer producer=null;
public Rec(MessageConsumer consumer, Session session,Connection connection,MessageProducer producer) {
this.consumer = consumer;
this.session = session;
this.connection=connection;
this.producer=producer;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
TextMessage smsg = session.createTextMessage("hello my name is liaomin");
producer.send(smsg);
session.commit();
TextMessage msg = (TextMessage) consumer.receive();
System.out.println(msg.getText());
// 接收时必须提交以下 否则会出现上次收到的旧数据
session.commit();
session.close();
connection.close();
break;
} catch (Exception e) {
}
}
}
}
订阅者
package publisher.to.subscriber;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
public class Subscriber {
/**
* @param args
* @throws JMSException
*/
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
Connection connection = factory.createConnection();
connection.start();
// 第一个参数设置是否需要事务支持
Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
//订阅者接受消息
Destination receiveTopic = new ActiveMQTopic("msg.message");
//订阅者发送消息
Destination sendTopic = new ActiveMQTopic("msg.control");
MessageProducer producer = session.createProducer(sendTopic);
MessageConsumer consumer = session.createConsumer(receiveTopic);
Rec1 rec = new Rec1(consumer,session,connection,producer);
rec.start();
}
public void onMessage(Message msg) {
// TODO Auto-generated method stub
}
}
class Rec1 extends Thread {
private MessageConsumer consumer = null;
private Session session = null;
private Connection connection;
private MessageProducer producer;
public Rec1(MessageConsumer consumer, Session session,Connection connection,MessageProducer producer) {
this.consumer = consumer;
this.session = session;
this.connection=connection;
this.producer=producer;
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
TextMessage msg = (TextMessage) consumer.receive();
session.commit();
System.out.println(msg.getText());
TextMessage remsg = session.createTextMessage("ok receive");
producer.send(remsg);
// 接收时必须提交以下 否则会出现上次收到的旧数据
session.commit();
session.close();
connection.close();
break;
} catch (Exception e) {
}
}
}
}
先运行 订阅者 在运行 发布者
Publisher/Subscriber(发布/订阅者)消息模式开发流程的更多相关文章
- JavaScript实现的发布/订阅(Pub/Sub)模式
JavaScript实现的发布/订阅(Pub/Sub)模式 时间 2016-05-02 18:47:58 GiantMing's blog 原文 http://giantming.net/java ...
- Kafka 分布式的,基于发布/订阅的消息系统
Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 通过O(1)的磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. 高吞吐量:即使是非常 ...
- kafka高吞吐量的分布式发布订阅的消息队列系统
一:kafka介绍kafka(官网地址:http://kafka.apache.org)是一种高吞吐量的分布式发布订阅的消息队列系统,具有高性能和高吞吐率. 1.1 术语介绍BrokerKafka集群 ...
- node-amqp 使用fanout发布订阅rabbitmq消息
publisher代码 const amqp = require('amqp'); let option = { host: 'server-ip', port: 5672, login: 'gues ...
- redis实现消息队列&发布/订阅模式使用
在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录. Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...
- JMS 企业开发流程实现
关于JMS的一些介绍参见[http://blog.csdn.net/aking21alinjuju/article/details/6051421] [补充] 消息的组成 1. 头(head) 每条J ...
- “一切都是消息”--MSF(消息服务框架)之【发布-订阅】模式
在上一篇,“一切都是消息”--MSF(消息服务框架)之[请求-响应]模式 ,我们演示了MSF实现简单的请求-响应模式的示例,今天来看看如何实现[发布-订阅]模式.简单来说,该模式的工作过程是: 客户端 ...
- “一切都是消息”--iMSF(即时消息服务框架)之【发布-订阅】模式
MSF的名字是 Message Service Framework 的简称,由于目前框架主要功能在于处理即时(immediately)消息,所以iMSF就是 immediately Message S ...
- 【EasyNetQ】- 发布/订阅模式
EasyNetQ支持的最简单的消息传递模式是发布/ 订阅.这种模式是消除消费者信息提供者的绝佳方式.出版商简单地向全世界说,“这已经发生了”或“我现在有了这些信息”.它不关心是否有人正在倾听,他们可能 ...
随机推荐
- 【转载】Using the Web Service Callbacks in the .NET Application
来源 This article describes a .NET Application model driven by the Web Services using the Virtual Web ...
- No Hibernate Session bound to thread, and configuration does not allow creat
No Hibernate Session bound to thread, and configuration does not allow creat 今天遇到这么一个错误,在网上差了很多都没有能解 ...
- 【技术贴】Maven打包文件增加时间后缀
构建war包,或者jar包的,时候,maven会自动增加一个版本号和时间放在jar包后面比如poi-3.9-20131115.jar这样子,但是我自己打war包,总是给我生成一个快照的后缀report ...
- Central Europe Regional Contest 2012 Problem J: Conservation
题目不难,感觉像是一个拓扑排序,要用双端队列来维护: 要注意细节,不然WA到死 = =! #include<cstdio> #include<cstring> #includ ...
- SEO 网站URL优化
很多人都知道URL对SEO的重要之处,但是很多站点却忽略了站点的路径优化.今天本人在这里写几点关于优化路径小篇! 本人结论出关于站点URL在优化中其实也是占为一个相当重要的一个优化!优化站点的URL本 ...
- Linq查询出结果集中重复数据
private List<FMDS_FarmPlotNewInfo> GetSame(List<FMDS_FarmPlotNewInfo> lst) { List<FMD ...
- JDBC事务控制管理
1.事务 (1)事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句 update account set mone ...
- FindBugs
FindBugs是一个能静态分析源代码中可能会出现Bug的Eclipse插件工具. 可以从http://sourceforge.net/project/showfiles.php?group_id=9 ...
- 为web服务器设置HttpOnly防范XSS攻击
HttpOnly标识是一个可选的.避免利用XSS(Cross-Site Scripting)来获取session cookie的标识.XSS攻击最常见一个的目标是通过获取的session cookie ...
- js函数参数设置默认值
php有个很方便的用法是在定义函数时可以直接给参数设默认值,如: function simue ($a=1,$b=2){ return $a+$b;}echo simue(); //输出3echo ...