实战

代码如下:

信息生产者

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. jQuery扁平化风格手风琴菜单

    在线演示 本地下载

  2. python基础之函数进阶

    假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print(bi ...

  3. python网络爬虫(4)结构与基本概念

    基本模型 请求与响应 import urllib.request as urllib2 request=urllib2.Request('http://www.zhihu.com') response ...

  4. shiro配置学习

    一.shiro的配置 1.shiro的web过滤 实例化ShiroFilterFactoryBean 设置securityManager.loginUrl.unauthorizedUrl.sucess ...

  5. openlayers之全屏控件的使用

    import { FullScreen } from 'ol/control' map.addControl(new FullScreen())

  6. Nginx做代理路由时,不转发http的header问题

    从header里面拿不到 TX_XID这种类型的字段, Nginx 会默认忽略含有 “_” 的 header 参数,而 TX_XID值的参数名恰好含有 “_” 符号,需要手动开启转发. 在 nginx ...

  7. mysql统计表中条目个数的方法举例

    说明:以下标红且加大括号的均需要替换为实际待查询的表名或数据库名. [1].统计某张或某几张表的数据量: select count(*) from {TABLE_NAME}; #or select c ...

  8. Delphi 处理异常情况

  9. 多态(Polymorphism)的实现机制

    1. 我理解的广义的 override 是指抛开各种访问权限,子类重定义(redefine)父类的函数(即函数签名相同). 2. C++中的三个所谓的原则:never redefine base cl ...

  10. pidstat 命令(Linux 进程使用资源情况采样)

    pidstat 作用 pidstat 获取服务器指定进程的使用资源信息(包括 CPU.设备IO.内存.线程.任务切换等). 执行一波 [root@wille ~]# pidstat Linux 2.6 ...