1.Linux上安装activemq
http://activemq.apache.org/activemq-5120-release.html
下载activemq,目前最新版本是5.12.0,上传至Linux服务器
a.新建目录 /server
cd /
mkdir server
上传安装包/server
解压安装包
tar zxvf apache-activemq-5.12.0-bin.tar.gz
得到安装目录apache-activemq-5.12.0
cd apache-activemq-5.12.0
文件结构如下图:
bin目录负责启动,停止,重启mq,里面有activemq,需要先给予授权chmod 777 activemq,然后用./activemq start/stop/restart启停mq。
conf目录是mq的配置文件,主要的有activemq.xml和jetty.xml
data目录是日志文件,mq启动,运行如果报错就会在此目录下生成activemq.log日志文件,可以通过查看该文件确定错误的问题
lib目录存放activemq的依赖包,如果需要配置消息持久化到数据,则需要在此目录下添加相应的数据库驱动包,还应将数据源的依赖包添加进去。
接下来就是配置activemq.xml文件了,按照默认的文件配置,很多不需要修改,这里只说一下,配置持久化到数据的方法
首先找到
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/-->
<jdbcPersistenceAdapter dataSource="#mysql-ds" useDatabaseLock="false"/>
</persistenceAdapter>
将默认的配置注释,改为红色字体部分配置,useDatabaseLock 此属性设置为true,是为数据库加锁,在主备集群方式中需要将其设置为true,表示当某个mq作为master时,即取得数据库的锁,全部消息经由它进行持久化,其他slave不能操作数据库,这样可以避免主键重复的错误,当master宕机后,优先取得数据库锁的slave将成为master。
配置好持久化适配器后,还需要配置数据bean
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mysql?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="poolPreparedStatements" value="true"/>
</bean>
这样持久化就配置完成。如果是同一台机器搭建多个activemq集群,则需要保证每个mq所用到的端口不发生冲突
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="nio" uri="nio://0.0.0.0:61618?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
activemq内置的服务器jetty的配置也是同理,特别要注意端口冲突的问题。
还需要注意每个mq配置中各个broker的命名不能相同
这样activemq安装就完成了。
接下来提供两个测试例子:
发送方:
package com.watson.httpclient;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Sender {
private static final int SEND_NUMBER = 5;
public static void main(String[] args) {
// 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,
"failover:(tcp://192.168.178.130:61616,tcp://192.168.178.130:61617)");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 获取session
destination = session.createQueue("FirstQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
public static void sendMessage(Session session, MessageProducer producer)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session
.createTextMessage("ActiveMq发送的消息" + i);
// 发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
producer.send(message);
}
}
}
接收发:
package com.watson.httpclient;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Receive {
public static void main(String[] args) {
// 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,
"failover:(tcp://192.168.178.130:61616,tcp://192.168.178.130:61617)");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// 获取session
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
// 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(10000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}