我在基于Spring RabbitMQ的应用程序的消费者端有这个代码 -
public class MyReceiver implements ChannelAwareMessageListener {
public MyReceiver() {
super();
}
@Autowired
@Qualifier("some_name")
private SomeDependency dep;
@Override
public void onMessage(Message message, Channel channel) throws Exception {
try {
String messageString = new String(message.getBody());
if (StringUtils.isNotEmpty(messageString)) {
LOGGER.info("Received Message is:" + messageString);
dep.someMethod();
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
} catch (Exception e) {
}
}
}
当我在try中的第一行放置break时,控制到达断点并继续执行后面的LOGGER.info()调用,但是其他行在大多数时间没有执行,并且控制在其他地方之前移出到达那里。如何确保执行onMessage中的整个代码?
这是控制台输出 -
信息:mypackage.MyReceiver - 收到的消息是:{“有些东西在这里”} DEBUG:org.springframework.amqp.rabbit.connection.CachingConnectionFactory - 关闭缓存频道:AMQChannel(amqp://[email protected]:5672 /, 2)错误:org.springframework.amqp.rabbit.connection.CachingConnectionFactory - 通道关闭:通道错误;协议方法:#method(reply-code = 406,reply-text = PRECONDITION_FAILED - 未知传递标签1,class-id = 60,method-id = 80)DEBUG:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - 检索消费者的交付:tags = [{amq.ctag-utTZhTES5HFCGizQGiFWqg = tx.mt.sessionstats.recvq}],channel = Cached Rabbit Channel:AMQChannel(amqp://[email protected]:5672 /,3),acknowledgeMode = AUTO本地队列大小= 0 DEBUG:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - 消费者标签的接收关闭信号= amq.ctag-utTZhTES5HFCGizQGiFWqg com.rabbitmq.client.ShutdownSignalException:channel error;协议方法:#method(reply-code = 406,reply-text = PRECONDITION_FAILED - 未知传递标签1,class-id = 60,method-id = 80),位于com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN。 java:484)at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:321)at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)at com.rabbitmq.client.impl .AMQChannel.handleFrame(AMQChannel.java:91)at com.rabbitmq.client.impl.AMQConnection $ MainLoop.run(AMQConnection.java:554)at java.lang.Thread.run(Thread.java:745)DEBUG:org .springframework.test.context.support.DirtiesContextTestExecutionListener - 在测试类之后:context [DefaultTestContext @ 4ddced80 testClass = MyReceiverUnitTest,testInstance = [null],testMethod = [null],testException = [null],
在catch块中添加异常日志记录(永远不会捕获异常,而不会在catch块中执行任何有用的操作),并在try块的最后一行添加日志记录。
编辑:
我认为调用channel.basicAck可能是多余的,尝试删除它。