初识ActiveMQ
JSM
Java Message Service 即java消息服务,是java平台中关于面向消息中间件的API,用于在两个应用程序(系统)中,或分布式系统中发送消息,进行异步通信,类似于JDBC(用来访问数据库的API)
JSM消息类型
点对点
queue 点对点是基于队列的,生产者发消息到队列,消费者从队列接收消息,使消息异步传输
注意
1.如果在session关闭时有部分消息没有被消费,那消费者下次连接到相同的队列时,消息会被再次接收
2.队列可以长久保存消息直到被消费者收到,消费者不用担心消息会丢失而时刻和队列保持激活的连接状态,充分体现了异步传输模式的优势
发布订阅(主题)
topic 主题可以被认为时消息的传输中介,发布者发布消息到主题,订阅者从主题订阅消息。主题时消息订阅者和消息发布者互相独立,不需要接触就能保证消息的传递。
非持久订阅
非持久订阅只有当客户端为激活状态,也就是和MQ保持连接状态才能接收到消息。如果消费者处于离线状态,生产者发送的主题消息将会丢失作废,消费者永远不会收到
先订阅才能收到发布,只给订阅者发布消息
持久订阅
客户端向MQ注册一个自己的身份id,当客户端离线状态时,生产者会为这个id保存所有发送到主题的消息,当客户端再次连接到MQ时,会根据id得到处于离线状态下的消息
非持久订阅状态下,不能恢复或重新派送一个未签收的消息
持久订阅可以恢复或重新派送一个未签收的消息
ActiveMQ
Apache出品的开源的消息总线,完全支持JSM API规范的实现
SpringBoot集成ActiveMQ实例
下载ActiveMQ并解压
官网下载即可http://activemq.apache.org/components/classic/download/
启动ActiveMQ
bin→win64/win32→activemq.bat
访问ActiveMQ后台管理页面
通过tcp://localhost:61616来连接消息服务器,用到的用户名和密码都在conf→users.properties中(默认为admin=admin)
添加springboot依赖
<!--ActiveMq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
close-timeout: 15s # 在考虑结束之前等待的时间
in-memory: true # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
non-blocking-redelivery: false # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
send-timeout: 0 # 等待消息发送响应的时间。设置为0等待永远。
queue-name: active.queue
topic-name: active.topic.name.model
# packages:
# trust-all: true #不配置此项,会报错
pool:
enabled: true
max-connections: 10 #连接池最大连接数
idle-timeout: 30000 #空闲的连接过期时间,默认为30秒
# jms:
# pub-sub-domain: true #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
# 是否信任所有包
#spring.activemq.packages.trust-all=
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
#spring.activemq.packages.trusted=
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
#spring.activemq.pool.block-if-full=true
# 如果池仍然满,则在抛出异常前阻塞时间。
#spring.activemq.pool.block-if-full-timeout=-1ms
# 是否在启动时创建连接。可以在启动时用于加热池。
#spring.activemq.pool.create-connection-on-startup=true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
#spring.activemq.pool.enabled=false
# 连接过期超时。
#spring.activemq.pool.expiry-timeout=0ms
# 连接空闲超时
#spring.activemq.pool.idle-timeout=30s
# 连接池最大连接数
#spring.activemq.pool.max-connections=1
# 每个连接的有效会话的最大数目。
#spring.activemq.pool.maximum-active-session-per-connection=500
# 当有"JMSException"时尝试重新连接
#spring.activemq.pool.reconnect-on-exception=true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
#spring.activemq.pool.time-between-expiration-check=-1ms
# 是否只使用一个MessageProducer
#spring.activemq.pool.use-anonymous-producers=true
启动类增加 @EnableJms 注解
@SpringBootApplication
@EnableJms
public class ActivemqApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqApplication.class, args);
}
}
初始化和配置 ActiveMQ 的连接
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Value("${spring.activemq.user}")
private String username;
@Value("${spring.activemq.password}")
private String password;
@Value("${spring.activemq.queue-name}")
private String queueName;
@Value("${spring.activemq.topic-name}")
private String topicName;
@Bean(name = "queue")
public Queue queue() {
return new ActiveMQQueue(queueName);
}
@Bean(name = "topic")
public Topic topic() {
return new ActiveMQTopic(topicName);
}
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(username, password, brokerUrl);
}
@Bean
public JmsMessagingTemplate jmsMessageTemplate(){
return new JmsMessagingTemplate(connectionFactory());
}
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
//在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
定义生产者
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@PostMapping("/queue/send")
public String sendQueue(@RequestBody String message){
this.sendMessage(queue,message);
return "1";
}
@PostMapping("/topic/send")
public String sendTopic(@RequestBody String message){
this.sendMessage(topic,message);
return "1";
}
/**
* 发送消息 destination是发送到的队列,message是待发送的消息
*/
private void sendMessage(Destination destination,String message){
jmsMessagingTemplate.convertAndSend(destination,message);
}
Queue模式消费者
@JmsListener(destination = "${spring.activemq.queue-name}",containerFactory = "queueListener")
public void readQueueMessage(String message){
System.out.println("queue收到:"+message);
}
Topic模式消费者
@JmsListener(destination = "${spring.activemq.topic-name}" ,containerFactory = "topicListener")
public void readTopicMessage(String message){
System.out.println("收到Topic消息:"+message);
}
代码getee地址:https://gitee.com/jiang_li_bin/activemq