RabbitMQ with Spring Boot

Spring项目下有两个项目和RabbitMQ有关:

  • spring-amqp
  • spring-rabbit

其中spring-amqp是基础框架,spring-rabbitspring-amqpRabbitMQ实现。

如果希望在Spring Boot中使用RabbitMQ只需要在pom.xml中使用amqpstarter就可以了。


    org.springframework.boot
    spring-boot-starter-amqp

在配置文件application.properties中添加

mq.rabbit.host=127.0.0.1
mq.rabbit.port=5672
mq.rabbit.virtualHost=/test
mq.rabbit.username=test
mq.rabbit.password=123456

设置一个监听的业务类,实现接口MessageListener

public class Receiver implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println("=================");
        System.out.println(message);
        System.out.println("=================");
    }
}

这里写的比较简单,可以利用Spring提供的SimpleMessageConverter来将Message类转换成对应的业务对象。

使用配置类RabbitMQConfiguration来连接RabbitMQ:

@Configuration
public class RabbitMQConfiguration {

    final static String TEST_QUEUE_NAME = "test_queue";
    final static String TEST_EXCHANGE_NAME = "test_exchange";
    final static String ROUTING_KEY="test";

    // RabbitMQ的配置信息
    @Value("${mq.rabbit.host}")
    private String mqRabbitHost;
    @Value("${mq.rabbit.port}")
    private Integer mqRabbitPort;
    @Value("${mq.rabbit.username}")
    private String mqRabbitUsername;
    @Value("${mq.rabbit.password}")
    private String mqRabbitPassword;
    @Value("${mq.rabbit.virtualHost}")
    private String mqRabbitVirtualHost;
    
    // 建立一个连接容器,类型数据库的连接池。
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory =
                new CachingConnectionFactory(mqRabbitHost, mqRabbitPort);

        connectionFactory.setUsername(mqRabbitUsername);
        connectionFactory.setPassword(mqRabbitPassword);
        connectionFactory.setVirtualHost(mqRabbitVirtualHost);

        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
    
    // RabbitMQ的使用入口
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
    
    // 要求RabbitMQ建立一个队列。
    @Bean
    public Queue myQueue() {
        return new Queue(TEST_QUEUE_NAME);
    }
    
    // 声明一个监听容器
    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, Receiver listenerAdapter) {

        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(new String[]{TEST_QUEUE_NAME});
        container.setMessageListener(listenerAdapter);

        return container;
    }
    
    // 在spring容器中添加一个监听类
    @Bean
    Receiver receiver() {
        return new Receiver();
    }
    // 定义一个直连交换机
    @Bean
    DirectExchange exchange() {
        return new DirectExchange(TEST_EXCHANGE_NAME);
    }
    // 要求队列和直连交换机绑定,指定ROUTING_KEY
    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

}

然后启动Application类,Spring Boot的项目就会一直监听RabbitMQ队列的情况。

你可能感兴趣的:(RabbitMQ with Spring Boot)