1. ActiveMQ简述
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现。
下载地址:http://activemq.apache.org/
2. 运行ActiveMQ
下载好压缩包后,解压,直接运行bin/win64(或32,据系统而定)下的activemq.bat即可,程序开启后,可通过浏览器访问http://localhost:8161/admin进入管理界面。如需帐号密码,请输入admin和admin。
3. 创建项目和测试
主要有生产者(对应Sender)和消费者(对应Receiver),然后使用多线程进行测试。其中的消息队列对通过生产者:destination = session.createQueue(“TestFirst”)
自动在ActiveMQ中创建。
下面是详细的代码(创建工程后请将下载的ActiveMQ中lib文件夹里面的jar包添加进去):
Receiver.java
public class Receiver {
public void listen() {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("TestFirst");
consumer = session.createConsumer(destination);
while (true) {
Thread.sleep(20);
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.err.println("收到消息:" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
Sender.java
public class Sender {
private static final int SEND_NUMBER = 5;
private int userId;
public Sender(int userId) {
this.userId = userId;
}
public void send() {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// TextMessage message;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("TestFirst");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
String msg = "用户" + userId + ":内容" + i;
TextMessage message = session
.createTextMessage(msg + i);
// 发送消息到目的地方
System.out.println("发送消息:" + msg);
producer.send(message);
}
}
}
MyThread.java
public class MyThread extends Thread {
private boolean flag;
private int i = 0;
MyThread(){
flag = true;
}
MyThread(int i){
flag = false;
this.i = i;
}
public void run() {
int i = 1000;
if(flag)
new Receiver().listen();
else
while(--i>0)
new Sender(this.i).send();
}
public static void main(String[] args){
//一个消费者
new MyThread().start();
//多个生产者
int i = 80;
while(i-->0)
new MyThread(i).start();
}
}
本文说的并不详细,后期会补上。
先给几个连接:
ActiveMQ入门实例:
http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html
作者:xwdreamerActiveMQ初体验:
http://www.cnblogs.com/diorlv/p/3328712.html
作者:diorlvAndroid中应用ActiveMQ(推送相关):
http://blog.youkuaiyun.com/junfeng120125/article/details/36420083
作者:永不放弃的IT码农