這篇文章給大家分享的是有關(guān)使用ActiveMQ測(cè)試小程序的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了遂平免費(fèi)建站歡迎大家使用!
修改bin目錄下activemq.bat
在最后一行加入
set ACTIVEMQ_OPTS=-Xms1G -Xmx1G
保存后執(zhí)行該bat文件
新建maven項(xiàng)目
pom文件中加入依賴
org.apache.activemq activemq-all 5.9.0 org.apache.activemq activemq-broker 5.10.0 org.apache.activemq activemq-client 5.14.0
發(fā)送端程序
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用它創(chuàng)建連接 ConnectionFactory connectionFactory; //Connection:JMS客戶端到JMS Provider的連接 Connection connection = null; //Session:一個(gè)發(fā)送或接收消息的線程 Session session; //Destination:消息的目的地;消息的接收者 Destination destination; //MessageProducer:消息發(fā)送者 MessageProducer producer; //TextMessage message; //構(gòu)造ConnectionFactory實(shí)例對(duì)象,此處采用ActiveMQ的實(shí)現(xiàn)jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616" ); try{ //構(gòu)造從工廠得到連接對(duì)象 connection = connectionFactory.createConnection(); //啟動(dòng) connection.start(); //獲取操作連接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //獲取session注意參數(shù)值test是一個(gè)服務(wù)器的queue,須在ActiveMQ的console配置 destination = session.createQueue("test"); //得到發(fā)送者 producer = session.createProducer(destination); //設(shè)置不持久化,實(shí)際情況請(qǐng)根據(jù)項(xiàng)目決定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //構(gòu)造消息,這里寫(xiě)死了,項(xiàng)目就是參數(shù),或者方法獲取 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);//發(fā)送消息到目的地 System.out.println("發(fā)送:"+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; //接收者-消費(fèi)者 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){ } } } }
感謝各位的閱讀!關(guān)于使用ActiveMQ測(cè)試小程序的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!