3. \u6295\u9012\u5931\u8d25\u7684\u5904\u7406\u673a\u5236<\/strong><\/div>\n\u5bf9\u5e94\u4e0a\u9762\u7684\u4e24\u79cd\u60c5\u51b5\uff0cRabbitMQ\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n
ConfirmCallback<\/p>\n
RabbitMQ\u63d0\u4f9b\u4e86ConfirmCallback\u63a5\u53e3\u7528\u4e8e\u5b9e\u73b0\u6d88\u606f\u53d1\u9001\u5230RabbitMQ\u4ea4\u6362\u5668\u540e\u8fdb\u884c\u786e\u8ba4\u56de\u8c03\u3002<\/p>\n
\u5728Spring Boot\u4e2d\u9700\u8981\u5f00\u542f\uff1a<\/p>\n
\r\n spring: \r\n rabbitmq: \r\n # \u901a\u5e38\u9009\u62e9 correlated \r\n publisher-confirm-type: \r\n<\/pre>\n\u901a\u5e38\u6709\u4e09\u79cd\u9009\u62e9\uff1a<\/p>\n
NONE \uff0c\u7981\u7528\u53d1\u5e03\u786e\u8ba4\u6a21\u5f0f\uff0c\u662f\u9ed8\u8ba4\u503c\u3002
\n CORRELATED\uff0c\u53d1\u5e03\u6d88\u606f\u65f6\u4f1a\u643a\u5e26\u4e00\u4e2aCorrelationData\uff0c\u88aback\/nack\u65f6CorrelationData\u4f1a\u88ab\u8fd4\u56de\u8fdb\u884c\u5bf9\u7167\u5904\u7406\uff0cCorrelationData\u53ef\u4ee5\u5305\u542b\u6bd4\u8f83\u4e30\u5bcc\u7684\u5143\u4fe1\u606f\u8fdb\u884c\u56de\u8c03\u903b\u8f91\u7684\u5904\u7406\u3002
\n SIMPLE\uff0c\u5f53\u88aback\/nack\u540e\u4f1a\u7b49\u5f85\u6240\u6709\u6d88\u606f\u88ab\u53d1\u5e03\uff0c\u5982\u679c\u8d85\u65f6\u4f1a\u89e6\u53d1\u5f02\u5e38\uff0c\u751a\u81f3\u5173\u95ed\u8fde\u63a5\u901a\u9053\u3002<\/p>\n
\u8fd9\u91cc\u6211\u4f7f\u7528CORRELATED\u6a21\u5f0f\uff0c\u58f0\u660e\u4e00\u4e2aConfirmCallback\u5e76\u8bbe\u7f6e\u5230RabbitTemplate\u4e2d<\/p>\n
\r\n rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> { \r\n \/\/ correlationData \u53ef\u80fd\u4e3a\u7a7a \r\n if (ack) { \r\n log.debug(\"\u6d88\u606f\u53d1\u9001\u5230exchange\u6210\u529f,id: {}\", correlationData.getId()); \r\n } else { \r\n log.debug(\"\u6d88\u606f\u53d1\u9001\u5230exchange\u5931\u8d25,\u539f\u56e0: {}\", cause); \r\n } \r\n }); \r\n<\/pre>\n\u5f53\u6d88\u606f\u6295\u9012\u5230\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u4ea4\u6362\u673aExchange\u4e14ack=false\u65f6\u4f1a\u8f93\u51fa\u65e5\u5fd7\uff1a<\/p>\n
\r\n - Publishing message [(Body:'\"hello\"' MessageProperties [headers={spring_listener_return_correlation=a088eb3f-a234-4e15-bb7a-3aa9a6f043e6, spring_returned_message_correlation=29975bc1-f363-4e3a-85ca-010d13888720, __TypeId__=java.lang.String}, contentType=application\/json, contentEncoding=UTF-8, contentLength=7, deliveryMode=PERSISTENT, priority=0, deliveryTag=0])] on exchange [DIRECT_EXCHANGE1], routingKey = [DIRECT_ROUTING_KEY2] \r\n \r\n - \u6d88\u606f\u53d1\u9001\u5230exchange\u5931\u8d25,\u539f\u56e0: channel error; protocol method: #method(reply-code=404, reply-text=NOT_FOUND - no exchange 'DIRECT_EXCHANGE1' in vhost 'my_vhost', class-id=60, method-id=40) \r\n<\/pre>\n\u8fd9\u91cc\u5b9e\u73b0\u7684\u6bd4\u8f83\u7b80\u5355\u4f60\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u6d88\u606f\u6295\u9012\u5230\u4ea4\u6362\u673a\u5931\u8d25\u540e\u7684\u64cd\u4f5c\u5904\u7406\u903b\u8f91\u3002<\/p>\n
ReturnCallback<\/p>\n
ReturnCallback\u63a5\u53e3\u7528\u4e8e\u5b9e\u73b0\u6d88\u606f\u5df2\u7ecf\u6210\u529f\u53d1\u9001\u5230RabbitMQ\u4ea4\u6362\u673a\uff0c\u4f46\u6ca1\u6709\u5339\u914d\u5230\u961f\u5217\u65f6\u7684\u56de\u8c03\u3002<\/p>\n
\u5728Spring Boot\u4e2d\u9700\u8981\u540c\u65f6\u5f00\u542f\uff1a<\/p>\n
\r\n spring: \r\n rabbitmq: \r\n publisher-returns: true \r\n template: \r\n mandatory: true \r\n<\/pre>\nRabbitTemplate\u4e2d\u7684mandatory\u8bbe\u7f6e\u503c\u4f18\u5148\u7ea7\u8981\u9ad8\u4e00\u4e9b\u3002<\/p>\n
\u6211\u4eec\u58f0\u660e\u4e00\u4e2aReturnCallback\u5e76\u8bbe\u7f6e\u5230RabbitTemplate\u4e2d<\/p>\n
\r\n rabbitTemplate.setMandatory(true); \r\n rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> { \r\n String correlationId = message.getMessageProperties() \r\n .getHeader(PublisherCallbackChannel.RETURNED_MESSAGE_CORRELATION_KEY); \r\n log.debug(\"\u6d88\u606f\uff1a{} \u53d1\u9001\u5931\u8d25, \u5e94\u7b54\u7801\uff1a{} \u539f\u56e0\uff1a{} \u4ea4\u6362\u673a: {} \u8def\u7531\u952e: {}\", correlationId, \r\n replyCode, replyText, exchange, routingKey); \r\n }); \r\n<\/pre>\n\u5f53\u6d88\u606f\u6210\u529f\u6295\u9012\u5230\u4ea4\u6362\u673a\u4f46\u662f\u65e0\u6cd5\u5339\u914d\u5230\u961f\u5217\u65f6\uff1a<\/p>\n
\r\n - Publishing message [(Body:'\"hello\"' MessageProperties [headers={spring_listener_return_correlation=155648bd-fc3e-4c8b-a650-7b1ce720c7a6, spring_returned_message_correlation=7029ee49-357a-42fc-8532-dc41b4bb8e87, __TypeId__=java.lang.String}, contentType=application\/json, contentEncoding=UTF-8, contentLength=7, deliveryMode=PERSISTENT, priority=0, deliveryTag=0])] on exchange [DIRECT_EXCHANGE], routingKey = [DIRECT_ROUTING_KEY2] \r\n \r\n - \u6d88\u606f\uff1a7029ee49-357a-42fc-8532-dc41b4bb8e87 \u53d1\u9001\u5931\u8d25, \u5e94\u7b54\u7801\uff1a312 \u539f\u56e0\uff1aNO_ROUTE \u4ea4\u6362\u673a: DIRECT_EXCHANGE \u8def\u7531\u952e: DIRECT_ROUTING_KEY2 \r\n - \u6d88\u606f\u53d1\u9001\u5230exchange\u6210\u529f,id: 7029ee49-357a-42fc-8532-dc41b4bb8e87 \r\n<\/pre>\n\u4ece\u4e0a\u9762\u6211\u4eec\u4e5f\u53ef\u4ee5\u770b\u51faReturnCallback\u53ea\u5904\u7406\u6295\u9012\u5230\u961f\u5217\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u5e76\u4e0d\u50cfConfirmCallback\u65e2\u80fd\u5904\u7406\u5931\u8d25\u7684\u60c5\u51b5\u4e5f\u80fd\u5904\u7406\u6210\u529f\u7684\u60c5\u51b5\u3002<\/p>\n
4. \u603b\u7ed3<\/strong><\/div>\n\u6d88\u606f\u6295\u9012\u5931\u8d25\u7684\u5904\u7406\u5728\u4f7f\u7528RabbitMQ\u7684\u4f7f\u7528\u4e2d\u65f6\u975e\u5e38\u5fc5\u8981\u7684\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u8ffd\u8e2a\u6d88\u606f\u7684\u6295\u9012\u60c5\u51b5\uff0c\u4ee5\u53ca\u5904\u7406\u6d88\u606f\u6295\u9012\u5f02\u5e38\u6216\u8005\u6210\u529f\u540e\u7684\u903b\u8f91\u5904\u7406\uff0c\u4e3a\u6d88\u606f\u4e22\u5931\u8fdb\u884c\u4e00\u4e9b\u515c\u5e95\u6216\u8005\u8bb0\u5f55\u3002\u4f46\u662f\u8bf7\u6ce8\u610f\u8fd9\u4e2a\u5e76\u4e0d\u662f\u53d1\u751f\u5728\u6d88\u8d39\u9636\u6bb5\uff0c\u662f\u5426\u6210\u529f\u6d88\u8d39\u5e76\u4e0d\u662f\u7531\u8fd9\u4e24\u79cd\u56de\u8c03\u6765\u5904\u7406\uff0c\u6211\u4eec\u6709\u7a7a\u518d\u5bf9\u6d88\u606f\u7684\u6d88\u8d39\u786e\u8ba4\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u4f7f\u7528RabbitMQ\u6d88\u606f\u4e2d\u95f4\u4ef6\u65f6\uff0c\u56e0\u4e3a\u6d88\u606f\u7684\u6295\u9012\u662f\u5f02\u6b65\u7684\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cRabbitMQ\u4f1a\u5220\u9664\u90a3\u4e9b\u65e0\u6cd5\u8def\u7531\u7684\u6d88 […]<\/p>\n","protected":false},"author":1898,"featured_media":202221,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[607],"class_list":["post-202216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-rabbitmq"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=202216"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202216\/revisions"}],"predecessor-version":[{"id":202220,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/202216\/revisions\/202220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/202221"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=202216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=202216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=202216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}