1. package pfs.y2017.m11.mq.activemq.demo07;
  2.  
  3. import org.apache.activemq.ActiveMQConnectionFactory;
  4.  
  5. import javax.jms.*;
  6. import java.util.Random;
  7.  
  8. public class Client implements MessageListener {
  9. private static int ackMode;
  10. private static String clientQueueName;
  11.  
  12. private boolean transacted = false;
  13. private MessageProducer producer;
  14.  
  15. static {
  16. clientQueueName = "client.messages";
  17. ackMode = Session.AUTO_ACKNOWLEDGE;
  18. }
  19.  
  20. public Client() {
  21. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  22. Connection connection;
  23. try {
  24. connection = connectionFactory.createConnection();
  25. connection.start();
  26. Session session = connection.createSession(transacted, ackMode);
  27. Destination adminQueue = session.createQueue(clientQueueName);
  28.  
  29. // Setup a message producer to send message to the queue the server is consuming
  30. // from
  31. this.producer = session.createProducer(adminQueue);
  32. this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  33.  
  34. // Create a temporary queue that this client will listen for responses on then
  35. // create a consumer
  36. // that consumes message from this temporary queue...for a real application a
  37. // client should reuse
  38. // the same temp queue for each message to the server...one temp queue per
  39. // client
  40. Destination tempDest = session.createTemporaryQueue();
  41. MessageConsumer responseConsumer = session.createConsumer(tempDest);
  42.  
  43. // This class will handle the messages to the temp queue as well
  44. responseConsumer.setMessageListener(this);
  45.  
  46. // Now create the actual message you want to send
  47. TextMessage txtMessage = session.createTextMessage();
  48. // 设置信息
  49. txtMessage.setText("MyProtocolMessage");
  50.  
  51. // Set the reply to field to the temp queue you created above, this is the queue
  52. // the server
  53. // will respond to
  54. txtMessage.setJMSReplyTo(tempDest);
  55.  
  56. // Set a correlation ID so when you get a response you know which sent message
  57. // the response is for
  58. // If there is never more than one outstanding message to the server then the
  59. // same correlation ID can be used for all the messages...if there is more than
  60. // one outstanding
  61. // message to the server you would presumably want to associate the correlation
  62. // ID with this
  63. // message somehow...a Map works good
  64. String correlationId = this.createRandomString();
  65. txtMessage.setJMSCorrelationID(correlationId);
  66. this.producer.send(txtMessage);
  67. } catch (JMSException e) {
  68. // Handle the exception appropriately
  69. }
  70. }
  71.  
  72. private String createRandomString() {
  73. Random random = new Random(System.currentTimeMillis());
  74. long randomLong = random.nextLong();
  75. return Long.toHexString(randomLong);
  76. }
  77.  
  78. public void onMessage(Message message) {
  79. String messageText = null;
  80. try {
  81. if (message instanceof TextMessage) {
  82. TextMessage textMessage = (TextMessage) message;
  83. messageText = textMessage.getText();
  84. System.out.println("响应内容 = " + messageText);
  85. }
  86. } catch (JMSException e) {
  87. // Handle the exception appropriately
  88. }
  89. }
  90.  
  91. public static void main(String[] args) {
  92. new Client();
  93. }
  94. }
  1. package pfs.y2017.m11.mq.activemq.demo07;
  2.  
  3. public class MessageProtocol {
  4. public String handleProtocolMessage(String messageText) {
  5. String responseText;
  6. // 判断是否是client传过来的信息,在这里就可以做些解析
  7. if ("MyProtocolMessage".equalsIgnoreCase(messageText)) {
  8. responseText = "我收到了信息";
  9. } else {
  10. responseText = "我不知道你传的是什么: " + messageText;
  11. }
  12.  
  13. return responseText;
  14. }
  15. }
  1. package pfs.y2017.m11.mq.activemq.demo07;
  2.  
  3. import org.apache.activemq.broker.BrokerService;
  4. import org.apache.activemq.ActiveMQConnectionFactory;
  5.  
  6. import javax.jms.*;
  7.  
  8. public class Server implements MessageListener {
  9. private static int ackMode;
  10. private static String messageQueueName;
  11. private static String messageBrokerUrl;
  12.  
  13. private Session session;
  14. private boolean transacted = false;
  15. private MessageProducer replyProducer;
  16. private MessageProtocol messageProtocol;
  17.  
  18. static {
  19. messageBrokerUrl = "tcp://localhost:61616";
  20. messageQueueName = "client.messages";
  21. ackMode = Session.AUTO_ACKNOWLEDGE;
  22. }
  23.  
  24. public Server() {
  25. try {
  26. // This message broker is embedded
  27. BrokerService broker = new BrokerService();
  28. broker.setPersistent(false);
  29. broker.setUseJmx(false);
  30. broker.addConnector(messageBrokerUrl);
  31. broker.start();
  32. } catch (Exception e) {
  33. // Handle the exception appropriately
  34. }
  35.  
  36. // Delegating the handling of messages to another class, instantiate it before
  37. // setting up JMS so it
  38. // is ready to handle messages
  39. this.messageProtocol = new MessageProtocol();
  40. this.setupMessageQueueConsumer();
  41. }
  42.  
  43. private void setupMessageQueueConsumer() {
  44. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(messageBrokerUrl);
  45. Connection connection;
  46. try {
  47. connection = connectionFactory.createConnection();
  48. connection.start();
  49. this.session = connection.createSession(this.transacted, ackMode);
  50. Destination adminQueue = this.session.createQueue(messageQueueName);
  51.  
  52. // Setup a message producer to respond to messages from clients, we will get the
  53. // destination
  54. // to send to from the JMSReplyTo header field from a Message
  55. this.replyProducer = this.session.createProducer(null);
  56. this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  57.  
  58. // Set up a consumer to consume messages off of the admin queue
  59. MessageConsumer consumer = this.session.createConsumer(adminQueue);
  60. consumer.setMessageListener(this);
  61. } catch (JMSException e) {
  62. // Handle the exception appropriately
  63. }
  64. }
  65.  
  66. public void onMessage(Message message) {
  67. try {
  68. TextMessage response = this.session.createTextMessage();
  69. if (message instanceof TextMessage) {
  70. TextMessage txtMsg = (TextMessage) message;
  71. String messageText = txtMsg.getText();
  72. response.setText(this.messageProtocol.handleProtocolMessage(messageText));
  73. }
  74.  
  75. // Set the correlation ID from the received message to be the correlation id of
  76. // the response message
  77. // this lets the client identify which message this is a response to if it has
  78. // more than
  79. // one outstanding message to the server
  80. response.setJMSCorrelationID(message.getJMSCorrelationID());
  81.  
  82. // Send the response to the Destination specified by the JMSReplyTo field of the
  83. // received message,
  84. // this is presumably a temporary queue created by the client
  85. this.replyProducer.send(message.getJMSReplyTo(), response);
  86. } catch (JMSException e) {
  87. // Handle the exception appropriately
  88. }
  89. }
  90.  
  91. public static void main(String[] args) {
  92. new Server();
  93. }
  94. }

ActiveMQ(六) 转的更多相关文章

  1. 学习ActiveMQ(六):JMS消息的确认与重发机制

    当我们发送消息的时候,会出现发送失败的情况,此时我们需要用到activemq为我们提供了消息重发机制,进行消息的重新发送.那么我们怎么知道消息有没有发送失败呢?activemq还有消息确认机制,消费者 ...

  2. java高级工程师(一)

    一.无笔试题   不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试     二.三大框架方面问题   1.Spring 事务的隔离性,并说说每个隔离性的区别 ...

  3. 面试总结——Java高级工程师(一)

    一.无笔试题 不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试 二.三大框架方面问题 1.Spring 事务的隔离性,并说说每个隔离性的区别 解答:spri ...

  4. JMS学习(六)-ActiveMQ的高可用性实现

    原文地址:http://www.cnblogs.com/hapjin/p/5663024.html 一,ActiveMQ高可用性的架构 ActiveMQ的高可用性架构是基于Master/Slave 模 ...

  5. 消息中间件-activemq实战之消息持久化(六)

    对于activemq消息的持久化我们在第二节的时候就简单介绍过,今天我们详细的来分析一下activemq的持久化过程以及持久化插件.在生产环境中为确保消息的可靠性,我们肯定的面临持久化消息的问题,今天 ...

  6. JMS学习六(ActiveMQ消息传送模型)

    ActiveMQ 支持两种截然不同的消息传送模型:PTP(即点对点模型)和Pub/Sub(即发布 /订阅模型),分别称作:PTP Domain 和Pub/Sub Domain. 一.PTP消息传送模型 ...

  7. ActiveMQ 笔记(六)ActiveMQ的消息存储和持久化

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.持久化机制 1.Activemq持久化 1.1 什么是持久化: 持久化就是高可用的机制,即使服务器宕 ...

  8. ActiveMQ第六弹:设置多个并行的消费者

    消息队列本来就是一种经典的生产者与消费者模式.生产者向消息队列中发送消息,消费者从消息队列中获取消息来消费. 消息的传送一般由一个代理来实现的,那就是Message broker(即消息代理).Mes ...

  9. ActiveMQ消息队列的使用及应用

    这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录:  一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...

随机推荐

  1. ZipKin原理学习--zipkin支持日志打印追踪信息

       目前在zipkin brave已经提供功能在我们使用logback或log4j等时可以在日志信息中将traceId和spanId等信息打印到运行日志,这样可能对我们通过日志查看解决问题有比较大的 ...

  2. jQuary的相关动画效果

    第一种:该方法隐藏所有 <p> 元素: <html> <head> <script type="text/javascript" src= ...

  3. 【bzoj1878】[SDOI2009]HH的项链 - 树状数组 - 离线处理

    [SDOI2009]HH的项链 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4834  Solved: 2384[Submit][Status][Dis ...

  4. CSS选择器与XPath语言

    一 在爬取页面信息的过程中,需要到想要的信息进行定位,主要有两种方法.CSS选择器和XPath语言.查找某一个标签,两种方法都可以做到. 二 CSS选择器 http://www.w3school.co ...

  5. 【HDOJ6351】Beautiful Now(贪心,搜索)

    题意:给定一个数字n,最多可以交换其两个数位k次,求交换后的最大值与最小值,最小值不能有前导0 n,k<=1e9 思路: 当k>=n的位数时只需要无脑排序 k<n时有一个显然的贪心是 ...

  6. iscroll API

    概况 资料来源 http://cubiq.org/iscroll-4 http://www.cnblogs.com/wanghun/archive/2012/10/17/2727416.html ht ...

  7. oracle分区表备份恢复

    https://blog.csdn.net/jc_benben/article/details/51546815

  8. (47)C#运行时序列化

    序列化是将对象或对象图转化成字节流的过程.反序列化是将字节流转换回对象图的过程.

  9. Debian9初始配置

    1 进入root用户 su root 2 修改镜像源:编辑/etc/apt/sources.list文件 nano /etc/apt/sources.list 修改内容如下: deb http://m ...

  10. Hive入门及常用指令

    基础命令show databases; # 查看某个数据库use 数据库; # 进入某个数据库show tables; # 展示所有表desc 表名; # 显示表结构show partitions 表 ...