這篇文章給大家分享的是有關(guān)使用ActiveMQ測試小程序的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

修改bin目錄下activemq.bat
在最后一行加入
set ACTIVEMQ_OPTS=-Xms1G -Xmx1G
保存后執(zhí)行該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>
發(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:一個發(fā)送或接收消息的線程
Session session; //Destination:消息的目的地;消息的接收者
Destination destination; //MessageProducer:消息發(fā)送者
MessageProducer producer; //TextMessage message;
//構(gòu)造ConnectionFactory實例對象,此處采用ActiveMQ的實現(xiàn)jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"
); try{ //構(gòu)造從工廠得到連接對象
connection = connectionFactory.createConnection();
//啟動
connection.start();
//獲取操作連接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
//獲取session注意參數(shù)值test是一個服務(wù)器的queue,須在ActiveMQ的console配置
destination = session.createQueue("test");
//得到發(fā)送者
producer = session.createProducer(destination);
//設(shè)置不持久化,實際情況請根據(jù)項目決定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//構(gòu)造消息,這里寫死了,項目就是參數(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; //接收者-消費者
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測試小程序的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享題目:使用ActiveMQ測試小程序的方法-創(chuàng)新互聯(lián)
文章鏈接:http://www.chinadenli.net/article4/dpsoie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、品牌網(wǎng)站制作、定制網(wǎng)站、微信小程序、移動網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)