我在基于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://guest@127.0.0.1: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 - 检索Consumer的传递:tags = [{amq.ctag-utTZhTES5HFCGizQGiFWqg = tx.mt.sessionstats.recvq}],channel = Cached Rabbit Channel:AMQChannel(amqp:/ /guest@127.0.0.1:5672/,3),acknowledgeMode = AUTO本地队列大小= 0 DEBUG:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer - 消费者标签的收到关机信号= amq.ctag-utTZhTES5HFCGizQGiFWqg com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#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) 在com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) 在com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) at com.rabbitmq.client.impl.AMQConnection $ MainLoop.run(AMQConnection.java:554) 在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],
答案 0 :(得分:0)
在 catch 块中添加异常日志记录(永远不会捕获异常,而不会在catch块中执行任何有用的操作),并在 try 块的最后一行添加日志记录。
修改强>
我认为对 channel.basicAck 的调用可能是多余的,请尝试删除它。