第一步:开发activeMQ要先导入activemq-all-5.14.0.jar包,因为我创建的是maven 项目,因此需要使用maven坐标导入。如果创建的是普通java项目,则应该在lib目录下导入jar包。
<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.niwotaxuexiba.maven</groupId>
<artifactId>activeMQ_helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>activeMQ_helloworld</name>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.0</version>
</dependency>
</dependencies>
</project>
第二步:因为要使用JUnit测试,所以也需要将JUnit的jar包也导入pom文件。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
第三步:编写MQ消息生产者。分为几个小的步骤:
①连接工厂。
②选择使用默认的用户名/密码/路径。
③new一个ActiveMQConnectionFactory对象,也就是实例化一个工厂,这是使用ActiveMQ的入口。
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
④获取一个连接
Connection connection = connectionFactory.createConnection();
⑤建立会话
Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
⑥创建队列或者话题对象
Queue queue = session.createQueue("HelloWorld");
⑦创建生产者或者消费者
MessageProducer producer = session.createProducer(queue);
⑧发送消息
for(int i = 0; i < 10; i++){
producer.send(session.createTextMessage("你好,activeMQ:"+i));
}
⑨提交操作
session.commit();
public class ActiveMQProducer {
@Test
public void testProduceMQ() throws Exception {
// 连接工厂
// 使用默认用户名、密码、路径
// 路径 tcp://host:61616
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 获取一个连接
Connection connection = connectionFactory.createConnection();
// 建立会话
Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);
// 创建队列或者话题对象
Queue queue = session.createQueue("HelloWorld");
// 创建生产者 或者 消费者
MessageProducer producer = session.createProducer(queue);
// 发送消息
for (int i = 0; i < 10; i++) {
producer.send(session.createTextMessage("你好,activeMQ:" + i));
}
// 提交操作
session.commit();
}
}
查看控制台,消息已经被生产
①使用MessageConsumer完成消费:
public class ActiveMQConsumer {
@Test
// 直接消费
public void testCosumeMQ() throws Exception {
// 连接工厂
// 使用默认用户名、密码、路径
// 路径 tcp://host:61616
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 获取一个连接
Connection connection = connectionFactory.createConnection();
// 开启连接
connection.start();
// 建立会话
// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// 创建队列或者话题对象
Queue queue = session.createQueue("HelloWorld");
// 创建消费者
MessageConsumer messageConsumer = session.createConsumer(queue);
while (true) {
TextMessage message = (TextMessage) messageConsumer.receive(10000);
if (message != null) {
System.out.println(message.getText());
} else {
break;
}
}
}
}
查看控制台,发现信息已经被消费
②使用监听器,监听消息的内容,进行消费。
@Test
// 使用监听器消费
public void testCosumeMQ2() throws Exception {
// 连接工厂
// 使用默认用户名、密码、路径
// 路径 tcp://host:61616
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
// 获取一个连接
Connection connection = connectionFactory.createConnection();
// 开启连接
connection.start();
// 建立会话
// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// 创建队列或者话题对象
Queue queue = session.createQueue("HelloWorld");
// 创建消费者
MessageConsumer messageConsumer = session.createConsumer(queue);
messageConsumer.setMessageListener(new MessageListener() {
// 每次接收消息,自动调用 onMessage
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
while (true) {
// 不能让junit线程死掉
}
}
以上就是activeMQ消息的生产及效果的代码编写。