1、启动ActiveMQ。如何安装启动ActiveMQ请看上一篇文章。
2、Queue实现
①:pom文件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
②:消息发布
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
@Slf4j
public class Producer {
// ActiveMQ默认用户名
private static final String USERNAME = "admin";
// ActiveMQ默认登陆密码
private static final String PASSWORD = "admin";
// ActiveMQ链接地址
private static final String BROKEN_URL = "tcp://localhost:61616";
// 定义发送消息的主题名称
private static final String QUEUE_NAME = "QueueMessage";
public Boolean sendMessage() throws JMSException {
// 创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
// 从工厂中创建一个链接
Connection connection = activeMQConnectionFactory.createConnection();
// 打开连接
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建队列目标
Destination destination = session.createQueue(QUEUE_NAME);
// 创建一个生产者
MessageProducer producer = session.createProducer(destination);
// 创建消息
TextMessage message = session.createTextMessage("么么哒~~~");
// 发送消息
producer.send(message);
// 在本地打印消息
System.out.println("发送消息:" + message.getText());
// 关闭连接
connection.close();
return true;
}
}
③:消息消费
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
@Slf4j
public class Consumer {
// ActiveMQ默认用户名
private static final String USERNAME = "admin";
// ActiveMQ默认登陆密码
private static final String PASSWORD = "admin";
// ActiveMQ链接地址
private static final String BROKEN_URL = "tcp://localhost:61616";
// 定义发送消息的主题名称
private static final String QUEUE_NAME = "QueueMessage";
Boolean result;
public Boolean getMessage() throws JMSException {
// 创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEN_URL);
// 从工厂中创建一个链接
Connection connection = activeMQConnectionFactory.createConnection();
// 打开连接
connection.start();
// 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 创建队列目标
Destination destination = session.createQueue(QUEUE_NAME);
// 创建消费者
MessageConsumer consumer = session.createConsumer(destination);
// 创建消费的监听
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("获取消息:" + textMessage.getText());
if (textMessage.getText() != null || textMessage.getText() != "") {
result = true;
} else {
result = false;
}
} catch (JMSException e) {
e.printStackTrace();
result = false;
}
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
connection.close();
return result;
}
}
3、运行消息发布者和消费者。
- Queue队列是一种点对点的消费模式, 即服务端发布一条消息,消费端根据发布的消息名称进行消息队列消费。消费后,该消息状态为已消费并回收,不再支持消费端进行消费。点对点消费不过时,服务端发布消息后,如果消费端未进行及时消费,则消息一直处于挂起状态,直到消费端启动并消费,整个消息队列流程才算完成。如果在等待消费过程中,服务端系统宕机,此时如果服务端发送消息状态为持久化消息(默认),则重新启动后会出现消息回流,消费端可继续进行消费;若消息状态为非持久化状态消息,消息丢失。