GeXiangDong

精通Java、SQL、Spring的拼写,擅长Linux、Windows的开关机

0%

使用ActiveMQ 发送接收消息的例子

不用spring,仅仅jms和activeMQ

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.devmgr.activemqlistener</groupId>
<artifactId>activemq-listener</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>

</project>

发送端 JmsMessageSender.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cn.devmgr.activemqlistener;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

public class JmsMessageSender {

public static void main(String[] argvs) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 消息生产者;发送消息
Queue queue = session.createQueue("print-queue");
String payload = "需要打印了";
Message msg = session.createTextMessage(payload);
MessageProducer producer = session.createProducer(queue);
System.out.println("Sending text '" + payload + "'");
producer.send(msg);

session.close();
connection.close();
}
}

接收端 JmsMessageReceiver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cn.devmgr.activemqlistener;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

public class JmsMessageReceiver {

public static void main(String[] argvs) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 消息消费者: 接收消息
Queue queue = session.createQueue("print-queue");
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(new ConsumerMessageListener("print-queue"));
connection.start();

//60秒后退出
Thread.sleep(60000);
session.close();

connection.close();
}


public static class ConsumerMessageListener implements javax.jms.MessageListener {
private String consumerName;

public ConsumerMessageListener(String consumerName) {
this.consumerName = consumerName;
}

public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(consumerName + " received " + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}

}
}