Queue实现的是点到点模型,在以下的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue发送多个消息。
代码如下:
public class QueueTest {

/**
* @param args
*/
public static void main(String[] args) {
ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory("vm://localhost");
Connection con=null;
Session session=null;
Queue queue=null;
try {
con=factory.createConnection();
//这句很重要
con.start();
} catch (JMSException e) {
e.printStackTrace();
}
try {
session=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
System.out.println("--创建Session出现错误--");
}
queue=new ActiveMQQueue("testQueue");
//注册消费者1
try {
MessageConsumer consumer1=session.createConsumer(queue);
consumer1.setMessageListener(new MessageListener(){
public void onMessage(Message arg0) {
try {
System.out.println("消费者1接收到的消息 ---------"+((TextMessage)arg0).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (JMSException e) {
e.printStackTrace();}

//注册消费者2
try {
MessageConsumer consumer2=session.createConsumer(queue);
consumer2.setMessageListener(new MessageListener(){
public void onMessage(Message arg0) {
try {
System.out.println("消费者2接收到的消息---------"+((TextMessage)arg0).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (JMSException e) {
e.printStackTrace();
}

//注册一个消息生产者
try {
MessageProducer procedure=session.createProducer(queue);
for(int i=1;i<=10;i++)
{
procedure.send(session.createTextMessage("message"+i));
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
运行这个例子 可以看出:每个消息被消费了一次,但是如果有多个消费者共同监听一个Queue的话,无法确定一个消息最终会被那个消费者消费。

ActiveMQ之Queue的更多相关文章

  1. ActiveMQ的queue以及topic两种消息处理机制分析

    1    queue与topic的技术特点对比 对比项 Topic Queue 概要 Publish Subscribe messaging 发布订阅消息 Point-to-Point 点对点 有无状 ...

  2. SpringBoot整合ActiveMq实现Queue和Topic两种模式(看不懂你来打我)

    目录 一.前言 二.ActiveMq的下载和使用 三.依赖准备 四.yml文件配置 五.配置Bean 六.创建生产者(Queue+Topic) 七.创建消费者(Topic模式下) 八.测试结果(Top ...

  3. 【ActiveMQ入门-5】ActiveMQ学习-Queue与Topic的比较

    Queue与Topic的比较 1.JMS Queue执行load balancer语义: 一条消息仅能被一个consumer收到. 如果在message发送的时候没有可用的consumer,那么它将被 ...

  4. ActiveMQ Queue vs Topic vs VirtualTopic

    之前写过一篇文章讨论VirtualTopic,但觉得不够透彻,这里再根据实验结果进行一次横向对比破除模糊和选择困难症. 文章中核心对比要素是:消息副本和负载均衡 Queue的特点和优势 ActiveM ...

  5. ActiveMQ中的Destination高级特性(一)

    ---------------------------------------------------------------------------------------- Destination ...

  6. Activemq Jolokia

    打开JMX <broker … useJmx="true"> … <managementContext> <managementContext cre ...

  7. ActiveMQ;RabbitMQ;ZeroMQ

    中间件类型: Embedded middleware: As the name suggests, this typeof middleware handles embedded applicatio ...

  8. 消息队列-推/拉模式学习 & ActiveMQ及JMS学习

    一种分类是推和拉 . 还有一种分类是 Queue 和 Pub/Sub . 先看的这一篇:http://blog.csdn.net/heyutao007/article/details/50131089 ...

  9. JMX操作ActiveMQ(1)

    我们知道ActiveMQ broker的管理接口是通过JMX方式提供的. 一个简单的访问方式就是通过jconsole,输入 service:jmx:rmi:///jndi/rmi://localhos ...

随机推荐

  1. 基于Jquery Validate 的表单验证

    基于Jquery Validate 的表单验证 jquery.validate.js是jquery下的一个验证插件,运用此插件我们可以很便捷的对表单元素进行格式验证. 在讲述基于Jquery Vali ...

  2. SQL Server之内连接 左连接 右连接 全连接 交叉连接

    SELECT * FROM Table_A GO SELECT * FROM Table_B GO --内连接 SELECT a.*, b.* FROM Table_A a JOIN Table_B ...

  3. Linux常用命令之sed

    标题:sed命令的使用 作用:sed(stream editer)是以行为单位处理文本数据,可以对数据按行进行选取(显示打印).替换.删除和新增等功能. 工作流程:sed是一个流编辑器,它可以对从标准 ...

  4. Python中的字符串处理

    Python转义字符 在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.如下表: 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \' 单引号 \" 双引号 \a ...

  5. android 开源库集合的链接列表

    1.  内容挺多的 https://github.com/Trinea/android-open-project

  6. xcode6 真机运行报错 Command /usr/bin/codesign failed with exit code 1

    解决方法: 百度下载‘iphone配置实用工具’, 打开此软件之后,选择预配置描述文件, 选中iphone过期和重复的描述文件,按delete键删除.然后重启xcode即可

  7. SVN Server导项目到本地库时提示"方法OPTIONS失败与无法连接到服务器"

    方法 OPTIONS 失败于 “https://xxxx/svn/xxxx”: 无法连接到服务器 (https://xxxx) 要留意  https 使用了443 端口,检查防火墙是否开放了该端口. ...

  8. Android聊天界面刷新消息

    今天,我想来分享一下自己初用线程的感受,虽然写法略显粗糙,并没有用线程Thread中核心的Looper,MessageQueue消息队列这些知识,正因为是初学线程,所以就只用最基础的来写了,慢慢学习优 ...

  9. 找不到命名空间命名空间:System.Windows.Forms

    System.Windows.Forms在system.windows.forms.dll中.需要添加引用.在解决方案资源管理器中的引用上单击右键,选择添加引用.找到System.windows.fo ...

  10. 读书笔记-UIView与控件

    1.UIView 在Objective-C中,NSObject是所有类的“根”类.同样,在UIKit框架中,也存在一个如此神奇的类UIView.从继承关系上看,UIView是所有视图的根. 1.1.U ...