ActiveMQ(六) 转
package pfs.y2017.m11.mq.activemq.demo07; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*;
import java.util.Random; public class Client implements MessageListener {
private static int ackMode;
private static String clientQueueName; private boolean transacted = false;
private MessageProducer producer; static {
clientQueueName = "client.messages";
ackMode = Session.AUTO_ACKNOWLEDGE;
} public Client() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection;
try {
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(transacted, ackMode);
Destination adminQueue = session.createQueue(clientQueueName); // Setup a message producer to send message to the queue the server is consuming
// from
this.producer = session.createProducer(adminQueue);
this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create a temporary queue that this client will listen for responses on then
// create a consumer
// that consumes message from this temporary queue...for a real application a
// client should reuse
// the same temp queue for each message to the server...one temp queue per
// client
Destination tempDest = session.createTemporaryQueue();
MessageConsumer responseConsumer = session.createConsumer(tempDest); // This class will handle the messages to the temp queue as well
responseConsumer.setMessageListener(this); // Now create the actual message you want to send
TextMessage txtMessage = session.createTextMessage();
// 设置信息
txtMessage.setText("MyProtocolMessage"); // Set the reply to field to the temp queue you created above, this is the queue
// the server
// will respond to
txtMessage.setJMSReplyTo(tempDest); // Set a correlation ID so when you get a response you know which sent message
// the response is for
// If there is never more than one outstanding message to the server then the
// same correlation ID can be used for all the messages...if there is more than
// one outstanding
// message to the server you would presumably want to associate the correlation
// ID with this
// message somehow...a Map works good
String correlationId = this.createRandomString();
txtMessage.setJMSCorrelationID(correlationId);
this.producer.send(txtMessage);
} catch (JMSException e) {
// Handle the exception appropriately
}
} private String createRandomString() {
Random random = new Random(System.currentTimeMillis());
long randomLong = random.nextLong();
return Long.toHexString(randomLong);
} public void onMessage(Message message) {
String messageText = null;
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
messageText = textMessage.getText();
System.out.println("响应内容 = " + messageText);
}
} catch (JMSException e) {
// Handle the exception appropriately
}
} public static void main(String[] args) {
new Client();
}
}
package pfs.y2017.m11.mq.activemq.demo07; public class MessageProtocol {
public String handleProtocolMessage(String messageText) {
String responseText;
// 判断是否是client传过来的信息,在这里就可以做些解析
if ("MyProtocolMessage".equalsIgnoreCase(messageText)) {
responseText = "我收到了信息";
} else {
responseText = "我不知道你传的是什么: " + messageText;
} return responseText;
}
}
package pfs.y2017.m11.mq.activemq.demo07; import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Server implements MessageListener {
private static int ackMode;
private static String messageQueueName;
private static String messageBrokerUrl; private Session session;
private boolean transacted = false;
private MessageProducer replyProducer;
private MessageProtocol messageProtocol; static {
messageBrokerUrl = "tcp://localhost:61616";
messageQueueName = "client.messages";
ackMode = Session.AUTO_ACKNOWLEDGE;
} public Server() {
try {
// This message broker is embedded
BrokerService broker = new BrokerService();
broker.setPersistent(false);
broker.setUseJmx(false);
broker.addConnector(messageBrokerUrl);
broker.start();
} catch (Exception e) {
// Handle the exception appropriately
} // Delegating the handling of messages to another class, instantiate it before
// setting up JMS so it
// is ready to handle messages
this.messageProtocol = new MessageProtocol();
this.setupMessageQueueConsumer();
} private void setupMessageQueueConsumer() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
Connection connection;
try {
connection = connectionFactory.createConnection();
connection.start();
this.session = connection.createSession(this.transacted, ackMode);
Destination adminQueue = this.session.createQueue(messageQueueName); // Setup a message producer to respond to messages from clients, we will get the
// destination
// to send to from the JMSReplyTo header field from a Message
this.replyProducer = this.session.createProducer(null);
this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Set up a consumer to consume messages off of the admin queue
MessageConsumer consumer = this.session.createConsumer(adminQueue);
consumer.setMessageListener(this);
} catch (JMSException e) {
// Handle the exception appropriately
}
} public void onMessage(Message message) {
try {
TextMessage response = this.session.createTextMessage();
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String messageText = txtMsg.getText();
response.setText(this.messageProtocol.handleProtocolMessage(messageText));
} // Set the correlation ID from the received message to be the correlation id of
// the response message
// this lets the client identify which message this is a response to if it has
// more than
// one outstanding message to the server
response.setJMSCorrelationID(message.getJMSCorrelationID()); // Send the response to the Destination specified by the JMSReplyTo field of the
// received message,
// this is presumably a temporary queue created by the client
this.replyProducer.send(message.getJMSReplyTo(), response);
} catch (JMSException e) {
// Handle the exception appropriately
}
} public static void main(String[] args) {
new Server();
}
}
ActiveMQ(六) 转的更多相关文章
- 学习ActiveMQ(六):JMS消息的确认与重发机制
当我们发送消息的时候,会出现发送失败的情况,此时我们需要用到activemq为我们提供了消息重发机制,进行消息的重新发送.那么我们怎么知道消息有没有发送失败呢?activemq还有消息确认机制,消费者 ...
- java高级工程师(一)
一.无笔试题 不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试 二.三大框架方面问题 1.Spring 事务的隔离性,并说说每个隔离性的区别 ...
- 面试总结——Java高级工程师(一)
一.无笔试题 不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试 二.三大框架方面问题 1.Spring 事务的隔离性,并说说每个隔离性的区别 解答:spri ...
- JMS学习(六)-ActiveMQ的高可用性实现
原文地址:http://www.cnblogs.com/hapjin/p/5663024.html 一,ActiveMQ高可用性的架构 ActiveMQ的高可用性架构是基于Master/Slave 模 ...
- 消息中间件-activemq实战之消息持久化(六)
对于activemq消息的持久化我们在第二节的时候就简单介绍过,今天我们详细的来分析一下activemq的持久化过程以及持久化插件.在生产环境中为确保消息的可靠性,我们肯定的面临持久化消息的问题,今天 ...
- JMS学习六(ActiveMQ消息传送模型)
ActiveMQ 支持两种截然不同的消息传送模型:PTP(即点对点模型)和Pub/Sub(即发布 /订阅模型),分别称作:PTP Domain 和Pub/Sub Domain. 一.PTP消息传送模型 ...
- ActiveMQ 笔记(六)ActiveMQ的消息存储和持久化
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.持久化机制 1.Activemq持久化 1.1 什么是持久化: 持久化就是高可用的机制,即使服务器宕 ...
- ActiveMQ第六弹:设置多个并行的消费者
消息队列本来就是一种经典的生产者与消费者模式.生产者向消息队列中发送消息,消费者从消息队列中获取消息来消费. 消息的传送一般由一个代理来实现的,那就是Message broker(即消息代理).Mes ...
- ActiveMQ消息队列的使用及应用
这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录: 一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...
随机推荐
- Knockout v3.4.0 中文版教程-16-控制流-foreach绑定
2. 控制流 1. foreach绑定 目的 foreach绑定会遍历一个数组,为每个数组项生成重复的元素标记结构并做关联.这在渲染列表或表格的时候特别有用. 假设你的数组是一个监控数组,之后无论你进 ...
- Virtualbox虚拟机相关
Virtualbox虚拟机相关 Virtualbox是我一直使用的虚拟机,由于需要一些测试环境等,会经常使用多个虚拟机.经常捣腾.之前有涉及到一些virtualbox方面的问题的处理,并没有记录下来, ...
- bootshiro---开源的后台管理框架--基于springboot2+ shiro+jwt的真正rest api资源无状态认证权限管理框架,开发人员无需关注权限问题,后端开发完api,前端页面配置即可
https://gitee.com/tomsun28/bootshiro
- hdu6071[最短路+解不等式] 2017多校4
求出所有,从2走到x所需的花费在对 t = 2*min(d1,2, d2,3) 模运算下, 所有剩余系的最短路即可(把一个点拆成 t 个点, 每个点代表一种剩余系), 知道了所有剩余系就可以凑出答案 ...
- 学习系列 - 马拉车&扩展KMP
Manacher(马拉车)是一种求最长回文串的线性算法,复杂度O(n).网上对其介绍的资料已经挺多了的,请善用搜索引擎. 而扩展KMP说白了就是是求模式串和主串的每一个后缀的最长公共前缀[KMP更像是 ...
- BZOJ 2693 jzptab ——莫比乌斯反演
同BZOJ 2154 但是需要优化 $ans=\sum_{d<=n}d*\sum_{i<=\lfloor n/d \rfloor} i^2 *\mu(i)* Sum(\lfloor \fr ...
- USACO Runaround Numbers
题目大意:问最近的比n大的循环数是多少 思路:第n遍暴力大法好 /*{ ID:a4298442 PROB:runround LANG:C++ } */ #include<iostream> ...
- 巧克力王国 BZOJ 2850
巧克力王国 [问题描述] 巧克力王国里的巧克力都是由牛奶和可可做成的.但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜欢过于甜的巧克力.对于每一块巧克力,我们设x和y为其牛奶和可可的含量.由于 ...
- 转 Linux文件管理
Linux文件管理 http://www.cnblogs.com/vamei/archive/2012/09/09/2676792.html 作者:Vamei 出处:http://www.cnblog ...
- MongoDB 复制(副本集)学习
MongoDB 复制(副本集)学习 replication set复制集,复制集,多台服务器维护相同的数据副本,提高服务器的可用性.MongoDB复制是将数据同步在多个服务器的过程.复制提供了数据的冗 ...