rabbitmq中confirm模式可以通过yml文件中的publisher-confirm-type: correlated设置吗?
时间: 2024-02-16 20:04:14 浏览: 31
是的,可以通过在yml文件中设置`publisher-confirm-type: correlated`来启用RabbitMQ中的Confirm模式。在Spring Boot中,可以通过在application.yml文件中添加如下配置,来启用Confirm模式:
```yaml
spring:
rabbitmq:
publisher-confirm-type: correlated
```
这将会启用`Correlated`类型的Confirm模式,当消息成功发送到队列时,会调用`ConfirmCallback`接口的`confirm`方法,否则调用`ConfirmCallback`接口的`confirm`方法。同时,如果RabbitMQ Broker在发送消息时遇到了错误,那么会调用`ConfirmCallback`接口的`handleNack`方法。需要注意的是,启用Confirm模式会降低消息发送的速度,但可以保证消息的可靠性和一致性。
相关问题
rabbitmq中confirm模式通过yml文件中的publisher-confirm-type: correlated设置后,还需要调用confirmSelect()方法吗
是的,即使在yml文件中设置了`publisher-confirm-type: correlated`来启用了RabbitMQ中的Confirm模式,也需要在发送消息前调用`confirmSelect()`方法来开启Confirm模式。在Spring AMQP中,可以通过在`RabbitTemplate`中设置`confirmCallback`和`returnCallback`来监听Confirm消息的返回结果和返回失败的消息,同时在需要发送消息的时候,调用`confirmSelect()`方法来开启Confirm模式。例如:
```java
@Configuration
public class RabbitConfig {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void init() {
rabbitTemplate.setConfirmCallback(new ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
// Confirm消息发送成功或失败后的回调处理
}
});
rabbitTemplate.setReturnCallback(new ReturnCallback() {
@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
// Confirm消息发送失败后的回调处理
}
});
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmSelect();
}
}
```
需要注意的是,在使用Confirm模式时,需要设置`mandatory`为`true`,否则当消息发送失败时,不会调用`ReturnCallback`接口的方法。同时,在`RabbitTemplate`中需要设置`confirmCallback`和`returnCallback`来监听Confirm消息的返回结果和返回失败的消息。
rabbitmq中confirm消息没确认怎么设置重试
在 RabbitMQ 中,确认消息的方式有两种:confirm 模式和事务模式。如果你使用的是 confirm 模式,可以设置消息的确认超时时间,如果消息在超时时间内没有被确认,则可以进行重试。
具体来说,可以使用 `channel.confirm_select()` 方法开启 confirm 模式,并使用 `add_on_return_callback()` 方法为未确认的消息设置回调函数。在回调函数中,可以根据需要进行消息的重发或其他操作。
下面是一个简单的示例代码:
```
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# 开启 confirm 模式
channel.confirm_select()
# 设置未确认消息的回调函数
def on_return(channel, method, properties, body):
print("Message returned: %s" % body)
# 进行重试或其他操作
channel.add_on_return_callback(on_return)
# 发送消息
channel.basic_publish(exchange='', routing_key='test', body='Hello, world!', mandatory=True)
# 等待消息确认
if not channel.wait_for_confirmation():
# 消息未被确认,进行重试或其他操作
pass
connection.close()
```
在上面的代码中,我们使用 `mandatory=True` 将消息标记为必须路由到队列中,如果无法路由则会返回给生产者。如果消息被返回,则会触发回调函数 `on_return()` 进行处理。在 `wait_for_confirmation()` 方法中等待消息的确认,如果在超时时间内消息未被确认,则会返回 False,此时可以进行重试或其他操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)