如何使用java程序操作ActiveMQ
第一步:開發activeMQ要先導入activemq-all-5.14.0.jar包,因為我創建的是maven 項目,因此需要使用maven坐標導入。如果創建的是普通java項目,則應該在lib目錄下導入jar包。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.niwotaxuexiba.maven</groupId><artifactId>activeMQ_helloworld</artifactId><version>0.0.1-SNAPSHOT</version><name>activeMQ_helloworld</name><dependencies><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId><version>5.14.0</version></dependency></dependencies> </project>第二步:因為要使用JUnit測試,所以也需要將JUnit的jar包也導入pom文件。 <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> </dependency>
第三步:編寫MQ消息生產者。分為幾個小的步驟:
①連接工廠。
②選擇使用默認的用戶名/密碼/路徑。
③new一個ActiveMQConnectionFactory對象,也就是實例化一個工廠,這是使用ActiveMQ的入口。
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
④獲取一個連接
Connection connection = connectionFactory.createConnection();
⑤建立會話
Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
⑥創建隊列或者話題對象
Queue queue = session.createQueue("HelloWorld");
⑦創建生產者或者消費者
MessageProducer producer = session.createProducer(queue);
⑧發送消息
for(int i = 0; i < 10; i++){
producer.send(session.createTextMessage("你好,activeMQ:"+i));
}
⑨提交操作
session.commit();
public class ActiveMQProducer {@Testpublic void testProduceMQ() throws Exception {// 連接工廠// 使用默認用戶名、密碼、路徑// 路徑 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 獲取一個連接Connection connection = connectionFactory.createConnection();// 建立會話Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);// 創建隊列或者話題對象Queue queue = session.createQueue("HelloWorld");// 創建生產者 或者 消費者MessageProducer producer = session.createProducer(queue);// 發送消息for (int i = 0; i < 10; i++) {producer.send(session.createTextMessage("你好,activeMQ:" + i));}// 提交操作session.commit();} }查看控制臺,消息已經被生產
①使用MessageConsumer完成消費:
public class ActiveMQConsumer {@Test// 直接消費public void testCosumeMQ() throws Exception {// 連接工廠// 使用默認用戶名、密碼、路徑// 路徑 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 獲取一個連接Connection connection = connectionFactory.createConnection();// 開啟連接connection.start();// 建立會話// 第一個參數,是否使用事務,如果設置true,操作消息隊列后,必須使用 session.commit();Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 創建隊列或者話題對象Queue queue = session.createQueue("HelloWorld");// 創建消費者MessageConsumer messageConsumer = session.createConsumer(queue);while (true) {TextMessage message = (TextMessage) messageConsumer.receive(10000);if (message != null) {System.out.println(message.getText());} else {break;}}} }查看控制臺,發現信息已經被消費
②使用監聽器,監聽消息的內容,進行消費。
@Test// 使用監聽器消費public void testCosumeMQ2() throws Exception {// 連接工廠// 使用默認用戶名、密碼、路徑// 路徑 tcp://host:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();// 獲取一個連接Connection connection = connectionFactory.createConnection();// 開啟連接connection.start();// 建立會話// 第一個參數,是否使用事務,如果設置true,操作消息隊列后,必須使用 session.commit();Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 創建隊列或者話題對象Queue queue = session.createQueue("HelloWorld");// 創建消費者MessageConsumer messageConsumer = session.createConsumer(queue);messageConsumer.setMessageListener(new MessageListener() {// 每次接收消息,自動調用 onMessagepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println(textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});while (true) {// 不能讓junit線程死掉}}以上就是activeMQ消息的生產及效果的代碼編寫。
總結
以上是生活随笔為你收集整理的如何使用java程序操作ActiveMQ的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Selenium自动写公众号文章
- 下一篇: JAX-RS客户端WebClient的使