Spring Boot基礎教程22-使用異步消息服務-AMQP(RabbitMQ)

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");

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章