1、新建maven
2、pom.xml
<?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>com.hhf.activemq</groupId>
<artifactId>activemq_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--activemq所需要的jar包-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
<!-- 下面是通用jar包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在点对点的消息传递域中,目的地被称之为队列(queue):
生产者:
package com.hhf.activemq.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class JmsProduce {
public static final String ACTIVEMQ_URL = "tcp://192.168.136.137:61616";
public static final String QUEUE_NAME = "queue01";
public static void main(String[] args) throws JMSException {
//1.创建连接工场,按照给定的URL地址,采用默认用户名和密码
ActiveMQConnectionFactory activeMQConnectionFactory =new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工场,获得连接connection并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
connection.start(); //连上地址
//3.创建会话session,
// 两个参数,第一个参数叫:事物,第二个参数叫:签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列还是主题topic)
//Destination destination = session.createQueue(QUEUE_NAME); 可以赋给目的地,而不直接赋给具体的队列还是主题,但是一般不建议这么做
Queue queue = session.createQueue(QUEUE_NAME);
//5.创建消息的生产者
MessageProducer messageProducer = session.createProducer(queue);
//6.通过使用消息生产者messageProducer生产3条消息发送到MQ的队列里面
for (int i = 1; i <=3 ; i++) {
//7.创建消息,好比学生按照老师的要求写好的面试题消息
TextMessage textMessage = session.createTextMessage("msg---" + i);//理解为一个字符串
//8.通过messageProducer发送给MQ
messageProducer.send(textMessage);
}
//9.关闭资源
messageProducer.close();
session.close();
connection.close();
System.out.println("*****消息发布到MQ完成");
}
}
消费者:
package queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
public class JmsConsumer {
public static final String ACTIVEMQ_URL="tcp://192.168.56.10:61616";
//public static final String ACTIVEMQ_URL="tcp://localhost:61616";
public static final String QUEUE_NAME="jdbc01";
public static void main(String[] args) throws JMSException, IOException {
System.out.println("1号消费者");
//1、创建连接工厂
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2、通过连接工厂获得连接connection,并启动
Connection connection = factory.createConnection();
connection.start();
//3、创建会话session,参数:事务,签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4、创建目的地(具体是队列还是主题)
Queue queue = session.createQueue(QUEUE_NAME);
//5、创建消费者
MessageConsumer messageConsumer = session.createConsumer(queue);
/*
//同步阻塞方法(receive())
//订阅者或接收者调用messageConsumer的receive()方法来接收消息。receive方法在能够接收到消息(或超时)之前一直阻塞
while(true){
TextMessage textMessage =(TextMessage) messageConsumer.receive(4000L);
if(textMessage!=null){
System.out.println("消费者收到消息:"+textMessage.getText());
}else {
System.out.println("--------------");
break;
}
}
messageConsumer.close();
session.close();
connection.close();*/
//通过监听的方式来消费消息
//异步非阻塞方式(监听器onMessage())
//messageConsumer注册一个监听器,当消息到达之后,系统自动调用监听器setMessageListener的onMessage方法
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if(message != null && message instanceof TextMessage){
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("消费:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
});
System.in.read();//等待消费完再关闭
messageConsumer.close();
session.close();
connection.close();
}
}
两种消费方式
同步阻塞方式(receive())
订阅者或接收者调用MessageConsumer的receive()方法来接收消息,receive方法在能够接收到消息之前(或超时之前)将一直阻塞。
异步非阻塞方式(监听器onMessage())
订阅者或接收者通过MessageConsumer的setMessageListener(MessageListener listener)注册一个消息监听器,
当消息到达之后,系统自动调用监听器MessageListener的OnMessage(Message message)方法。
在发布订阅消息传递域中,目的地被称之为主题(topic)
发布主题生产者:
package com.hhf.activemq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by Administrator on 2019/9/21.
*/
public class JmsProduce_Topic {
public static final String ACTIVEMQ_URL = "tcp://192.168.136.137:61616";
public static final String TOPIC_NAME = "topic-atguigu";
public static void main(String[] args) throws JMSException {
//1.创建连接工场,按照给定的URL地址,采用默认用户名和密码
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工场,获得连接connection并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
connection.start(); //连上地址
//3.创建会话session,
// 两个参数,第一个参数叫:事物,第二个参数叫:签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列还是主题topic)
//Destination destination = session.createQueue(QUEUE_NAME); 可以赋给目的地,而不直接赋给具体的队列还是主题,但是一般不建议这么做
Topic topic = session.createTopic(TOPIC_NAME);
//5.创建消息的生产者
MessageProducer messageProducer = session.createProducer(topic);
//6.通过使用消息生产者messageProducer生产3条消息发送到MQ的队列里面
for (int i = 1; i <= 6; i++) {
//7.创建消息,好比学生按照老师的要求写好的面试题消息
TextMessage textMessage = session.createTextMessage("TOPIC_NAME---" + i);//理解为一个字符串
//8.通过messageProducer发送给MQ
messageProducer.send(textMessage);
}
//9.关闭资源
messageProducer.close();
session.close();
connection.close();
System.out.println("*****TOPIC_NAME消息发布到MQ完成");
}
}
订阅主题消费者:
package com.hhf.activemq.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
/**
* Created by Administrator on 2019/9/21.
*/
public class JmsConsumer_Topic {
public static final String ACTIVEMQ_URL = "tcp://192.168.136.137:61616";
public static final String TOPIC_NAME = "topic-atguigu";
public static void main(String[] args) throws JMSException, IOException {
System.out.println("***我是二号消费者");
//1.创建连接工场,按照给定的URL地址,采用默认用户名和密码
ActiveMQConnectionFactory activeMQConnectionFactory =new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工场,获得连接connection并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
connection.start(); //连上地址
//3.创建会话session,
// 两个参数,第一个参数是否以事物提交,第二个参数是默认的签收方式
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列还是主题topic)
//Destination destination = session.createQueue(QUEUE_NAME); 可以赋给目的地,而不直接赋给具体的队列还是主题,但是一般不建议这么做
Topic topic = session.createTopic(TOPIC_NAME);
//5.创建消费者
MessageConsumer messageConsumer = session.createConsumer(topic);
//通过监听的方式来消费消息
messageConsumer.setMessageListener((message) ->{
if (null != message && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("****消费者接收到Topic消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
System.in.read(); //保证控制台不关
messageConsumer.close();
session.close();
connection.close();
}
}
先启动订阅再启动生产,不然发送的消息是废消息