我目前正在与故障的情况下通过消息中间件的RabbitMQ通信时可能发生的实验。 我们的目标是评估通信怎么这么能做出更有弹性。
我特别希望在发送邮件时触发NACK(未确认)确认生产者提交模式 。 要做到这一点,我将消息发送到通过Spring AMQP的一个不存在的交换RabbitTemplate.send
。 在经由设置回调RabbitTemplate.setConfirmCallback
,我然后处理ack=false
通过重新发送该消息到现有的交流(模拟我照顾到NACK原因的)确认。
下面提供了一个示例类和相关测试,完整的示例项目中可以找到我的github仓库 。 我使用的RabbitMQ 3.6和Spring启动/ AMQP 2.0.2。
运行测试时,回调调用与ack=false
预期。 然而,重新发送该消息挂起,而重新创建(10分钟后用超时异常)的信道。 电话会议的转储的堆栈和下面提供日志。
一个解决问题的办法似乎是在不同的线程发送消息的建议在这里 。 如果您取消注释行service.runInSeparateThread = true;
在测试中,工作的事情!
然而,我既不忠实地理解事情为什么(不)工作,我也没有读到这种做法除了上述职位的任何地方。 这是预期的行为或错误? 有人可以解释的细节?
非常感谢您的咨询!
调用堆栈快照 :
"AMQP Connection 127.0.0.1:5672@3968" prio=5 tid=0xe nid=NA waiting
java.lang.Thread.State: WAITING
at java.lang.Object.wait(Object.java:-1)
at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:73)
at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:494)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:288)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:138)
at com.rabbitmq.client.impl.ChannelN.open(ChannelN.java:133)
at com.rabbitmq.client.impl.ChannelManager.createChannel(ChannelManager.java:176)
at com.rabbitmq.client.impl.AMQConnection.createChannel(AMQConnection.java:542)
at org.springframework.amqp.rabbit.connection.SimpleConnection.createChannel(SimpleConnection.java:57)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$ChannelCachingConnectionProxy.createBareChannel(CachingConnectionFactory.java:1156)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$ChannelCachingConnectionProxy.access$200(CachingConnectionFactory.java:1144)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.doCreateBareChannel(CachingConnectionFactory.java:585)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createBareChannel(CachingConnectionFactory.java:568)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getCachedChannelProxy(CachingConnectionFactory.java:538)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getChannel(CachingConnectionFactory.java:520)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.access$1500(CachingConnectionFactory.java:94)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$ChannelCachingConnectionProxy.createChannel(CachingConnectionFactory.java:1161)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1803)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1771)
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:859)
...
日志 :
...
10:21:24.613 [main] DEBUG org.springframework.amqp.rabbit.core.RabbitAdmin - declaring Exchange 'ExistentExchange'
10:21:24.630 [main] INFO com.example.rabbitmq.ProducerService - sending `initial Message`
10:21:24.648 [main] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - Added listener org.springframework.amqp.rabbit.core.RabbitTemplate$MockitoMock$952329793@562c877a
10:21:24.648 [main] DEBUG org.springframework.amqp.rabbit.core.RabbitTemplate - Added publisher confirm channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3013909b Shared Rabbit Connection: SimpleConnection@12db3386 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 1341] to map, size now 1
10:21:24.649 [main] DEBUG org.springframework.amqp.rabbit.core.RabbitTemplate - Executing callback RabbitTemplate$$Lambda$175/1694519286 on RabbitMQ Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1), conn: Proxy@3013909b Shared Rabbit Connection: SimpleConnection@12db3386 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 1341]
10:21:24.649 [main] DEBUG org.springframework.amqp.rabbit.core.RabbitTemplate - Publishing message (Body:'[B@67001148(byte[15])' MessageProperties [headers={}, contentType=application/octet-stream, contentLength=0, deliveryMode=PERSISTENT, priority=0, deliveryTag=0])on exchange [nonExistentExchange], routingKey = [nonExistentQueue]
10:21:24.659 [main] INFO com.example.rabbitmq.ProducerService - done with sending message
10:21:24.675 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1) PC:Nack:(close):1
10:21:24.677 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - Sending confirm PendingConfirm [correlationData=null cause=channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'nonExistentExchange' in vhost '/', class-id=60, method-id=40)]
10:21:24.677 [AMQP Connection 127.0.0.1:5672] INFO com.example.rabbitmq.ProducerService - In confirm callback, ack=false, cause=channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'nonExistentExchange' in vhost '/', class-id=60, method-id=40), correlationData=null
10:21:24.677 [AMQP Connection 127.0.0.1:5672] INFO com.example.rabbitmq.ProducerService - sending `resend Message`
10:21:24.678 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1) PC:Nack:(close):1
10:21:24.679 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - AMQChannel(amqp://guest@127.0.0.1:5672/,1) No listener for seq:1
10:21:24.679 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.core.RabbitTemplate - Removed publisher confirm channel: PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1) from map, size now 0
10:21:24.679 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.core.RabbitTemplate - Removed publisher confirm channel: PublisherCallbackChannelImpl: AMQChannel(amqp://guest@127.0.0.1:5672/,1) from map, size now 0
10:21:24.679 [AMQP Connection 127.0.0.1:5672] DEBUG org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl - PendingConfirms cleared
ProducerService:
@Service
public class ProducerService {
static final String EXISTENT_EXCHANGE = "ExistentExchange";
private static final String NON_EXISTENT_EXCHANGE = "nonExistentExchange";
private static final String QUEUE_NAME = "nonExistentQueue";
private final Logger logger = LoggerFactory.getLogger(getClass());
private final RabbitTemplate rabbitTemplate;
private final Executor executor = Executors.newCachedThreadPool();
boolean runInSeparateThread = false;
public ProducerService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
rabbitTemplate.setConfirmCallback(this::confirmCallback);
}
private void confirmCallback(CorrelationData correlationData, boolean ack, String cause) {
logger.info("In confirm callback, ack={}, cause={}, correlationData={}", ack, cause, correlationData);
if (!ack) {
if (runInSeparateThread) {
executor.execute(() -> sendMessage("resend Message", EXISTENT_EXCHANGE));
} else {
sendMessage("resend Message", EXISTENT_EXCHANGE);
}
} else {
logger.info("sending was acknowledged");
}
}
public void produceMessage() {
sendMessage("initial Message", NON_EXISTENT_EXCHANGE);
}
private void sendMessage(String messageBody, String exchangeName) {
logger.info("sending `{}`", messageBody);
rabbitTemplate.send(exchangeName, QUEUE_NAME, new Message(messageBody.getBytes(), new MessageProperties()));
logger.info("done with sending message");
}
}
ProducerServiceTest:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {RabbitAutoConfiguration.class, ProducerService.class})
@DirtiesContext
public class ProducerServiceTest {
@Autowired
private ProducerService service;
@SpyBean
private RabbitTemplate rabbitTemplate;
@Autowired
private AmqpAdmin amqpAdmin;
@Autowired
private CachingConnectionFactory cachingConnectionFactory;
@Before
public void setup() {
cachingConnectionFactory.setPublisherConfirms(true);
amqpAdmin.declareExchange(new DirectExchange(ProducerService.EXISTENT_EXCHANGE));
}
@After
public void cleanup() {
amqpAdmin.deleteExchange(ProducerService.EXISTENT_EXCHANGE);
}
@Test
public void sendMessageToNonexistentExchange() throws InterruptedException {
final CountDownLatch sentMessagesLatch = new CountDownLatch(2);
final List<Message> sentMessages = new ArrayList<>();
doAnswer(invocation -> {
invocation.callRealMethod();
sentMessages.add(invocation.getArgument(2));
sentMessagesLatch.countDown();
return null;
}).when(rabbitTemplate).send(anyString(), anyString(), any(Message.class));
// service.runInSeparateThread = true;
service.produceMessage();
sentMessagesLatch.await();
List<String> messageBodies = sentMessages.stream().map(message -> new String(message.getBody())).collect(toList());
assertThat(messageBodies, equalTo(Arrays.asList("initial Message", "resend Message")));
}
}