RabbitMQ学习五(Consumer Ack)
一.Consumer Ack
他表示消费端收到消息的确认方式,其提供了三种确认方式:
- 自动确认acknowledge="none":当消费者接收到消息的时候,就会自动给到RabbitMQ一个回执,告诉MQ我已经收到消息了,不在乎消费者接收到消息之后业务处理的成功与否。
-
手动确认acknowledge="manual":当消费者收到消息后,不会立刻告诉RabbitMQ已经收到消息了,而是等待业务处理成功后,通过调用代码的方式手动向MQ确认消息已经收到。当业务处理失败,就可以做一些重试机制,甚至让MQ重新向消费者发送消息都是可以的。
-
根据异常情况确认acknowledge="auto":该方式是通过抛出异常的类型,来做响应的处理(如重发、确认等)。这种方式比较麻烦。
自动确认是指,一旦消息被Consumer接收到,则自动确认收到,并将相应message从RabbitMQ的消息缓存中移除,但是在实际业务处理中,很可能消息接收到,业务处理出现异常,那么该消息就会丢失。如果设置了手动确认方式,则需要在业务处理成功后,调用channel.basicAck(),手动签收,如果出现异常,则调用channel.basicNack()方法,让其自动重新发送消息。
二.Consumer Ack开启
1.手动签收
配置文件
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.virtual-host=/
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--加载配置文件--> <context:property-placeholder location="classpath:rabbitmq.properties"/> <!-- 定义rabbitmq connectionFactory --> <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <context:component-scan base-package="com.itheima.listener" /> <!--定义监听器容器--> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener> </rabbit:listener-container> </beans>
监听器
package com.itheima.listener; import com.rabbitmq.client.Channel; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener; import org.springframework.stereotype.Component; import java.io.IOException; /* * 默认就为自动签收类型 * 实现MessageListener接口 * */ @Component public class AckListener implements MessageListener { @Override public void onMessage(Message message) { System.out.println(new String(message.getBody())); } }
测试类
package com.itheima.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml") public class ConsumerTest { @Test public void test(){ while (true){ } } }
2.手动确认
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--加载配置文件--> <context:property-placeholder location="classpath:rabbitmq.properties"/> <!-- 定义rabbitmq connectionFactory --> <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <context:component-scan base-package="com.itheima.listener" /> <!--定义监听器容器--> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual"> <rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener> <!-- <rabbit:listener ref="qosListener" queue-names="test_queue_confirm"></rabbit:listener>--> <!--定义监听器,监听正常队列--> <!--<rabbit:listener ref="dlxListener" queue-names="test_queue_dlx"></rabbit:listener>--> <!--延迟队列效果实现: 一定要监听的是 死信队列!!!--> <!--<rabbit:listener ref="orderListener" queue-names="order_queue_dlx"></rabbit:listener>--> </rabbit:listener-container> </beans>
监听器
package com.itheima.listener; import com.rabbitmq.client.Channel; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener; import org.springframework.stereotype.Component; import java.io.IOException; /* * Consumer ACK机制: * 1.设置手动签收 acknowledge="manual" * 2.让监听器实现ChannelAwareMessageListener * 3.如果消息成功处理,则调用channel的basicAck()签收 * 4.如果消息处理失败,则调用channel的basicNack()拒绝签收,brocker重新发送给consumer * */ @Component public class AckListener implements ChannelAwareMessageListener { @Override public void onMessage(Message message, Channel channel) throws Exception { Thread.sleep(1000); long deliveryTag = message.getMessageProperties().getDeliveryTag();//当前收到的消息的标签 try{ //1.接受转换消息 System.out.println(new String(message.getBody())); //2.处理业务逻辑 System.out.println("处理业务逻辑"); //3.手动签收 channel.basicAck(deliveryTag,true); } catch (Exception e){ //4.拒绝签收 /* * 第三个参数:requeue:重回队列。设置为true,则消息重新回到queue,broker会重新发送该消息给消费端 * */ channel.basicNack(deliveryTag,true,true); } } }

浙公网安备 33010602011771号