您的位置:首页 > 编程语言 > Java开发

SpringBoot RabbitMq集成

2017-03-24 00:00 585 查看
参考文档:

http://projects.spring.io/spring-boot/

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

1、配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=test
spring.rabbitmq.virtual-host=test

2、代码

@SpringBootApplication // 自动配置
@RabbitListener(queues = "foo") // 监听foo队列
@EnableScheduling // 开启定时任务
public class SampleAmqpSimpleApplication {
// 类似xml 定义 send bean
@Bean
public Sender mySender() {
return new Sender();
}
// 类似xml 定义 队列
@Bean
public Queue fooQueue() {
return new Queue("foo");
}
// 消费者处理
@RabbitHandler
public void process(@Payload String foo) {
System.out.println(new Date() + ": " + foo);
}
// 程序入口
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleAmqpSimpleApplication.class, args);
}

}

生产者

public class Sender {

@Autowired
private RabbitTemplate rabbitTemplate;

@Scheduled(fixedDelay = 1000L)
public void send() {
this.rabbitTemplate.convertAndSend("foo", "hello");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: