实战

代码如下:

信息生产者

package test.mq.helloword;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender {
public static void main(String[] args) throws JMSException, InterruptedException {
ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory(
"tcp://localhost:61616"
);
Connection connection=ConnectionFactory.createConnection();
connection.start();
Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue("my_queue");
MessageProducer Producer=session.createProducer(destination); for(int i=;i<=;i++){
TextMessage message=session.createTextMessage("message----"+i);
//Thread.sleep(1000);
Producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}

ActiveMQ页面

信息消费者

package test.mq.helloword;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
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.broker.ConsumerBrokerExchange; public class Receiver { public static void main(String[] args) throws JMSException {
ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory(
"tcp://localhost:61616"
);
Connection connection=ConnectionFactory.createConnection();
connection.start();
Session session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination=session.createQueue("my_queue");
MessageConsumer Consumer=session.createConsumer(destination); while(true){
TextMessage msg=(TextMessage) Consumer.receive();
msg.acknowledge();
if(msg==null)break;
System.out.println("接收信息:------》"+msg.getText());
}
if(connection!=null){
connection.close();
}
} }

控制台

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. Postgresql 大小版本升级

    文章结构如下: Postgresql是一个非常活跃的社区开源项目,更新速度很快,每一次版本的更新都会积极的修复旧版本的BUG,性能上也会有不同幅度的提升.10之前的版本由三部分组成,10开始只有两部分 ...

  2. Jquery table相关--工时系统

    1.jquery 的弹出对话框,单击事件之后 if (confirm("确定要删除?")) { // //点击确定后操作 } 2.对某个table中的checkbox是否被选中的遍 ...

  3. Auto-increment 自动增长

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  4. vue运行碰到的问题

    Expected indentation of 0 spaces but found 2 解决方案: 文件中加入"indent": ["off", 2]就可以了 ...

  5. Bat批处理文件入门

    这个东西吧,感觉在使用windows时作用不是很大,毕竟在windows环境下命令行确实用的比较少,但有时候也会用到,比如测试算法程序时使用批处理+文件可以省去每次手动输入.赶巧最近对批处理也比较感兴 ...

  6. 多Y轴示例

    //多Y轴示例 <template> <div id="main" :style="{width:'1000px',height:'500px' }&q ...

  7. LVS DR模型RS端修改配置脚本

    #!/bin/bash vip=x.x.x.x in start) > /proc/sys/net/ipv4/conf/all/arp_ignore > /proc/sys/net/ipv ...

  8. Delphi 鼠标的编程

  9. MySQL快速清空表数据

    truncate table 可以不删除表的情况下,快速情况表数据

  10. Mac OSX编译安装php5.6

    安装好OSX 10.13以后默认自带的php7.1.7,跟现有环境不兼容,所以准备编译安装php5.6,自带的php7不建议卸载,重新安装一份php5.6 1.安装php的一些依赖,推荐使用brew安 ...