手工签收必须依赖通道
分为消息体和消息头
手工签收有一个basicAck的响应
手工签收模式必须依赖于channel
package com.mfl.demo.utils; import com.mfl.demo.entity.Order; import com.rabbitmq.client.Channel; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.amqp.support.AmqpHeaders; import org.springframework.messaging.handler.annotation.Headers; import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Component; import java.util.Map; @Component public class MessageConsumerUtils { @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "mfl-1-queue",durable = "true"), exchange = @Exchange(name = "mfl-1-exchange", type = "topic"), key = "order.*" )) @RabbitHandler //Payload 指定消息体是order public void receive(@Payload Order order, @Headers Map<String,Object> headers, Channel channel) throws Exception{ System.out.println("-------收到消息,开始消费---------"); System.out.println(order.getName()); Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG); // deliveryTag:该消息的index // multiple:是否批量.true:将一次性ack所有小于deliveryTag的消息。 channel.basicAck(deliveryTag,false); } }
配置文件:
## springboot整合rabbitmq基本配置 spring.rabbitmq.addresses=192.168.254.135:5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/ spring.rabbitmq.connection-timeout=15000 ## springboot整合rabbitmq消费端配置 #线程数 spring.rabbitmq.listener.simple.concurrency=5 #签收模式manual:手工签收 auto自动签收 none不签收 spring.rabbitmq.listener.simple.acknowledge-mode=manual #最大线程数 spring.rabbitmq.listener.simple.max-concurrency=10 #限流,每个线程同一时间只能过来一条消息,消费完了再取下一条 spring.rabbitmq.listener.simple.prefetch=1 server.servlet.context-path=/ server.port=8002
修改编码配置
##springboot整合rabbitmq消费端配置
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10
spring.rabbitmq.listener.simple.ackonwlege-mode=AUTO
spring.rabbitmq.listener.simple.prefetch=1
消费端 配置
rabbitmq的配置
concurrency:连接数
prefetch限流 //prefetch=1 同一时间 只能有一条消息过来
ackonwledge-mode=AUTO //manual手动签收,auto自动签收
rabbitmq的配置
concurrency:连接数
manual手动签收,auto自动签收
prefetch限流
OrderReceiver
Consumer配置
消费端的基本配置