{"id":1401,"date":"2023-03-19T11:18:57","date_gmt":"2023-03-19T03:18:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1401"},"modified":"2023-04-28T21:09:24","modified_gmt":"2023-04-28T13:09:24","slug":"three-ways-for-rocketmq-producers-to-send-messages","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/three-ways-for-rocketmq-producers-to-send-messages\/","title":{"rendered":"RocketMQ\u751f\u4ea7\u8005Producer\u53d1\u9001\u6d88\u606f\u7684\u4e09\u79cd\u65b9\u5f0f"},"content":{"rendered":"<h2>\u53ef\u9760\u540c\u6b65\u53d1\u9001<\/h2>\n<p>\u540c\u6b65\u53d1\u9001\u662f\u6307\u6d88\u606f\u53d1\u9001\u65b9\u53d1\u51fa\u6570\u636e\u540e\uff0c\u4f1a\u5728\u6536\u5230\u63a5\u6536\u65b9\u53d1\u56de\u54cd\u5e94\u4e4b\u540e\u624d\u53d1\u4e0b\u4e00\u4e2a\u6570\u636e\u5305\u7684\u901a\u8baf\u65b9\u5f0f\u3002<\/p>\n<p>\u8c03\u7528<code>DefaultMQProducer<\/code>\u7684<code>send<\/code>\u65b9\u6cd5<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class SyncProducer {\n    public static void main(String[] args) throws Exception {\n        \/\/Instantiate with a producer group name.\n        DefaultMQProducer producer = new DefaultMQProducer(&quot;example_group_name&quot;);\n        \/\/Launch the instance.\n        producer.start();\n        for (int i = 0; i &lt; 100; i++) {\n            \/\/Create a message instance, specifying topic, tag and message body.\n            Message msg = new Message(&quot;TopicTest&quot;, &quot;TagA&quot;,\n                    (&quot;Hello RocketMQ &quot; + i).getBytes(RemotingHelper.DEFAULT_CHARSET));\n            \/\/Call send message to deliver message to one of brokers.\n            SendResult sendResult = producer.send(msg);\n            System.out.printf(&quot;%s%n&quot;, sendResult);\n        }\n        \/\/Shut down once the producer instance is not longer in use.\n        producer.shutdown();\n    }\n}<\/code><\/pre>\n<h2>\u53ef\u9760\u5f02\u6b65\u53d1\u9001<\/h2>\n<p>\u5f02\u6b65\u53d1\u9001\u662f\u6307\u53d1\u9001\u65b9\u53d1\u51fa\u6570\u636e\u540e\uff0c\u4e0d\u7b49\u63a5\u6536\u65b9\u53d1\u56de\u54cd\u5e94\uff0c\u63a5\u7740\u53d1\u9001\u4e0b\u4e2a\u6570\u636e\u5305\u7684\u901a\u8baf\u65b9\u5f0f\u3002 MQ \u7684\u5f02\u6b65\u53d1\u9001\uff0c\u9700\u8981\u7528\u6237\u5b9e\u73b0\u5f02\u6b65\u53d1\u9001\u56de\u8c03\u63a5\u53e3\uff08SendCallback\uff09\u3002\u6d88\u606f\u53d1\u9001\u65b9\u5728\u53d1\u9001\u4e86\u4e00\u6761\u6d88\u606f\u540e\uff0c\u4e0d\u9700\u8981\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\u5373\u53ef\u8fd4\u56de\uff0c\u8fdb\u884c\u7b2c\u4e8c\u6761\u6d88\u606f\u53d1\u9001\u3002\u53d1\u9001\u65b9\u901a\u8fc7\u56de\u8c03\u63a5\u53e3\u63a5\u6536\u670d\u52a1\u5668\u54cd\u5e94\uff0c\u5e76\u5bf9\u54cd\u5e94\u7ed3\u679c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-java\">public class AsyncProducer {\n    public static void main(String[] args) throws Exception {\n        \/\/Instantiate with a producer group name.\n        DefaultMQProducer producer = new DefaultMQProducer(&quot;example_group_name&quot;);\n        \/\/Launch the instance.\n        producer.start();\n        producer.setRetryTimesWhenSendAsyncFailed(0);\n        for (int i = 0; i &lt; 100; i++) {\n            final int index = i;\n            \/\/Create a message instance, specifying topic, tag and message body.\n            Message msg = new Message(&quot;TopicTest&quot;, &quot;TagA&quot;, &quot;OrderID188&quot;,\n                    &quot;Hello World&quot;.getBytes(RemotingHelper.DEFAULT_CHARSET));\n            producer.send(msg, new SendCallback() {\n                public void onSuccess(SendResult sendResult) {\n                    System.out.printf(&quot;%-10d OK %s %n&quot;, index,\n                            sendResult.getMsgId());\n                }\n\n                public void onException(Throwable e) {\n                    System.out.printf(&quot;%-10d Exception %s %n&quot;, index, e);\n                    e.printStackTrace();\n                }\n            });\n        }\n        \/\/Shut down once the producer instance is not longer in use.\n        producer.shutdown();\n    }\n}<\/code><\/pre>\n<h2>\u5355\u5411\uff08Oneway\uff09\u53d1\u9001<\/h2>\n<p>\u5355\u5411\uff08Oneway\uff09\u53d1\u9001\u7279\u70b9\u4e3a\u53d1\u9001\u65b9\u53ea\u8d1f\u8d23\u53d1\u9001\u6d88\u606f\uff0c\u4e0d\u7b49\u5f85\u670d\u52a1\u5668\u56de\u5e94\u4e14\u6ca1\u6709\u56de\u8c03\u51fd\u6570\u89e6\u53d1\uff0c\u5373\u53ea\u53d1\u9001\u8bf7\u6c42\u4e0d\u7b49\u5f85\u5e94\u7b54\u3002\u6b64\u65b9\u5f0f\u53d1\u9001\u6d88\u606f\u7684\u8fc7\u7a0b\u8017\u65f6\u975e\u5e38\u77ed\uff0c\u4e00\u822c\u5728\u5fae\u79d2\u7ea7\u522b\u3002<\/p>\n<p>\u8c03\u7528<code>DefaultMQProducer<\/code>\u7684<code>sendOneway<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public class OnewayProducer {\n    public static void main(String[] args) throws Exception{\n        \/\/Instantiate with a producer group name.\n        DefaultMQProducer producer = new DefaultMQProducer(&quot;example_group_name&quot;);\n        \/\/Launch the instance.\n        producer.start();\n        for (int i = 0; i &lt; 100; i++) {\n            \/\/Create a message instance, specifying topic, tag and message body.\n            Message msg = new Message(&quot;TopicTest&quot;, &quot;TagA&quot;,\n                    (&quot;Hello RocketMQ &quot; + i).getBytes(RemotingHelper.DEFAULT_CHARSET)\n            );\n            \/\/Call send message to deliver message to one of brokers.\n            producer.sendOneway(msg);\n        }\n        \/\/Shut down once the producer instance is not longer in use.\n        producer.shutdown();\n    }\n}<\/code><\/pre>\n<h2>\u6bd4\u8f83\u7279\u70b9<\/h2>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u53d1\u9001\u65b9\u5f0f<\/th>\n<th style=\"text-align: center;\">\u53d1\u9001TPS<\/th>\n<th style=\"text-align: center;\">\u53d1\u9001\u7ed3\u679c\u53cd\u9988<\/th>\n<th style=\"text-align: center;\">\u53ef\u9760\u6027<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">\u540c\u6b65\u53d1\u9001<\/td>\n<td style=\"text-align: center;\">\u5feb<\/td>\n<td style=\"text-align: center;\">\u6709<\/td>\n<td style=\"text-align: center;\">\u4e0d\u4e22\u5931<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5f02\u6b65\u53d1\u9001<\/td>\n<td style=\"text-align: center;\">\u5feb<\/td>\n<td style=\"text-align: center;\">\u6709<\/td>\n<td style=\"text-align: center;\">\u4e0d\u4e22\u5931<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u5355\u9879\u53d1\u9001<\/td>\n<td style=\"text-align: center;\">\u6700\u5feb<\/td>\n<td style=\"text-align: center;\">\u65e0<\/td>\n<td style=\"text-align: center;\">\u53ef\u80fd\u4e22\u5931<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/help.aliyun.com\/document_detail\/29547.html\">https:\/\/help.aliyun.com\/document_detail\/29547.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u9760\u540c\u6b65\u53d1\u9001 \u540c\u6b65\u53d1\u9001\u662f\u6307\u6d88\u606f\u53d1\u9001\u65b9\u53d1\u51fa\u6570\u636e\u540e\uff0c\u4f1a\u5728\u6536\u5230\u63a5\u6536\u65b9\u53d1\u56de\u54cd\u5e94\u4e4b\u540e\u624d\u53d1\u4e0b\u4e00\u4e2a\u6570\u636e\u5305\u7684\u901a\u8baf\u65b9\u5f0f\u3002 \u8c03\u7528D [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-1401","post","type-post","status-publish","format-standard","hentry","category-rocketmq"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1401"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1401\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}