gpt4 book ai didi

java - 在 RabbitMQ 中发布时提供不正确(不存在)的队列名称

转载 作者:行者123 更新时间:2023-12-02 11:19:35 27 4
gpt4 key购买 nike

我正在使用 AyncRabbitTemplate 来发布消息。发布时提供不正确的(不存在的)队列名称 - 它会默默地删除消息。

我尝试在 AyncRabbitTemplate 上启用“确认”和“授权”,并添加了所需的回调方法,如下所示:

@Bean
AsyncRabbitTemplate template() {
RabbitTemplate rabbit = rabbitTemplate();
rabbit.setChannelTransacted(true); //to throw error when channel shuts down in case of incorrect exchange names

AsyncRabbitTemplate asyncRabbitTemplate = new AsyncRabbitTemplate(rabbit, rpcReplyMessageListenerContainer(connectionFactory()));
asyncRabbitTemplate.setEnableConfirms(true);
asyncRabbitTemplate.setMandatory(true); //if the message cannot be delivered to a queue an AmqpMessageReturnedException will be thrown
return asyncRabbitTemplate;
}

@Bean
public SimpleMessageListenerContainer rpcReplyMessageListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(connectionFactory);
simpleMessageListenerContainer.setQueueNames(Constants.REPLY_QUEUE);
simpleMessageListenerContainer.setTaskExecutor(Executors.newCachedThreadPool());
simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
return simpleMessageListenerContainer;
}

@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
return connectionFactory;
}

回调方法为:

RabbitConverterFuture<String> future = this.asyncRabbitTemplate.convertSendAndReceive("",Constants.SNS_QUEUE, "This is the request message ",new MessagePostProcessor() {

@Override
public Message postProcessMessage(Message message) {
message.getMessageProperties().setTimestamp(new Date());
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
return message;
}
});


ListenableFuture<Boolean> future2 = future.getConfirm();
future2.addCallback(new ListenableFutureCallback<Boolean>() {

@Override
public void onSuccess(Boolean result) {
System.out.println("Publish Result " + result);

}

@Override
public void onFailure(Throwable ex) {
System.out.println("Publish Failed: " + ex);

}
});

最佳答案

the documentation 中所述您必须在连接工厂上启用返回消息。

发送不会引发异常,但无法送达的消息将返回到 ReturnCallback(如果强制为 true)。

对于无法送达的消息,不会发送确认。仅当经纪商存在某种问题时才会收到否定确认;它们很罕见。

关于java - 在 RabbitMQ 中发布时提供不正确(不存在)的队列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50028533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com