1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
当前最新版本5.13.0 必须要运行在jdk1.7以上版本,如果你的jdk无法升级到1.7 那只去
2. 解压缩
tar -zxvf apache-activemq-5.13.0-bin.tar.gz
./apache-activemq-5.13.0/bin/activemq start
3.1 启动成功后,可以看到如下提示:
INFO: Loading '/Users/java/apache-activemq-5.13.0//bin/env'
INFO: Using java '/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/Users/java/apache-activemq-5.13.0//data/activemq.pid' (pid '1982')
3.2 通过PS命令验证进程的存在:
bogon:conf wuyan$ ps -a | grep 1982
1982 ttys002 0:25.36 /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bin/java -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/Users/wy/java/apache-activemq-5.13.0//conf/login.config -Dcom.sun.management.jmxremote -Djava.awt.headless=true -Djava.io.tmpdir=/Users/wy/java/apache-activemq-5.13.0//tmp -Dactivemq.classpath=/Users/wy/java/apache-activemq-5.13.0//conf:/Users/wy/java/apache-activemq-5.13.0//../lib/ -Dactivemq.home=/Users/wy/java/apache-activemq-5.13.0/ -Dactivemq.base=/Users/wy/java/apache-activemq-5.13.0/ -Dactivemq.conf=/Users/wy/java/apache-activemq-5.13.0//conf -Dactivemq.data=/Users/wy/java/apache-activemq-5.13.0//data -jar /Users/wy/java/apache-activemq-5.13.0//bin/activemq.jar start
4. 多线程的发送、读取实现
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* @Description:
* @author wuyan
* @date 2015年12月21日 下午1:59:13
*/
public class POPQueue {
private Connection connection = null;
private Session session = null;
private MessageProducer producer = null ;
private MessageConsumer consumer = null ;
// 队列初始化,用于多线程
public POPQueue(String QName){
this("tcp://localhost:61616",ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,QName);
}
public POPQueue(String host,String user,String pwd,String QName){
ConnectionFactory connectionFactory;
Connection connection = null;
Destination destination;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(user,pwd,host);
try {
connection = connectionFactory.createConnection();
connection.start();
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(QName);
// 发送者
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 接收者
consumer = session.createConsumer(destination);
//
} catch (Exception e) {
e.printStackTrace();
}
}
// 消息发送
public void sendMessage(String message) throws Exception {
System.out.println("send:" + message);
TextMessage txtMessage = session
.createTextMessage(message);
// 发送消息到目的地方
producer.send(txtMessage);
session.commit();
}
// 消息接收
public void close() throws JMSException{
if (null !=session)
session.close();
if (null != connection)
connection.close();
}
public String getMessage() throws JMSException{
TextMessage msg = (TextMessage)consumer.receiveNoWait();
session.commit();
return msg == null ? null : msg.getText();
}
public static void main(String[] s) throws Exception{
new Thread("sender"){
public void run(){
try{
POPQueue queue = new POPQueue("test");
// int i = 0 ;
// while(true){
// i++;
for(int i=0; i< 10 ;i++){
queue.sendMessage("msg_" + i);
Thread.sleep(1000);
}
queue.close();
}catch(Exception e){
e.printStackTrace();
}
}
}.start();
new Thread("receiver"){
public void run(){
try{
POPQueue queue = new POPQueue("test");
int i = 0 ;
while(true){
String s = queue.getMessage();
if (s == null){
System.out.println("-----------------------------wait");
Thread.sleep(2000);
continue ;
}
System.out.println("-----------------------------receive:" + s);
Thread.sleep(10);
}
}catch(Exception e){
e.printStackTrace();
}
}
}.start();
}
}
5. 打印结果
-----------------------------wait
send:msg_0
send:msg_1
-----------------------------receive:msg_1
-----------------------------wait
send:msg_2
send:msg_3
-----------------------------receive:msg_3
-----------------------------wait
send:msg_4
send:msg_5
-----------------------------receive:msg_5
-----------------------------wait
send:msg_6
send:msg_7
-----------------------------receive:msg_7
-----------------------------wait
send:msg_8
send:msg_9
-----------------------------receive:msg_9
-----------------------------wait
-----------------------------receive:msg_0
-----------------------------receive:msg_2
-----------------------------receive:msg_4
-----------------------------receive:msg_6
-----------------------------receive:msg_8
-----------------------------wait
-----------------------------wait
-----------------------------wait
6. 番外篇
以上属于极顺利的过程,但是在我验证完以上环节之后,准备部署到测试服务器时,发现MQ怎么也启不来。执行activemq start时不报错,但是在进程中却看不到它的身影,于是我急呀急。。。急呀急。。。。。。
首先想到的是找日志,可是发现activemq的目录里没有logs目录,肿么办。。。肿么办。。。。
于是,我进入bin目录把activemq.jar 包解压缩了,通过查看解压缩后的 META-INF\MANIFEST.MF 文件发现,原来5.13.0是用jdk1.7打的包,而我当前的jdk是1.6 所以启动不成功。
解决办法1:升级jdk(老板不同意) 2:降低mq的版本(老板不管),于是,问题解决了。。。。。。。