Apache ActiveMQ is the most popular and powerful open source messaging and Integration Patterns server.
Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License
一、ActiveMQ下载安装:
下载版本:ActiveMQ 5.14.5 Release(Windows )
(目前最新版本ActiveMQ 5.15.0 Release.The minimum Java version has been upgraded to Java 8.)
解压目录说明:bin存放的是脚本文件
conf存放的是基本配置文件
data存放的是日志文件
docs存放的是说明文档
examples存放的是简单的实例
lib存放的是activemq所需jar包
webapps用于存放项目的目录
二、ActiveMQ启动和终止
启动命令(CMD):E:\apache-activemq-5.14.5\bin>activemq startActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。
http://127.0.0.1:8161/admin/
默认账号:admin/adminActiveMQ默认使用的TCP连接端口是61616, 通过查看该端口的信息可以测试ActiveMQ是否成功启动
netstat -an|find “61616”
停止服务器,只需要按着Ctrl+Shift+C,之后输入y即可。
(linux 下输入 ./activemq start 启动activeMQ 服务, 终止命令是 ./activemq stop)
三、 ActiveMQ特性Supports a variety of Cross Language Clients and Protocols from Java, C, C++, C#, Ruby, Perl, Python, PHPOpenWire for high performance clients in Java, C, C++, C#
Stomp support so that clients can be written easily in C, Ruby, Perl, Python, PHP, ActionScript/Flash, Smalltalk to talk to ActiveMQ as well as any other popular Message Broker
AMQP v1.0 support
MQTT v3.1 support allowing for connections in an IoT environment.
full support for the Enterprise Integration Patterns both in the JMS client and the Message Broker
Supports many advanced features such as Message Groups, Virtual Destinations, Wildcards and Composite Destinations
Fully supports JMS 1.1 and J2EE 1.4 with support for transient, persistent, transactional and XA messaging
Spring Support so that ActiveMQ can be easily embedded into Spring applications and configured using Spring's XML configuration mechanism
Tested inside popular J2EE servers such as TomEE, Geronimo, JBoss, GlassFish and WebLogicIncludes JCA 1.5 resource adaptors for inbound & outbound messaging so that ActiveMQ should auto-deploy in any J2EE 1.4 compliant server
Supports pluggable transport protocols such as in-VM, TCP, SSL, NIO, UDP, multicast, JGroups and JXTA transports
Supports very fast persistence using JDBC along with a high performance journal
Designed for high performance clustering, client-server, peer based communication
REST API to provide technology agnostic and language neutral web based API to messaging
Ajax to support web streaming support to web browsers using pure DHTML, allowing web browsers to be part of the messaging fabric
CXF and Axis Support so that ActiveMQ can be easily dropped into either of these web service stacks to provide reliable messaging
Can be used as an in memory JMS provider, ideal for unit testing JMS
四、ActiveMQ应用场景
多个项目之间集成
(1) 跨平台
(2) 多语言
(3) 多项目
降低系统间模块的耦合度,解耦
(1) 软件扩展性
系统前后端隔离
(1) 前后端隔离,屏蔽高安全区
五、ActiveMQ应用实例HelloWorldMaven依赖引入
org.apache.activemq
activemq-all
5.14.5
编写生产者package com.mq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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 JMSProducer
{
// 默认连接用户名
private static final StringUSERNAME= ActiveMQConnection.DEFAULT_USER;
// 默认连接密码
private static final StringPASSWORD= ActiveMQConnection.DEFAULT_PASSWORD;
// 默认连接地址
private static final StringBROKEURL= ActiveMQConnection.DEFAULT_BROKER_URL;
// 发送的消息数量
private static final intSENDNUM= 10;
public static void main(String[] args)
{
// 连接工厂
ConnectionFactory connectionFactory = null;
// 连接
Connection connection = null;
// 会话 接收或发送消息的线程
Session session = null;
// 消息的目的地
Destination destination = null;
// 消息生产者
MessageProducer messageProducer = null;
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
try
{
// 通过连接工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建一个名称为HelloWorld的消息队列
destination=session.createQueue("HelloWorld");
//创建消息生产者
messageProducer=session.createProducer(destination);
//不持久化消息,重启消息丢失。默认持久化消息。
//messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, messageProducer);
}
catch (JMSException e)
{
e.printStackTrace();
}
finally
{
if(connection!=null)
{
try
{
connection.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
}
private static void sendMessage(Session session, MessageProducer messageProducer) throws JMSException
{
for(int i=0;i
{
TextMessage message=session.createTextMessage("{\"name\":\"ActiveMQ消息\",\"num\":"+i+"}");
messageProducer.send(message);
System.out.println("发送消息条数:"+i);
}
try
{
session.commit();
//Thread.sleep(1000*5);
//System.out.println("休息5s结束,继续发发送。");
//sendMessage(session, messageProducer);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}编写消费者import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSConsumer
{
private static final StringUSERNAME= ActiveMQConnection.DEFAULT_USER;// 默认连接用户名
private static final StringPASSWORD= ActiveMQConnection.DEFAULT_PASSWORD;// 默认连接密码
private static final StringBROKEURL= ActiveMQConnection.DEFAULT_BROKER_URL;// 默认连接地址
public static void main(String[] args)
{
// 连接工厂
ConnectionFactory connectionFactory = null;
// 连接
Connection connection = null;
// 会话 接收或发送消息的线程
Session session = null;
// 消息的目的地
Destination destination = null;
// 消息的消费者
MessageConsumer messageConsumer;
// 实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
try
{
// 通过连接工厂获取连接
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建一个连接HelloWorld的消息队列
destination = session.createQueue("HelloWorld");
// 创建消息消费者
messageConsumer = session.createConsumer(destination);
while (true)
{
TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);
if (textMessage != null)
{
System.out.println(new Date()+"收到的消息:" + textMessage.getText());
}
else
{
break;
}
}
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}