一.订阅/发布模式

1.生产者

/**
* 消息生产者
*
*/
public class JMSProducer { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;
private static final int SENDNUM=10; public static void main(String[] args) { ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageProducer messageProducer; connectionFactory=new ActiveMQConnectionFactory
(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL); try {
connection=connectionFactory.createConnection();
connection.start();
session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
destination=session.createTopic("FirstTopic1"); //创建topic,为目的地
messageProducer=session.createProducer(destination); //创建消息生产者
sendMessage(session, messageProducer); //发送消息
session.commit(); //提交session
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} /**
*发送消息
*/
public static void sendMessage(Session session,MessageProducer messageProducer)throws Exception{
for(int i=0;i<JMSProducer.SENDNUM;i++){
TextMessage message=session.createTextMessage("ActiveMQ 发送消息"+i);
System.out.println("发送消息:"+"ActiveMQ 发送的消息:"+i);
messageProducer.send(message);
}
}
}

2.消息消费者

/**
* 消息消费者
*
*/
public class JMSConsumer2 { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer messageConsumer; connectionFactory=new ActiveMQConnectionFactory
(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL); try {
connection=connectionFactory.createConnection();
connection.start();
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination=session.createTopic("FirstTopic1"); //指定消费的topic
messageConsumer=session.createConsumer(destination); //创建消息消费者
messageConsumer.setMessageListener(new Listener2()); //注册监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

3.监听器

/**
* 监听器
*/
public class Listener implements MessageListener{ @Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("监听器:"+((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

分别运行生产者,消费者代码,观察控制台

Apache ActiveMQ 实践 <二>的更多相关文章

  1. Apache ActiveMQ 实践 <一>

    一.下载最新版本 ActiveMq http://activemq.apache.org/activemq-5152-release.html,下载目录如下: 二.创建项目 1.普通项目 添加 jar ...

  2. 消息队列MQ - Apache ActiveMQ

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Jave程式,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行. 1.que ...

  3. Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移

    本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0  1.对信息的传输/ ...

  4. apache activemq的重连

    1.activemq的重连机制 maxReconnectAttempts -1 | 0 From version 5.6 onwards: -1 is default and means retry ...

  5. apache activemq 学习笔记

    0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...

  6. How to Setup Replicated LevelDB Persistence in Apache ActiveMQ 5.9--转载

    原文地址:https://simplesassim.wordpress.com/2013/11/03/how-to-setup-replicated-leveldb-persistence-in-ap ...

  7. Apache ActiveMQ消息中间件的基本使用

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行. 支持Jav ...

  8. Apache ActiveMQ实战(2)-集群

    ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这些问题--集群的两种方式: Master slave Broker clusters ActiveMQ的集群有两种方式: ...

  9. Apache ActiveMQ实战(1)-基本安装配置与消息类型

    ActiveMQ简介 ActiveMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信.ActiveMQ使用Apache ...

随机推荐

  1. 牛客假日团队赛1 B

    B.便便传送门(一) 题目链接:https://ac.nowcoder.com/acm/contest/918/B 题目 Farmer John最讨厌的农活是运输牛粪.为了精简这个过程,他制造了一个伟 ...

  2. 微服务网关 Spring Cloud Gateway

    1.  为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...

  3. 8天入门docker系列 —— 第七天 让你的container实现跨主机访问

    当你有若干个容器之后,你可能就希望实现容器的跨机部署访问了,比如aspnetcore在一台host上,mysql在另外一个host上,如果要实现这样的功能,需要你借助 docker自带的overlay ...

  4. iOS中 分类(category)与扩展(Extension)的区别?

    1.分类(category)的作用 (1).作用:可以在不修改原来类的基础上,为一个类扩展方法.(2).最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? (1).分类中只能添加“方法”,不 ...

  5. 关于char[]转换成LPCWSTR的有关问题[转]

    一.问题的原因:VS2010默认采用宽字符UNICODE编码方式,定义了Unicode,因此相关的字符串必须为unicode字符串,而非ascii字符串. LPCWSTR中的W是宽字符的意思,是UNI ...

  6. HDU 6181:Two Paths(A* + SPFA)

    题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...

  7. Python旅途——函数的递归和栈的使用

    Python--函数之递归.栈的使用 今天主要和大家分享函数的递归,同时引入一个新的概念--栈 1.递归 1.定义 函数的递归指的就是函数自己调用自己,什么是函数自己调用自己呢?我们来看一个栗子: 这 ...

  8. MacOS使用GitBook制作电子书

    目录 目录 一.简介 二.安装 1. 安装node.js 2. 安装gitbook 三.使用 四.常用命令 1. 初始化 或 编辑目录 2. 编辑内容之后编译书籍 3. 启动web服务通过浏览器预览数 ...

  9. Socket网络编程系列教程序

    C语言的用途相当多,可以用在数据结构.数据库.网络.嵌入式等方面,历经40多年不衰,真是厉害!最近一直想从某一应用方面写一个系列教程,好好地把某一方面讲深讲透.         正好博主对网络方面的编 ...

  10. py+selenium 自动判断页面是否报错并显示在自动化测试报告【原创】

    有需求就会去研究解决的路子. 现在需求就是,测试报告报错信息一堆,但却无法肉眼看出是什么问题,你只能知道定位不到元素或是超时,但你却不知道其实进入页面就报错了或是提交表单就报错了!也就是看到报错,需要 ...