当前位置: 首页 > article >正文

Springboot结合RabbitMQ

pom.xml

 <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

application.yaml

spring:
  rabbitmq:
    host: 127.0.0.1
    username: guest
    password: guest
    port: 5672
    virtual-host: test_vhost
    connection-timeout: 600000
    # 关闭自动ack,设置成手动ack
    listener:
      simple:
        acknowledge-mode: manual
        prefetch: 10
        concurrency: 2
        max-concurrency: 5

配置文件,也可以使用@PostConstruct

@Configuration
public class RabbitMQConfig {
	/**
	* 绑定
	*/
    public static final String EXCHANGE_NAME="springboot_topic_exchange";
    public static final String QUEUE_NAME="springboot_queue";

    @Bean("exchange")
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    @Bean("queue")
    public Queue queue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean()
    public Binding bindQueueExchange(@Qualifier("exchange")Exchange exchange,
                                     @Qualifier("queue")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
	
   /*
	* Fanout 模式
	*/
	public static final String EXCHANGE_NAME2="my_topic_exchange";
    public static final String QUEUE_NAME2="my_queue";
    //创建交换机
    @Bean
    public FanoutExchange fannoutExchange(){
        return new FanoutExchange(EXCHANGE_NAME2,true,false);
    }

    //创建队列
    @Bean
    public Queue fannoutQueue(){
        return new Queue(QUEUE_NAME2,true,false,false);
    }
    //声明绑定关系
    @Bean
    public Binding bindQueue(@Qualifier("exchange")Exchange exchange,
                                     @Qualifier("queue")Queue queue){
        return BindingBuilder.bind(fannoutQueue()).to(fannoutExchange());
    }
    
   /*
	* topic 模式
	*/
	@Bean
    public Queue topicQueue(){
        return new Queue(QUEUE_NAME,true,false,false);
    }
    //创建交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(EXCHANGE_NAME,true,false);
    }
    //声明绑定关系
    @Bean
    public Binding bindTopicQueue(){
        return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("yuyang.#");
    }


消费者代码

@Component
@Slf4j
public class MessageListener  {
	@RabbitListener(queues="springboot_queue")
    public void process(Message message) {
        log.info("helloWorld模式 received message : {}",new String(message.getBody()));
    }


}

生产者代码

	@Autowired
    private RabbitTemplate rabbitTemplate;

    // pub/sub 发布订阅模式   交换机类型 fanout
    @GetMapping("/fanoutSend")
    public String fanoutSend() throws AmqpException, UnsupportedEncodingException {
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitMQService.sendMessage("my_topic_exchange","*","fanoutSend");
        return "message sended : "+message;
    }

	//topic 工作模式   交换机类型 topic
    @GetMapping("/topicSend")
    public String topicSend() throws AmqpException, UnsupportedEncodingException {
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitTemplate.convertAndSend("spring_topic_exchange","yuyang.test","sadedf");
        return "ok";
    }

手动ack

	@RabbitListener(queues = "springboot_queue")
    public void listenerQueue(Message message, Channel channel) throws IOException {
         log.info(new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

http://www.kler.cn/news/328934.html

相关文章:

  • 经典文献阅读之--Stereo-NEC(全新双目VIO初始化)
  • web前端-CSS引入方式
  • Vue3 工具函数(总结)
  • Python和QT哪个更适合嵌入式方向的上位机开发?
  • 【计算机毕业设计】springboot就业信息管理系统
  • Java中的HTTP请求:使用Apache HttpClient
  • python程序操作Windows系统中的软件如word等(是否可以成功操作待验证)
  • 计算机网络实验3——基于TCP的多线程Web Server服务器的实现
  • vue页面保持在div的底部(适用于聊天界面等需要显示最新信息的场景)
  • R包:ggheatmapper热图
  • Postgresql源码(136)syscache/relcache 缓存及失效机制
  • 【数据结构】环形队列(循环队列)学习笔记总结
  • 技术人生-电脑突然卡顿怎么办
  • 滚雪球学Oracle[3.4讲]:事务控制与锁管理
  • Vite:为什么选 Vite
  • 22.4k star,好用、强大的链路监控软件,skywalking
  • gcc选项-fno-access-control 使用
  • redis中的数据类型(Set与ZSet)
  • pre-commit 的配置文件
  • c++primier第十二章类和动态内存
  • Flink 性能优化的高频面试题及答案
  • 【redis-03】redis缓存穿透、缓存击穿、缓存雪崩
  • 平安养老险深圳分公司积极开展“金融教育宣传月”活动,展现金融为民新风尚
  • C++随心记
  • Linux 再入门整理:详解 /etc/fstab 文件
  • diffusion vs GAN
  • HealChat心理大语言模型 丨OPENAIGC开发者大赛高校组AI创作力奖
  • 数据结构-3.7.双端队列
  • 栈(模板)、队列(模板)(9.27)
  • 5分钟精通Excel在go中的使用