ActiveMQ的使用
- 创建连接工厂
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
- 获取连接
connection = mqf.createConnection();
- 生成会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- 生成对应的topic
Destination destination = session.createTopic("mytopic");
- 创建生产者
MessageProducer producer = session.createProducer(destination);
- 设置发送消息使用的模式
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- 生成消息
TextMessage msg = session.createTextMessage(“message");
- 启动连接
connection.start();
- 发送消息
producer.send(msg);
- 关闭生产者
producer.close();
- 关闭会话
session.close();
- 关闭连接
connection.close();
- 继承接口
- 创建连接工厂
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
- 获取连接
Connection connection = mqf.createConnection();
- 生成会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- 生成对应的topic
Destination destination = session.createTopic("mytopic”);
- 创建消费者
MessageConsumer consumer = session.createConsumer(destination);
- 启动连接
connection.start();
- 设置消息监听
consumer.setMessageListener(this);
- 设置异常监听
connection.setExceptionListener(this);
- 实现onMessage方法
TextMessage tm = (TextMessage)message;
String result = tm.getText();
- 关闭消费者
consumer.close();
- 关闭会话
session.close();
- 关闭连接
connection.close();
- 生产者实现程序
package activemq_test; import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Producer_tool { private final static String userName = ActiveMQConnection.DEFAULT_USER;
private final static String password = ActiveMQConnection.DEFAULT_PASSWORD;
private final static String brokerURL = "tcp://192.168.0.5:61616";
private MessageProducer producer = null;
private Connection connection = null;
private Session session = null; public void initialize() throws JMSException {
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
connection = mqf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("mytopic");
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} public void send(String message) throws JMSException {
initialize();
TextMessage msg = session.createTextMessage(message);
System.out.println("sending message: " + message);
connection.start();
producer.send(msg);
} public void close() throws JMSException {
if(producer != null) {
producer.close();
}
if(session != null) {
session.close();
}
if(connection != null) {
connection.close();
}
System.out.println("closed");
} }
- 生产者主程序
package activemq_test;
import javax.jms.JMSException;
public class Producer_test {
public static void main(String[] args) throws JMSException {
Producer_tool producer = null;
for(int i = 0; i < 10; i++) {
producer = new Producer_tool();
producer.send("message" + i);
producer.close();
}
}
}
- 消费者实现程序
package activemq_test; import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; public class Consumer_tool implements MessageListener,ExceptionListener{ private final static String userName = ActiveMQConnection.DEFAULT_USER;
private final static String password = ActiveMQConnection.DEFAULT_PASSWORD;
private final static String brokerURL = "tcp://192.168.0.5:61616";
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
static boolean isConnection = false; public void initialize() throws JMSException {
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
connection = mqf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("mytopic");
consumer = session.createConsumer(destination);
} public void consumeMessage() throws JMSException {
initialize();
connection.start();
consumer.setMessageListener(this);
connection.setExceptionListener(this);
isConnection = true;
System.out.println("consumer is listening"); } @Override
public void onException(JMSException exception) {
isConnection = false;
} @Override
public void onMessage(Message message) {
if(message instanceof TextMessage) {
TextMessage tm = (TextMessage)message;
try {
System.out.println("consumer received " + tm.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
else {
System.out.println(message);
}
} public void close() throws JMSException {
if(consumer != null) {
consumer.close();
}
if(session != null) {
session.close();
}
if(connection != null) {
connection.close();
}
System.out.println("consumer has closed");
}
}
- 消费者主程序
package activemq_test;
import javax.jms.JMSException;
public class Consumer_test {
public static void main(String[] args) throws JMSException {
Consumer_tool consumer = new Consumer_tool();
consumer.consumeMessage();
while(Consumer_tool.isConnection) { }
consumer.close();
}
}
ActiveMQ的使用的更多相关文章
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- (jms)ActiveMQ 安装配置.
前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...
- node(ActiveMq)
简单尝试了node下的ActiveMQ 1.下载apache-activemq-5.9.0,执行bat文件: 2.登录http://localhost:8161/admin可查看其管理后台: 3.安装 ...
- ActiveMQ的集群方案对比及部署
转载:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- JMS学习之路(一):整合activeMQ到SpringMVC
JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...
- ActiveMQ消息队列的使用及应用
这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录: 一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- Spring下ActiveMQ实战
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...
- ActiveMQ(li)
一.ActiveMQ 首先,ActiveMQ不是一个框架,它不是struct,webx,netty这种框架,它更像是tomcat服务器,因为你使用它之前必须启动它,activeMQ和JMS的关系有点类 ...
随机推荐
- ADF BC New Features
Examining ADF Business Components New Features Purpose In this tutorial, you create a series of si ...
- 学习C++模板,类模板
当我们使用向量时,会经常使用形如:vector<int> a的式子.这个表达式就是一个类模板实例化的例子,vector是一个类模板,我们给他传递模板参数(见<>里),然后创建一 ...
- 携程Android App的插件化和动态加载框架
携程Android App的插件化和动态加载框架已上线半年,经历了初期的探索和持续的打磨优化,新框架和工程配置经受住了生产实践的考验.本文将详细介绍Android平台插件式开发和动态加载技术的原理和实 ...
- CMake命令行添加编译参数
CMake命令行添加编译参数 学习自 coroserver 例程: https://github.com/windoze/coroserver coroserver 是一个应用 Boost.Asio ...
- redis持久化AOF与RDB
RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原 ...
- leetCode之旅(12)-反转二叉树
背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...
- 0502-其他html标签
其他html标签 一.框架 1.<frameset><frame/><frameset> 注意:html文档中不能有<body></body> ...
- Python Selenium之异常处理
自动化测试执行过程中,难免会有错误/异常出现,比如测试脚本没有发现对应元素,则会立刻抛出NoSuchElementException异常.这时不要怕,肯定是测试脚本或者测试环境哪里出错了!那如何处理才 ...
- 学习ASP.NET Core Razor 编程系列八——并发处理
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 多重影分身——C#中多线程的使用二(争抢共享资源)
只要服务器承受得了,我们可以开任意个线程同时工作以提高效率,然而 两个线程争抢资源可能导致数据混乱. 例如: public class MyFood { public static int Last ...