RabbitMQ下載地址:http://www.rabbitmq.com/download.html
erlang 下載地址:http://www.erlang.org/downloads
- 添加依賴
<!-- amqp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 配置文件
# RABBIT (RabbitProperties)
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.password=
#spring.rabbitmq.username=
- 代碼實現
1.啓用註解: @EnableRabbit
2.配置
/**
* amqp 隊列配置
*
* @author wujing
*/
@Configuration
public class AmqpConfiguration {
@Bean
public Queue queue() {
return new Queue("roncoo.queue");
}
}
3.
/**
*
* @author wujing
*/
@Component
public class RoncooAmqpComponent {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String msg) {
this.amqpTemplate.convertAndSend("roncoo.queue", msg);
}
@RabbitListener(queues = "roncoo.queue")
public void receiveQueue(String text) {
System.out.println("接受到:" + text);
}
}
- 測試
@Autowired
private RoncooAmqpComponent roncooAmqpComponent;
@Test
public void send() {
roncooAmqpComponent.send("hello world2");
}