企业网站开发信息,亳州做网站,兼职做调查哪个网站好,做网站能成功吗我们知道#xff0c;RabbitMQ的消息最终是存储在Queue上的#xff0c;而在Queue之前还要经过Exchange#xff0c;那么这个过程中就有两个地方可能导致消息丢失。第一个是Producer到Exchange的过程#xff0c;第二个是Exchange到Queue的过程。 为了解决这个问题#xff0c…我们知道RabbitMQ的消息最终是存储在Queue上的而在Queue之前还要经过Exchange那么这个过程中就有两个地方可能导致消息丢失。第一个是Producer到Exchange的过程第二个是Exchange到Queue的过程。 为了解决这个问题有两种方案一种是通过confirm机制另外一种是事务机制因为事务机制并不推荐这里先介绍Confirm机制。
Publisher Confirm是一种机制用于确保消息已经被Exchange成功接收和处理。一旦消息成功到达Exchange并被处理RabbitMQ会向消息生产者发送确认信号ACK。如果由于某种原因例如Exchange不存在或路由键不匹配消息无法被处理RabbitMQ会向消息生产者发送否定信号NACK。
//启用Publisher Confirmschannel.confirmSelect();//设置Publisher Confirms回调channel.addConfirmListener(new ConfirmListener() {Overridepublic void handleAck(long deliveryTag, boolean multiple) throws IOException {System.out.println(Message confirmed with deliveryTag:deliveryTag);//在这里处理消息确认}Overridepublic void handleNack(long deliveryTag, boolean multiple) throws IOException {System.out.println(Message not confirmed with deliveryTag:deliveryTag);//在这里处理消息未确认}});Publisher Returns机制与Publisher Confirms类似但用于处理在消息无法路由到任何队列时的情况。当RabbitMQ在无法路由消息时将消息返回给消息生产者但是如果能正常路由则不会返回消息。
//启用Publisher Returnschannel.addReturnListener(new ReturnListener() {Overridepublic void handleReturn(int replyCode, String replyTest, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println(Message returned with replayCode: replyCode);//在这里处理消息发送到Queue失败的返回}});通过以上方式我们注册了两个回调监听用于在消息发送到Exchange或者Queue失败时进行异常处理。通常我们可以在失败时精心报警或者重试来保障一定能发送成功。
完整代码
package com.example.demo.rabbitmq;import com.rabbitmq.client.*;import java.io.IOException;public class PublisherCallbacksExample {public static void main(String[] args) throws Exception{ConnectionFactory factorynew ConnectionFactory();factory.setHost(localhost);try(Connection connectionfactory.newConnection();Channel channelconnection.createChannel()){//启用Publisher Confirmschannel.confirmSelect();//设置Publisher Confirms回调channel.addConfirmListener(new ConfirmListener() {Overridepublic void handleAck(long deliveryTag, boolean multiple) throws IOException {System.out.println(Message confirmed with deliveryTag:deliveryTag);//在这里处理消息确认}Overridepublic void handleNack(long deliveryTag, boolean multiple) throws IOException {System.out.println(Message not confirmed with deliveryTag:deliveryTag);//在这里处理消息未确认}});//启用Publisher Returnschannel.addReturnListener(new ReturnListener() {Overridepublic void handleReturn(int replyCode, String replyTest, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {System.out.println(Message returned with replayCode: replyCode);//在这里处理消息发送到Queue失败的返回}});String exchangeName my_exchange;String routingKey my_routing_key;String message Hello,RabbitMQ!;//发布消息到Exchangechannel.basicPublish(exchangeName,routingKey,true,null,message.getBytes());//等待Publisher Confirmsif (!channel.waitForConfirms()) {System.out.println(Message was not confirmed!);}//关闭通道和连接channel.close();}}
}
另外这里如果发送到Queue之后是否一定能持久化下来是否一定不丢这就是另外一个话题了。 文章转载自: http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.sskhm.cn.gov.cn.sskhm.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.hous-e.com.gov.cn.hous-e.com http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.zwyuan.com.gov.cn.zwyuan.com http://www.morning.hjjhjhj.com.gov.cn.hjjhjhj.com http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.flfxb.cn.gov.cn.flfxb.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.nkyqh.cn.gov.cn.nkyqh.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.llcgz.cn.gov.cn.llcgz.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.ndpzm.cn.gov.cn.ndpzm.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.snmsq.cn.gov.cn.snmsq.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.ffrys.cn.gov.cn.ffrys.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.spbp.cn.gov.cn.spbp.cn