ActiveMQ-安裝和基本使用
阿新 • • 發佈:2019-02-12
1.Windows下安裝ActiveMQ
1.1.官網下載ActiveMQ
1.2.解壓修改配置檔案
apache-activemq-5.15.3\conf\activemq.xml
將0.0.0.0修改為localhost
ps:若出現以下錯誤閃退無法啟動,則因MQ版本和JRE版本不匹配問題導致,更換JRE或者MQ版本即可。這裡我降低了MQ的版本解決問題。
ps:如果更換版本依然出現閃退,則可以通過編輯activemq.bat,在末端加上pause,重新啟動則可以暫停閃退找出問題
ps:windows系統直接執行activemq.bat閃退,則可嘗試使用cms命令列 執行activemq-admin.bat start可以解決問題
啟動成功後,訪問http://localhost:8161/admin,即可登入ActiveMQ管理介面。預設賬號admin 密碼admin
2.Linux下安裝ActiveMQ
2.1 建立目錄
cd /usr/local
mkdir activemq
2.2 下載並解壓activemq
tar -zxvf apache-activemq-5.13.5-bin.tar.gz
2.3 啟動activemq activemq啟動分linux-x86-32和linux-x86-64 cd linux-x86-64 ./activemq start
2.4 訪問
http://10.102.151.115:8161/admin/
3.ActiveMQ 在Java Api的基本使用
以下是一個使用參考:
3.1 導包
<!-- activemq 相關maven依賴 -->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency >
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.7.0</version>
</dependency>
<!-- 日誌相關依賴 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
3.2 API使用
ProducePool
package com.ithzk.activemq;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* @author hzk
* @date 2018/3/31
*/
public class ProducePool {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "mytopic";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageProducer messageProducer = null;
MessageProducer producer = null;
/**
* 初始化
* @throws JMSException
*/
public void initialize() throws JMSException {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = activeMQConnectionFactory.createConnection();
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
/**
* 傳送訊息
* @param message
* @throws JMSException
*/
public void produceMessage(String message) throws JMSException {
initialize();
TextMessage textMessage = session.createTextMessage(message);
connection.start();
System.out.println("Producer -> Send Message:"+message);
producer.send(textMessage);
System.out.println("Producer -> Send Message complete!");
}
public void close() throws JMSException {
System.out.println("Producer -> close connection!");
if(producer != null){
producer.close();
}
if(session != null){
session.close();
}
if(connection != null){
connection.close();
}
}
}
ProduceTest
package com.ithzk.activemq;
import javax.jms.JMSException;
import java.util.Random;
/**
* @author hzk
* @date 2018/3/31
*/
public class ProduceTest {
public static void main(String[] args) throws JMSException, InterruptedException {
ProducePool producePool = new ProducePool();
Random random = new Random();
for (int i =0;i < 20;i++){
Thread.sleep(random.nextInt(3)*1000);
producePool.produceMessage("Hello,ActiveMQ! Number:"+i);
producePool.close();
}
producePool.initialize();
}
}
這樣就可以往佇列中傳送資訊了