您好,登錄后才能下訂單哦!
一、安裝
啟動之后成功
二、創建實例測試ActiveMQ
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
消息發送
public class QueueSender {
public static void main(String[] args) throws Exception{
//連接工廠
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
//帶事務的session
Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < 3; i++) {
TextMessage message = session.createTextMessage("message--"+i);
Thread.sleep(1000);
//通過消息生產者發出消息
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
運行main方法,控制臺輸出如下:
那這個消息發送到哪里去了呢?
消息接收
public class QueueReceive {
public static void main(String[] args) throws Exception{
//連接工廠
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
//帶事務的session,并且自動簽收
final Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageConsumer consumer = session.createConsumer(destination);
int i = 0;
while (i<3){
i++;
TextMessage message = (TextMessage) consumer.receive();
session.commit();
System.out.println("收到的消息是:"+message.getText());
}
session.close();
connection.close();
}
}
運行main方法
此時再看服務器端會發送消息已經被接收處理了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。