springboot整合rabbitmq DEMO

springboot整合rabbitmq

maven依赖:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>````
##配置:
配置队列、交换器、队列绑定、消息监听容器等

```/**
 * Created by Torres on 17/1/21.
 */
@Configuration
public class RabbitmqConfig {
    @Autowired
    Environment          environment;
    private final String name = "helloworld";

    @Bean
    Queue queue() {
        return new Queue(name, false);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange("hello-exchange");
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).withQueueName();
    }

    @Bean
    SimpleMessageListenerContainer simpleMessageListenerContainer(MessageListenerAdapter listenerAdapter,
                                                                  @Qualifier("simpleConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setMessageListener(listenerAdapter);
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(name);
        return container;
    }

    @Bean(name = "simpleConnectionFactory")
    ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(environment.getProperty("rabbitmq.addresses"));
        connectionFactory.setUsername(environment.getProperty("rabbitmq.username"));
        connectionFactory.setPassword(environment.getProperty("rabbitmq.password"));
        connectionFactory.setVirtualHost(environment.getProperty("rabbitmq.vhost"));
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    Receiver receiver() {
        return new Receiver();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "processMessage");
    }

    @Bean(name = "myRabbitTemplate")
    RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
}```
##消息接受处理

**

  • Created by Torres on 17/1/21.
    */
    public class Receiver {
    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void processMessage(String message) {
    System.out.println(new Date() + "receive message is:" + message);
    countDownLatch.countDown();
    }

    public CountDownLatch getCountDownLatch() {
    return countDownLatch;
    }
    }```

消息发送:

这里使用定时任务每个五秒发送message.


/**
 * Created by Torres on 17/1/21.
 */
@EnableScheduling
//任务调度
@Component
public class Worker {
    @Autowired
    @Qualifier("myRabbitTemplate")
    RabbitTemplate rabbitTemplate;

    @Scheduled(fixedDelay = 5000)
    public void sendMessage() {
        rabbitTemplate.convertAndSend("helloworld", "hello");
    }
}```
##结果:

Sat Jan 21 16:18:00 CST 2017:hello
Sat Jan 21 16:18:03 CST 2017receive message is:hello
Sat Jan 21 16:18:03 CST 2017:hello
Sat Jan 21 16:18:06 CST 2017:hello
Sat Jan 21 16:18:08 CST 2017receive message is:hello
Sat Jan 21 16:18:09 CST 2017:hello
Sat Jan 21 16:18:12 CST 2017:hello
Sat Jan 21 16:18:13 CST 2017receive message is:hello



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,282评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,227评论 6 342
  • 关于消息队列,从前年开始断断续续看了些资料,想写很久了,但一直没腾出空,近来分别碰到几个朋友聊这块的技术选型,是时...
    预流阅读 586,422评论 51 787
  • 注:这份文档是我和几个朋友学习后一起完成的。 目录 RabbitMQ 概念 exchange交换机机制什么是交换机...
    Mooner_guo阅读 33,584评论 8 97
  • 之前刚来到didimax的时候,早上起床就想到今天要整合要熨衣服还要调陈列,一想到这些就感觉好烦,怎么总是干不完的...
    Ding欣欣阅读 1,657评论 0 0

友情链接更多精彩内容