您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關使用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){ } } } }
感謝各位的閱讀!關于使用ActiveMQ測試小程序的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。