Alex Guo
文章36
标签33
分类10
Spring rabbitMQ入门

Spring rabbitMQ入门

  • 引入rabbitMQ
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-amqp</artifactId>
          </dependency>
    
  • 配置属性
    ```java
    spring.application.name=rabbitmq-hello

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest


- 配置
```java
@Configuration
public class RabbitConfig {
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
}
  • 消息发送者

    @Component
    public class Sender {
    
      @Autowired
      private AmqpTemplate rabbitTemplate;
    
      public void send() {
          String context = "hello " + new Date();
          System.out.println("Sender : " + context);
          this.rabbitTemplate.convertAndSend("hello", context);
      }
    }
    
  • 消息接收者

    @Component
    @RabbitListener(queues = "hello")
    public class Receiver {
    
      @RabbitHandler
      public void process(String hello) {
          System.out.println("Receiver : " + hello);
      }
    }
    
本文作者:Alex Guo
本文链接:https://alexguo.net/2018/12/05/Spring-rabbitMQ%E5%85%A5%E9%97%A8/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可