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的版本(老闆不管),於是,問題解決了。。。。。。。