什么是ActiveMQ?
ActiveMQ是Apache所提供的一个开源的消息系统,完全采用Java来实现,因此,它能很好地支持J2EE提出的JMS(Java Message Service,即Java消息服务)规范, 更多关于ActiveMQ的知识点这里不做过多讲解, 这里主要讲解Springboot项目中怎么整合ActiveMQ消息队列
Springboot整合ActiveMQ
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
- 配置application.yml
spring:
activemq:
broker-url: tcp://ip:61616
user: admin
password: 123456
jms:
pub-sub-domain: true #是否使用发布订阅模式,默认false,使用点对点
// 队列名
queue: test1
// 主题名
topic:topic1
- 配置类
@Configuration
public class QueueConfig {
@Value("${queue}")
private String queueName;
@Value("${topic}")
private String topicName;
@Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
}
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
}
- 生产者
/**
* 生产者
*/
@Component
public class Producter {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
public void send1(){
jmsMessagingTemplate.convertAndSend(queue, “测试”);
}
public void send2(){
jmsMessagingTemplate.convertAndSend(topic, “测试”);
}
}
- 消费者
/**
* 消费者
*/
@Component
public class Consumer {
@JmsListener(destination = "${queue}")
public void receive1(String msg){
System.out.println(msg);
}
@JmsListener(destination = "${topic}")
public void receive2(String msg){
System.out.println(msg);
}
@JmsListener(destination = "${topic}")
public void receive3(String msg){
System.out.println(msg);
}
}
到这里就配置好了, 有什么不明白的可以留言咨询哦
网友评论