实战

代码如下:

信息生产者

  1. package test.mq.helloword;
  2.  
  3. import javax.jms.Connection;
  4. import javax.jms.ConnectionFactory;
  5. import javax.jms.Destination;
  6. import javax.jms.JMSException;
  7. import javax.jms.MessageProducer;
  8. import javax.jms.Session;
  9. import javax.jms.TextMessage;
  10.  
  11. import org.apache.activemq.ActiveMQConnectionFactory;
  12.  
  13. public class Sender {
  14. public static void main(String[] args) throws JMSException, InterruptedException {
  15. ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory(
  16. "tcp://localhost:61616"
  17. );
  18. Connection connection=ConnectionFactory.createConnection();
  19. connection.start();
  20. Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
  21. Destination destination=session.createQueue("my_queue");
  22. MessageProducer Producer=session.createProducer(destination);
  23.  
  24. for(int i=;i<=;i++){
  25. TextMessage message=session.createTextMessage("message----"+i);
  26. //Thread.sleep(1000);
  27. Producer.send(message);
  28. }
  29. session.commit();
  30. session.close();
  31. connection.close();
  32. }
  33. }

ActiveMQ页面

信息消费者

  1. package test.mq.helloword;
  2.  
  3. import javax.jms.Connection;
  4. import javax.jms.ConnectionFactory;
  5. import javax.jms.DeliveryMode;
  6. import javax.jms.Destination;
  7. import javax.jms.JMSException;
  8. import javax.jms.MessageConsumer;
  9. import javax.jms.MessageProducer;
  10. import javax.jms.Session;
  11. import javax.jms.TextMessage;
  12.  
  13. import org.apache.activemq.ActiveMQConnectionFactory;
  14. import org.apache.activemq.broker.ConsumerBrokerExchange;
  15.  
  16. public class Receiver {
  17.  
  18. public static void main(String[] args) throws JMSException {
  19. ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory(
  20. "tcp://localhost:61616"
  21. );
  22. Connection connection=ConnectionFactory.createConnection();
  23. connection.start();
  24. Session session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
  25. Destination destination=session.createQueue("my_queue");
  26. MessageConsumer Consumer=session.createConsumer(destination);
  27.  
  28. while(true){
  29. TextMessage msg=(TextMessage) Consumer.receive();
  30. msg.acknowledge();
  31. if(msg==null)break;
  32. System.out.println("接收信息:------》"+msg.getText());
  33. }
  34. if(connection!=null){
  35. connection.close();
  36. }
  37. }
  38.  
  39. }

控制台

ActiveMQ页面

分布式-信息方式-ActiveMQ示例的更多相关文章

  1. 分布式-信息方式-ActiveMQ的Destination高级特性3

    虚拟destination用来创建逻辑destination,客户端可以通过它来生产和消费消息,它会把消息映射到物理destination. ActiveMQ支持2种方式: 1:虚拟主题(Virtua ...

  2. 分布式-信息方式-ActiveMQ静态网络连接的容错

    容错的链接Failover Protocol 前面讲述的都是client配置链接到指定的 broker上.但是,如果 Broker的链接失败怎么办呢?此时, Client有两个选项:要么立刻死掉,要么 ...

  3. 分布式-信息方式-ActiveMQ的消息存储持久化

    ActiveMQ的消息存储持久化■概述ActiveMQ不仅支持 persistent和 non-persistent两种方式,还支持消息的恢复( recovery)方式PTPQueue的存储是很简单的 ...

  4. 分布式-信息方式-ActiveMQ的Message dispatch高级特性之(指针) Message cursors

    Message dispatch高级特性之 Message cursors概述            ActiveMQ发送持久消息的典型处现方式是:当消息的消费者准备就绪时,消息发送系统把存储的 消息 ...

  5. 分布式-信息方式-ActiveMQ的Destination高级特性2

    使用filtered destinations,在xml配置如下: <destinationInterceptors> <virtualDestinationInterceptor& ...

  6. 分布式-信息方式-ActiveMQ的Destination高级特性1

    ActiveMQ的Destination高级特性 Destination高级特性----->Composite Destinations 组合队列Composite Destinations : ...

  7. 分布式-信息方式-ActiveMQ的集群

    ActiveMQ的集群Queue consumer clusters              ActiveMQ支持 Consumer对消息高可靠性的负载平衡消费,如果一个 Consumer死掉,该消 ...

  8. 分布式-信息方式-ActiveMQ的动态网络链接

    ActiveMQ的动态网络链接多播协议 multicast ActiveMQ使用 Multicast协议将一个 Service和其他的 Broker的 Service连接起来,IPmulticast是 ...

  9. 分布式-信息方式-ActiveMQ静态网络连接信息回流功能

    “丢失”的消息 有这样的场景, broker1和 broker2通过 netwoskconnector连接,一些 consumers连接到 broker1,消费 broker2上的消息.消息先被 br ...

随机推荐

  1. MySQL两种内核对比

    MySQL内核 https://blog.csdn.net/baichoufei90/article/details/83504446 关键字:全文索引 索引外置 两种内核:MyISAM 和InnoD ...

  2. celery 分布式异步队列框架使用方法

    简介: Celery  是一个python开发的异步分布式任务调度模块,是一个消息传输的中间件,可以理解为一个邮箱,每当应用程序调用celery的异步任务时,会向broker传递消息,然后celery ...

  3. php enum 数字类型插入失败的解决办法

    今天遇到一个问题,我设置mysql字段 existence 为enum ,值为 ''0','1'. 当使用 php 进行查找 where existence = 1 的时候,并没有查询出什么数据. 后 ...

  4. 对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.

    ListDemo2.java ----------------- package com.fs.test; import java.util.ArrayList; import java.util.C ...

  5. 用SVM处理XSS时,数据清洗打标数据标准化处理的方法和意义

    def get_len(url): return len(url) def get_url_count(url): if re.search('(http://)|(https://)', url, ...

  6. 检查linux是否安装java、tomcat、mysql

    linux下,查看安装软件 1.linux下的java Java -version 如果出现java版本,证明java安装成功. 2.linux下的tomcat 2.1.检查linux是否安装tomc ...

  7. 数据库命令行工具USQL、mycli、litecli、pgcli

    USQL USQL 是一款使用 Go 语言开发的支持 SQL/NoSQL 数据库的通用命令行工具,它支持多种主流的数据库软件,目前最新版本是usql 0.7.0.比如 PostgreSQL.MySQL ...

  8. CDH5.16.1的Yarn提交任务默认资源分配

    1 同时运行5个Spark任务的资源分配截图 2 每个任务占用3个Container 3个core以及4.5GB内存 也就是说一个Container需要 1个core 以及 512MB的内存 如果资源 ...

  9. Qualcomm_Mobile_OpenCL.pdf 翻译-1

    1 前言 1.1 目的 这篇文档的主要目的是,向原始设备制造商(OEMs),独立软件供应商(ISVs),第三方开发者们,提供在基于高通骁龙400系列.600系列,和800系列的手机平台和芯片上进行开发 ...

  10. tensorflow 源码编译tensorflow 1.1.0到 tensorflow 2.0,ver:1.1.0rc1、1.4.0rc1、1.14.0-rc1、2.0.0b1

    目录 tensorflow-build table 更多详细过程信息及下载: tensorflow-build tensorflow 源码编译,提升硬件加速,支持cpu加速指令,suport SSE4 ...