activemq 与 spring boot 整合
1、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <exclusions> <exclusion> <artifactId>logback-core</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> <exclusion> <artifactId>logback-classic</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> </exclusions> </dependency>
2、配置文件
spring: activemq: broker-url: tcp://localhost:61616 user: admin password: admin in-memory: true pool: enabled: false
3、消息发送服务
@Service public class ActiveMQProducer { @Autowired private JmsTemplate jmsTemplate; /** * 实时投递消息 * @param destination * @param message */ public void sendMessage (Destination destination, final String message) { this.jmsTemplate.convertAndSend(destination, message); } }
4、发送消息
@RestController @RequestMapping("/idle") public class IdleController { @Autowired private ActiveMQProducer activeMQProducer; @RequestMapping(value = "/add", method = RequestMethod.POST) public Object add (){ Destination demon = new ActiveMQQueue("demon"); this.activeMQProducer.sendMessage(demon, "message"); } }
5、添加监听
@JmsListener(destination = "demon") public void imUser (String message) { System.out.println(message); }