ActiveMQ在linux下安装非常简单,步骤如下:
1 环境准备:
系统环境CentOS5.5 、 JDK版本为OpenJDK1.6.0 设置JAVA_HOME。
下载 apache-activemq-5.5.1-bin.tar.gz 。
2 安装步骤
上传apache-activemq-5.5.1-bin.tar.gz到服务器,解压
tar -xvf apache-activemq-5.5.1-bin.tar.gz
3 启动ActiveMQ
cd apache-activemq-5.5.1
bin/activemq start/restart
ps -ef|grep activemq //查看activemq进程
4 测试是否成功安装
http://192.168.110.223:8161/admin/ MQ自带的web控制台(可以在控制台测试消息的创建和消费)
5 测试代码
MQConfig 用于初始化连接
package com.yazuo.util;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MQConfig {
// 创建ActiveMQ连接工厂
private static ActiveMQConnectionFactory conFactory = null;
// connection
private static Connection con = null;
private static Session session = null;
private static MessageProducer mpCorrect = null;
private static Destination destCorrect = null;
private static MessageProducer mpError = null;
private static Destination destError = null;
public static ActiveMQConnectionFactory getConFactory() {
if (conFactory == null) {
conFactory = new ActiveMQConnectionFactory(WeiboConstants.MQServerURL);
}
return conFactory;
}
public static Connection getConnection() throws JMSException {
if (con == null) {
con = getConFactory().createConnection();
}
return con;
}
public static Session getSession() throws JMSException {
if (session == null) {
session = getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
}
return session;
}
public static Destination getDestCorrect() throws JMSException {
if (destCorrect == null) {
destCorrect = getSession().createQueue(WeiboConstants.QueueNameWeibo);
}
return destCorrect;
}
public static MessageProducer getMPCorrect() throws JMSException {
if (mpCorrect == null) {
mpCorrect = getSession().createProducer(getDestCorrect());
}
return mpCorrect;
}
public static Destination getDestError() throws JMSException {
if (destError != null) {
destError = getSession().createQueue(WeiboConstants.QueueNameWeiboError);
}
return destError;
}
public static MessageProducer getMPError() throws JMSException {
if (mpError == null) {
mpError = getSession().createProducer(getDestError());
}
return mpError;
}
}