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的关系有点类 ...
随机推荐
- 01_Nginx安装,nginx下部署项目,nginx.conf配置文件修改,相关文件配置
1.下载Nginx,进入Nginx下载地址:http://nginx.org/ 点击nginx-1.8.0,进入:http://nginx.org/en/download.html,下载文件: ...
- Android Activity的四种经典传值方法
文/ http://blog.csdn.net/sk719887916/article/details/41723613 skay 开发中遇到多个activity的传值问题 相邻两个之间的传值 或者 ...
- ActiveMQ系列之三:理解和掌握JMS
JMS是什么 JMS Java Message Service,Java消息服务,是Java EE中的一个技术. JMS规范 JMS定义了Java 中访问消息中间件的接口,并没有给予实现,实现JMS ...
- Oracle Global Finanicals Technical Reference(二)
Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...
- Android特效专辑(一)——水波纹过渡特效(首页)
Android特效专辑(一)--水波纹过渡特效(首页) 也是今天看到的一个特效,感觉挺漂亮的,最近也一直在筹划一个APP,就想把他当做APP的首页,然后加些处理,关于首页APP的特效等我完工了再贴出来 ...
- java--加强之 类加载器,动态代理
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9944561 ***************************************** ...
- SharePoint2010 -- ECMAScript客户端模型简单示例
ECMAScript客户端模型,是SharePoint2010推出的三种客户端模型".NET托管"."ECMAScript"."Sliverlight ...
- BCD码转十进制C语言实现
#include <stdio.h> #include <stdlib.h> #define uchar unsigned char uchar BCD_Decimal(uch ...
- 创建Sencha touch第一个应用
最近学习Sencha touch ,是一个菜鸟级别.废话不多说,让我们来创建Sencha touch的第一应用. 首先,我们下载Sencha touch2.0 sdk 和SDK工具. SDK工具直接 ...
- Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能
博客描述:后台返回地点的经纬度在地图上进行描点,点击导航弹出PopupWindow进行选择地图操作,如果手机中没有安装地图,提示没有,否则传值调起地图进行导航操作 看一下实现的效果,没图说再多都白搭 ...