
目录:
- 环境
- ActiveMQ的安装
- Springboot整合ActiveMQ
环境
- MacOS(本篇博客介绍)
- Windows(比较简单,可以看这里)
ActiveMQ的安装
下载apache-activemq
下载地址:https://download.csdn.net/download/qq_34077993/10750953解压并更改相关配置文件
启动ActiveMQ
进入到
bin/macosx目录,启动ActiveMQ./activemq start控制面板登录
在浏览器中输入http://127.0.0.1:8161/admin/
默认用户名密码都为admin
Springboot整合ActiveMQ
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
配置文件
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
queue: kmx.pas.job.sgtest
@Configuration
public class QueueConfig {
@Value("${queue}")
private String queue;
@Bean
public Queue logQueue() {
return new ActiveMQQueue(queue);
}
}
生产者
@Component
@EnableScheduling
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Scheduled(fixedDelay = 5000)
public void send() {
jmsMessagingTemplate.convertAndSend(queue, "测试消息队列" + System.currentTimeMillis());
}
}
消费者
@Component
public class Consumer {
@JmsListener(destination = "${queue}")
public void receive(String msg) {
System.out.println("监听器收到msg:" + msg);
}
}