1.下载并安装activemq服务(windows),下载地址:http://download.csdn.net/download/loveuserzzz/9938202
2.创建springboot工程,并引入依赖
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.0</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>3.编写生产者代码实现
@Component
public class Producer implements CommandLineRunner{
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Override
public void run(String... args) throws Exception {
send("Sample message");
System.out.println("Message was sent to the Queue");
}
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
}
4.编写客户端实现
@Component
public class Consumer {
@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println(text);
}
}
5.编写application.properties
spring.activemq.in-memory=true spring.activemq.pool.enabled=false6.启动activemq服务
7.启动main运行查看结果
下载源码:https://github.com/yifanzzz/springboot-mq.git
示例中包含activemq和rabbitmq两部分,可以加群一起讨论QQ:324070788