创建maven工程,pom文件如下
[html] view plain copy
- <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>com.tansun</groupId>
- <artifactId>ActivemqTest</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-all</artifactId>
- <version>5.15.2</version>
- </dependency>
- </dependencies>
- </project>
<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>com.tansun</groupId>
<artifactId>ActivemqTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.2</version>
</dependency>
</dependencies>
</project>
生产者的类:每运行一次生产者的主方法,就会发送一条消息
[java] view plain copy
- package com.tansun;
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.JMSException;
- import javax.jms.MessageProducer;
- import javax.jms.Queue;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- public class Producer {
- public static void main(String[] args) throws JMSException {
- // 创建连接工厂
- ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.229.128:61616");
- // 创建连接
- Connection connection = connectionFactory.createConnection();
- // 开启连接
- connection.start();
- // 创建会话(不使用事物)
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- // 创建一对一的消息队列
- Queue queue = session.createQueue("queue test");
- // 创建生产者
- MessageProducer producer = session.createProducer(queue);
- // 创建消息
- TextMessage message = session.createTextMessage("hello activemq");
- // 发送消息
- producer.send(message);
- // 关闭会话和连接
- session.close();
- connection.close();
- }
- }
package com.tansun;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.229.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 开启连接
connection.start();
// 创建会话(不使用事物)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建一对一的消息队列
Queue queue = session.createQueue("queue test");
// 创建生产者
MessageProducer producer = session.createProducer(queue);
// 创建消息
TextMessage message = session.createTextMessage("hello activemq");
// 发送消息
producer.send(message);
// 关闭会话和连接
session.close();
connection.close();
}
}
消费者的类:消费者使用死循环接收消息并打印到控制台上
[java] view plain copy
- package com.tansun;
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.JMSException;
- import javax.jms.MessageConsumer;
- import javax.jms.Queue;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- public class Consumer {
- public static void main(String[] args) throws JMSException {
- // 创建连接工厂
- ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.229.128:61616");
- // 创建连接
- Connection connection = connectionFactory.createConnection();
- // 开启连接
- connection.start();
- // 创建回话(不使用事物)
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- // 创建一对一的消息队列
- Queue queue = session.createQueue("queue test");
- // 创建消费者
- MessageConsumer consumer = session.createConsumer(queue);
- // 接收消息
- while(true){
- TextMessage message = (TextMessage)consumer.receive();
- System.out.println(message.getText());
- }
- }
- }
package com.tansun;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
public static void main(String[] args) throws JMSException {
// 创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.229.128:61616");
// 创建连接
Connection connection = connectionFactory.createConnection();
// 开启连接
connection.start();
// 创建回话(不使用事物)
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建一对一的消息队列
Queue queue = session.createQueue("queue test");
// 创建消费者
MessageConsumer consumer = session.createConsumer(queue);
// 接收消息
while(true){
TextMessage message = (TextMessage)consumer.receive();
System.out.println(message.getText());
}
}
}