黑马程序员MQ 学习笔记
https://www.rabbitmq.com/
https://www.rabbitmq.com/getstarted.html
RabbitMQ高级特性
Producer消息可靠性投递
confirm 确认模式
当生产的消息到exchange会返回一个confirmCallbac
k;
开启confirmCallback
<!--
confirm-type
- NONE 禁用发布确认模式,是默认值
- CORRELATED 值是发布消息成功到交换器后会触发回调方法(异步方式)
- SIMPLE 待探究
-->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"
confirm-type="CORRELATED"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
<rabbit:queue id="test_queue_confirm" name="test_queue_confirm" auto-declare="true" />
<rabbit:direct-exchange id="test_exchange_confirm" name="test_exchange_confirm" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="test_queue_confirm" key="confirm"/>
</rabbit:bindings>
</rabbit:direct-exchange>
编写测试类
@Test
public void testConfirm(){
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback(){
/**
*
* @param correlationData 相关配置信息
* @param ack 交换机是否成功收到消息
* @param cause 失败的原因
*/
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
System.out.println("confirm被调用了.");
System.out.println(correlationData);
if(ack){
System.out.println("success:"