spring boot 整合 amqp

作者: 就怕是个demo | 来源:发表于2017-03-01 16:26 被阅读416次

    安装RabbitMQ server

    进入解压得到的文件夹中的sbin目录

      执行 ./rabbitmq-server
      按CRTL+C关闭启动的server
      ./rabbitmq-plugins enable rabbitmq_management
      ./rabbitmq-server
      通过浏览器打开[http://localhost:15672/](http://localhost:15672/)   
      用户名和密码 是 guest/guest
    

    安装RabbtMQ Server操作详细见http://blog.csdn.net/mrzhangxl/article/details/53114616

    编写代码

    • 先看一下项目目录
    image.png

    编写amqp-sender

    • 在pom.xml中引入amqp依赖包

      <dependencies>
        <!-- ampq 依赖包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
      </dependencies>
      
    • 配置application.yml

      spring:
        rabbitmq:
            host: localhost
            port: 5672
            username: guest
            password: guest
      
    • 编写amqp-sender 配置类

      @Configuration
      public class RabbitMQConfig {
        public static final String QUEUE_NAME = "spring-boot";
        public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";
      
        @Bean
        public Queue queue() {
          // 是否持久化
          boolean durable = true;
          // 仅创建者可以使用的私有队列,断开后自动删除
          boolean exclusive = false;
          // 当所有消费客户端连接断开后,是否自动删除队列
          boolean autoDelete = false;
          return new Queue(QUEUE_NAME, durable, exclusive, autoDelete);
        }
      
        @Bean
        public TopicExchange exchange() {
          // 是否持久化
          boolean durable = true;
          // 当所有消费客户端连接断开后,是否自动删除队列
          boolean autoDelete = false;
          return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
        }
      
        @Bean
        public Binding binding(Queue queue, TopicExchange exchange) {
          return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
        }
      }
      
    • 编写sender消息发送功能类

      @Service
      public class Sender {
        @Autowired
        private AmqpTemplate rabbitTemplate;
      
        public void send() {
            System.out.println("ELSE 发送消息...");
            rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, "你好, ELSE!");
        }
      }
      
    • 编写amqp-sender测试类

      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class AmqpSenderApplicationTests {
      
        @Autowired
        private Sender sender;
        @Test
        public void send() throws Exception {
          sender.send();
        }
      }
      

    编写amqp-receiver

    • 配置application.yml

      server:
        port: 8081
        tomcat:
          uri-encoding: UTF-8
      
      spring:
        rabbitmq:
          host: localhost
          port: 5672
          username: guest
          password: guest
      
    • 编写amqp-receiver 配置类

      @Configuration
      public class RabbitMQConfig {
          public static final String QUEUE_NAME = "spring-boot";
      
           @Bean
           SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
              SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
              container.setConnectionFactory(connectionFactory);
              container.setQueueNames(QUEUE_NAME);
              container.setMessageListener(listenerAdapter);
              return container;
          }
      
          @Bean
          MessageListenerAdapter listenerAdapter(Receiver receiver) {
              return new MessageListenerAdapter(receiver, "receiveMessage");
          }
      }
      
    • 编写receiver消息接收功能类

      @Service
      public class Receiver {
      
        @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
        public void receiveMessage(String message) {
            System.out.println("Received <" + message + ">");
        }
      }
      

    代码地址:https://git.oschina.net/dream-maker/amqp-demo
    更多相关文章:http://www.tuicool.com/articles/vYNVV3I

    相关文章

      网友评论

        本文标题:spring boot 整合 amqp

        本文链接:https://www.haomeiwen.com/subject/nrfcgttx.html