日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ActiveMQ搭建

發(fā)布時(shí)間:2025/3/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ActiveMQ搭建 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

下載

到ActiveMQ官網(wǎng),找到下載點(diǎn)。

目前,

官網(wǎng)為http://activemq.apache.org/

Linux版本下載點(diǎn)之一為:http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz

?

啟動(dòng)

下載到本機(jī),并解壓

wget http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz tar -xf ./apache-activemq-5.11.1-bin.tar.gz
wget http://apache.fayea.com/activemq/5.11.1/apache-activemq-5.11.1-bin.tar.gz tar -xf ./apache-activemq-5.11.1-bin.tar.gz

?

啟動(dòng)(當(dāng)然,由于依賴于JAVA,如果你沒(méi)有安裝JAVA,它會(huì)提醒你的,哈哈)

cd ./apache-activemq-5.11.1-bin/bin ./activemq start
cd ./apache-activemq-5.11.1-bin/bin ./activemq start

?

測(cè)試啟動(dòng)成功與否

ActiveMQ默認(rèn)監(jiān)聽(tīng)61616端口,查此端口看看是否成功啟動(dòng)

netstat -an|grep 61616
netstat -an|grep 61616

?

如果一切順利,會(huì)看到如下日志

[nicchagil@localhost bin]$ ./activemq start INFO: Loading '/home/nicchagil/app/apache-activemq-5.11.1/bin/env' INFO: Using java '/home/nicchagil/app/jdk1.7.0_71//bin/java' INFO: Starting - inspect logfiles specified in logging.properties and log4j.prop erties to get details INFO: pidfile created : '/home/nicchagil/app/apache-activemq-5.11.1/data/activem q.pid' (pid '4858') [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ netstat -an | grep 61616 tcp 0 0 :::61616 :::* LIST EN [nicchagil@localhost bin]$
[nicchagil@localhost bin]$ ./activemq start INFO: Loading '/home/nicchagil/app/apache-activemq-5.11.1/bin/env' INFO: Using java '/home/nicchagil/app/jdk1.7.0_71//bin/java' INFO: Starting - inspect logfiles specified in logging.properties and log4j.prop erties to get details INFO: pidfile created : '/home/nicchagil/app/apache-activemq-5.11.1/data/activem q.pid' (pid '4858') [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ [nicchagil@localhost bin]$ netstat -an | grep 61616 tcp 0 0 :::61616 :::* LIST EN [nicchagil@localhost bin]$

?

順便,登錄下管理員頁(yè)面,看看有木有問(wèn)題:

URL : http://10.0.0.109:8161/admin/

ACC/PWD : admin/admin

?

嘗試基本消息功能

接下來(lái),用簡(jiǎn)單的點(diǎn)對(duì)點(diǎn)測(cè)試消息發(fā)送、消息接收。

引入包:

  • activemq-client-5.11.1.jar
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • hawtbuf-1.11.jar
  • slf4j-api-1.7.10.jar

?

消息發(fā)送

package com.nicchagil.activemq.study.No001點(diǎn)對(duì)點(diǎn);import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session;import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws JMSException {ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");Connection connection = factory.createConnection();connection.start();Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("TestQueue");MessageProducer producer = session.createProducer(destination);producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);ObjectMessage message = session.createObjectMessage("hello world...");producer.send(message);session.commit();System.out.println("sent...");}}
package com.nicchagil.activemq.study.No001點(diǎn)對(duì)點(diǎn);import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.ObjectMessage; import javax.jms.Session;import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws JMSException {ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");Connection connection = factory.createConnection();connection.start();Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("TestQueue");MessageProducer producer = session.createProducer(destination);producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);ObjectMessage message = session.createObjectMessage("hello world...");producer.send(message);session.commit();System.out.println("sent...");}}

?

消息接收

package com.nicchagil.activemq.study.No001點(diǎn)對(duì)點(diǎn);import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.ObjectMessage; import javax.jms.Session;import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver {public static void main(String[] args) throws JMSException {ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");Connection connection = factory.createConnection();connection.start();Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("TestQueue");MessageConsumer consumer = session.createConsumer(destination);ObjectMessage message = (ObjectMessage)consumer.receive();if (message != null) {String messageString = (String)message.getObject();System.out.println("Receive : " + messageString);}}}
package com.nicchagil.activemq.study.No001點(diǎn)對(duì)點(diǎn);import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.ObjectMessage; import javax.jms.Session;import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver {public static void main(String[] args) throws JMSException {ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://10.0.0.109:61616");Connection connection = factory.createConnection();connection.start();Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);Destination destination = session.createQueue("TestQueue");MessageConsumer consumer = session.createConsumer(destination);ObjectMessage message = (ObjectMessage)consumer.receive();if (message != null) {String messageString = (String)message.getObject();System.out.println("Receive : " + messageString);}}}

?

看到console打印出:Receive : hello world...,可知接收到消息了,內(nèi)流滿面啊啊啊啊。。。

?

關(guān)閉

查詢進(jìn)程id(pid),禁止其進(jìn)程:

ps -ef | grep activemq kill -9 pid
ps -ef | grep activemq kill -9 pid

?

再運(yùn)行Sender,她就無(wú)法連接了,哈哈哈哈哈

Exception in thread "main" javax.jms.JMSException: Could not connect to broker URL: tcp://10.0.0.109:61616. Reason: java.net.ConnectException: Connection refused: connect
Exception in thread "main" javax.jms.JMSException: Could not connect to broker URL: tcp://10.0.0.109:61616. Reason: java.net.ConnectException: Connection refused: connect

?

好了,基本的搭建告一段落。

?

荊棘

過(guò)程中,遇到一個(gè)小問(wèn)題,就是我一開(kāi)始是用JDK1.6去跑的,報(bào)出常見(jiàn)的Unsupported major.minor version 51.0

針對(duì)這個(gè)問(wèn)題,這個(gè)帖子有很好的參考意義:

http://www.cnblogs.com/chinafine/articles/1935748.html

找出jar中的一個(gè)class,執(zhí)行以下命令,可查出minor version、major version:

javap -verbose yourClassName
javap -verbose yourClassName

?

或直接查看jar中的META-INF\MANIFEST.MF。

然后對(duì)照帖子中的JDK版本,換成JDK1.7就OK了。

總結(jié)

以上是生活随笔為你收集整理的ActiveMQ搭建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。