介紹 Active MQ是個訊息佇列管理器,用於通訊的中間件。現在由我來跟大家介紹如何使用ActiveMQ測試小程式。
首先需要下載ActiveMQ,並修改記憶體
#修改bin目錄下activemq.bat
在最後一行加入
set ACTIVEMQ_OPTS=-Xms1G -Xmx1G
#儲存後執行該bat檔案
新maven專案
pom檔案中加入依賴
<dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <!--我的mq版本为5.9.0--> <version>5.9.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-broker --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> <version>5.10.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-client --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.14.0</version> </dependency> </dependencies>
發送端程式
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq. ActiveMQConnectionFactory;import javax.jms.*;public class Send { // private static final int SEND_NUMBER = 10; public static void main(String[] args){ //ConnectionFactory:连接工厂,JMS用它创建连接 ConnectionFactory connectionFactory; //Connection:JMS客户端到JMS Provider的连接 Connection connection = null; //Session:一个发送或接收消息的线程 Session session; //Destination:消息的目的地;消息的接收者 Destination destination; //MessageProducer:消息发送者 MessageProducer producer; //TextMessage message; //构造ConnectionFactory实例对象,此处采用ActiveMQ的实现jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616" ); try{ //构造从工厂得到连接对象 connection = connectionFactory.createConnection(); //启动 connection.start(); //获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //获取session注意参数值test是一个服务器的queue,须在ActiveMQ的console配置 destination = session.createQueue("test"); //得到发送者 producer = session.createProducer(destination); //设置不持久化,实际情况请根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //构造消息,这里写死了,项目就是参数,或者方法获取 sendMessage(session,producer); session.commit(); }catch (Exception e){ e.printStackTrace(); }finally { try { if(null != connection) connection.close(); }catch (Throwable ignore){} } } public static void sendMessage(Session session, MessageProducer producer) throws Exception{ for (int i = 1; i <= 100000; i++){ TextMessage message = session.createTextMessage("今日天气asda" + i);//发送消息到目的地 System.out.println("发送:"+message.getText()); producer.send(message); } } }
接收端程式
import org.apache.activemq.ActiveMQConnection;import org.apache.activemq. ActiveMQConnectionFactory;import javax.jms.*;public class revice { public static void main(String[] args) { ConnectionFactory connectionFactory; Connection connection = null; Session session; Destination destination; //接收者-消费者 MessageConsumer messageConsumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616"); try{ connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("test"); messageConsumer = session.createConsumer(destination); while(true){ TextMessage message = (TextMessage) messageConsumer.receive(1000); if(null != message){ System.out.println("收到:"+message.getText()); }else{ break; } message.acknowledge(); } }catch(Exception ex){ ex.printStackTrace(); }finally{ try{ if(null != connection){ connection.close(); } }catch(Throwable ig){ } } } }
相關建議:
1ActiveMQ使用2ActiveMQC#Client實現3ActiveMQC Client實作通
Yii的學習(5)--Active Record的關聯,yii--active
以上是如何使用ActiveMQ測試小程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器