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的关系有点类 ...
随机推荐
- Android LruCache技术原理
概述 记得在很早之前,我有写过一篇文章Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache技 ...
- web报表工具FineReport最经常用到部分函数详解
之前分别列出来了finereport常用的文本.时间函数的解释,这里应广大朋友的要求,整理了finereport最常用到的一些函数! SUM SUM(number1,number2,-):求一个指定单 ...
- leetcode之旅(6)-Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- ASP.NET Core 2.0 使用NLog实现日志记录
1.安装NuGet包 1.Install-Package NLog.Web.AspNetCore 2.Install-Package NLog 在csproj中编辑: <PackageRefer ...
- ES6中Promise详解
Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise 是一个对象,从它可以获取异步操作的消息. Promise 提供统一的 AP ...
- python---01.名片管理系统
这是第一篇文章,也是完整编写的第一份代码,,,,希望大神们多多指导,提出更好的想法. 第一部分-----提供选项的菜单栏 第二部分:根据用户输入的选择,提供功能 总体需要一个while True: 其 ...
- 浅析fork()和底层实现
记得以前初次接触fork()函数的时候,一直被“printf”输出多少次的问题弄得比较晕乎.不过,“黄天不负留心人".哈~ 终于在学习进程和进程创建fork相关知识后,总算是大致摸清了其中的 ...
- 手把手教你全家桶之React(二)
前言 上一篇已经讲了一些react的基本配置,本遍接着讲热更新以及react+redux的配置与使用. 热更新 我们在实际开发时,都有用到热更新,在修改代码后,不用每次都重启服务,而是自动更新.并而不 ...
- JAVA中Sql时间格式与util时间格式转换
关于时间格式转化: java.util.Date 与 java.sql.Date 互换 sql是子类 字符串转化成java.util.Date SimpleDateFormat date =n ...
- java运行机制、Jdk版本及Java环境变量
一.语言特性 计算机高级语言按程序的执行方式可分为:编译型和解释型两种.编译型的语言是指使用专门的编译器,针对特定的平台(操作系统)一次性翻译成被该平台硬件执行的机器码,并包装成该平台可执行性程序文件 ...