ActiveMQ使用分为两大块:生产者和消费者
一、准备
项目导入jar包:activemq-all-5.15.3.jar
并buildpath 
 
二、生产者
  1. 创建连接工厂
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
注:
userName是ActiveMQ的用户名,默认可以通过:ActiveMQConnection.DEFAULT_USER
password是ActiveMQ的密码,默认可以通过: ActiveMQConnection.DEFAULT_PASSWORD
brokerURL是ActiveMQ的连接,指定格式为:tcp://主机名:61616
 
  1. 获取连接
connection = mqf.createConnection();
 
  1. 生成会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
  1. 生成对应的topic
Destination destination = session.createTopic("mytopic");
 
  1. 创建生产者
MessageProducer producer = session.createProducer(destination);
 
  1. 设置发送消息使用的模式
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
默认是:DeliveryMode.PERSISTENT
 
  1. 生成消息
TextMessage msg = session.createTextMessage(“message");
 
  1. 启动连接
connection.start();
 
  1. 发送消息
producer.send(msg);
 
  1. 关闭生产者
producer.close();
 
  1. 关闭会话
session.close();
 
  1. 关闭连接
connection.close();
 
三、消费者
  1. 继承接口
MessageListener
ExceptionListener
并实现onException(JMSException exception)和onMessage(Message message)方法
 
  1. 创建连接工厂
ActiveMQConnectionFactory mqf = new ActiveMQConnectionFactory(userName, password, brokerURL);
具体参数同上
 
  1. 获取连接
Connection connection = mqf.createConnection();
 
  1. 生成会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
  1. 生成对应的topic
Destination destination = session.createTopic("mytopic”);
 
  1. 创建消费者
MessageConsumer consumer = session.createConsumer(destination);
 
  1. 启动连接
connection.start();
 
  1. 设置消息监听
consumer.setMessageListener(this);
 
  1. 设置异常监听
connection.setExceptionListener(this);
 
  1. 实现onMessage方法
改方法有一个参数Message message,这个参数是从ActiveMQ上拿到的消息,可以通过如下方法解析出来:
TextMessage tm = (TextMessage)message;
String result = tm.getText();
 
  1. 关闭消费者
consumer.close();
 
  1. 关闭会话
session.close();
 
  1. 关闭连接
connection.close();
 
四、例程
  1. 生产者实现程序
 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");
} }
  1. 生产者主程序
 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();
}
}
}
 
  1. 消费者实现程序
 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");
}
}
 
  1. 消费者主程序
 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的使用的更多相关文章

  1. Java消息队列--ActiveMq 实战

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  2. 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)

    Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...

  3. (jms)ActiveMQ 安装配置.

    前言 ActiveMQ他是Apache出品的一个JMS提供者,管理会话和队列,运行在JVM下,支持多种语言,如JAVA,C++,C#,应用协议: OpenWire,Stomp REST,WS Noti ...

  4. node(ActiveMq)

    简单尝试了node下的ActiveMQ 1.下载apache-activemq-5.9.0,执行bat文件: 2.登录http://localhost:8161/admin可查看其管理后台: 3.安装 ...

  5. ActiveMQ的集群方案对比及部署

    转载:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...

  6. JMS学习之路(一):整合activeMQ到SpringMVC

    JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...

  7. ActiveMQ消息队列的使用及应用

    这里就不说怎么安装了,直接解压出来就行了. 谢绝转载,作者保留所有权力 目录:  一:JMQ的两种消息模式 1.1:点对点的消息模式 1.2:订阅模式 二:点对点的实现代码 2.1:点对点的发送端 2 ...

  8. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  9. Spring下ActiveMQ实战

    MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...

  10. ActiveMQ(li)

    一.ActiveMQ 首先,ActiveMQ不是一个框架,它不是struct,webx,netty这种框架,它更像是tomcat服务器,因为你使用它之前必须启动它,activeMQ和JMS的关系有点类 ...

随机推荐

  1. 01_Nginx安装,nginx下部署项目,nginx.conf配置文件修改,相关文件配置

     1.下载Nginx,进入Nginx下载地址:http://nginx.org/ 点击nginx-1.8.0,进入:http://nginx.org/en/download.html,下载文件: ...

  2. Android Activity的四种经典传值方法

    文/ http://blog.csdn.net/sk719887916/article/details/41723613  skay 开发中遇到多个activity的传值问题 相邻两个之间的传值 或者 ...

  3. ActiveMQ系列之三:理解和掌握JMS

    JMS是什么 JMS Java Message Service,Java消息服务,是Java EE中的一个技术. JMS规范 JMS定义了Java 中访问消息中间件的接口,并没有给予实现,实现JMS  ...

  4. Oracle Global Finanicals Technical Reference(二)

    Skip Headers Oracle Global Finanicals Oracle Global Financials Technical Reference Manual Release 11 ...

  5. Android特效专辑(一)——水波纹过渡特效(首页)

    Android特效专辑(一)--水波纹过渡特效(首页) 也是今天看到的一个特效,感觉挺漂亮的,最近也一直在筹划一个APP,就想把他当做APP的首页,然后加些处理,关于首页APP的特效等我完工了再贴出来 ...

  6. java--加强之 类加载器,动态代理

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9944561 ***************************************** ...

  7. SharePoint2010 -- ECMAScript客户端模型简单示例

    ECMAScript客户端模型,是SharePoint2010推出的三种客户端模型".NET托管"."ECMAScript"."Sliverlight ...

  8. BCD码转十进制C语言实现

    #include <stdio.h> #include <stdlib.h> #define uchar unsigned char uchar BCD_Decimal(uch ...

  9. 创建Sencha touch第一个应用

    最近学习Sencha touch ,是一个菜鸟级别.废话不多说,让我们来创建Sencha touch的第一应用. 首先,我们下载Sencha touch2.0 sdk 和SDK工具.  SDK工具直接 ...

  10. Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能

    博客描述:后台返回地点的经纬度在地图上进行描点,点击导航弹出PopupWindow进行选择地图操作,如果手机中没有安装地图,提示没有,否则传值调起地图进行导航操作 看一下实现的效果,没图说再多都白搭 ...