转载自:https://www.cnblogs.com/skyessay/p/6928933.html

demo目录

【转载】Springboot使用rabbitmq

贴代码

1.ProducerConfig.java

package com.test.config;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by admin on 2017/6/1 13:23.
 */
@Configuration
public class ProducerConfig {
    @Bean
    public RabbitMessagingTemplate msgMessageTemplate(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 定义交换机,参数分别是:1.交换器名称(default.topic 为默认值),2.是否长期有效,3.如果服务器在不再使用时自动删除交换器
        TopicExchange exchange = new TopicExchange("default.topic", true, false);
        rabbitAdmin.declareExchange(exchange);
        // 定义队列,参数分别是:1.队列名称,2.声明一个持久队列,3.声明一个独立队列,4.如果服务器在不再使用时自动删除队列
        Queue queue = new Queue("test.demo.send", true, false, false);
        rabbitAdmin.declareQueue(queue);
        // 定义队列绑定到交换机再关联到路由,参数分别是:1.queue:绑定的队列,2.exchange:绑定到那个交换器,3.test2.send:绑定的路由名称
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test2.send"));
        return RabbitUtil.simpleMessageTemplate(connectionFactory);
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2021-11-23
  • 2022-02-23
  • 2021-06-08
  • 2021-08-11
  • 2021-05-15
  • 2021-04-05
  • 2022-12-23
猜你喜欢
  • 2021-12-10
  • 2021-06-05
  • 2021-07-17
  • 2021-09-29
  • 2021-09-02
  • 2021-09-14
相关资源
相似解决方案