
- Apache ActiveMQ 教程
- Apache ActiveMQ - 首頁
- Apache ActiveMQ - 概覽
- Apache ActiveMQ - 環境設定
- Apache ActiveMQ - 功能
- Apache ActiveMQ - 執行代理伺服器
- Apache ActiveMQ - 管理控制檯
- 基於 Apache ActiveMQ 佇列的示例
- Apache ActiveMQ - 生產程式應用程式
- Apache ActiveMQ - 消費程式應用程式
- Apache ActiveMQ - 測試程式應用程式
- 基於 Apache ActiveMQ 主題的示例
- Apache ActiveMQ - 釋出程式應用程式
- Apache ActiveMQ - 訂閱程式應用程式
- Apache ActiveMQ - 測試程式應用程式
- Apache ActiveMQ 有用資源
- Apache ActiveMQ - 快速指南
- Apache ActiveMQ - 有用資源
- Apache ActiveMQ - 討論
Apache ActiveMQ - 訂閱程式應用程式
現在,讓我們建立一個訂閱程式應用程式,該應用程式將從 ActiveMQ 主題接收訊息。
建立專案
使用 eclipse,選擇 **檔案** → **新建** → **Maven 專案**。勾選 **建立一個簡單專案(跳過原型選擇)**,然後單擊下一步。
輸入詳細資訊,如下所示 −
**groupId** − com.tutorialspoint
**artifactId** − subscriber
**version** − 0.0.1-SNAPSHOT
**name** − ActiveMQ 訂閱程式
點選完成按鈕,將建立一個新專案。
pom.xml
現在更新 pom.xml 的內容以包含 ActiveMQ 的依賴項。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tutorialspoint.activemq</groupId> <artifactId>subscriber</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ActiveMQ Subscriber</name> <dependencies> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jms_1.1_spec</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.qpid</groupId> <artifactId>qpid-jms-client</artifactId> <version>0.40.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.fusesource.mvnplugins</groupId> <artifactId>maven-uberize-plugin</artifactId> <version>1.14</version> <executions> <execution> <phase>package</phase> <goals><goal>uberize</goal></goals> </execution> </executions> </plugin> </plugins> </build> </project>
現在建立一個訂閱程式類,該類將從 ActiveMQ 佇列接收訊息。
package com.tutorialspoint.activemq; import java.io.Console; import javax.jms.Connection; import javax.jms.Destination; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.qpid.jms.JmsConnectionFactory; public class Subscriber { public static void main(String[] args) throws Exception { // Create a connection to ActiveMQ JMS broker using AMQP protocol JmsConnectionFactory factory = new JmsConnectionFactory("amqp://:5672"); Connection connection = factory.createConnection("admin", "password"); connection.start(); // Create a session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create a topic Destination destination = session.createTopic("MyFirstTopic"); // Create a subscriber specific to topic MessageConsumer subscriber = session.createConsumer(destination); Console c = System.console(); String response; do { // Receive the message Message msg = subscriber.receive(); response = ((TextMessage) msg).getText(); System.out.println("Received = "+response); } while (!response.equalsIgnoreCase("Quit")); // Close the connection connection.close(); } }
訂閱程式類建立一個連線,啟動會話,建立一個使用者,然後從主題(如有)接收訊息。如果主題包含退出作為訊息,則應用程式終止,否則它將繼續輪詢佇列以獲取訊息。
我們將在 ActiveMQ - 測試應用程式 章節中多次執行此應用程式以在 ActiveMQ 中建立多個訂閱程式。
廣告