{"id":2166,"date":"2017-10-16T15:21:24","date_gmt":"2017-10-16T07:21:24","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2166"},"modified":"2017-10-16T15:21:24","modified_gmt":"2017-10-16T07:21:24","slug":"openmessaging%e8%ae%bf%e9%97%aerocketmq%e6%95%99%e7%a8%8b%ef%bc%88%e5%bc%80%e5%8f%91%e5%ae%9e%e6%88%98%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2166","title":{"rendered":"OpenMessaging\u8bbf\u95eeRocketMQ\u6559\u7a0b\uff08\u5f00\u53d1\u5b9e\u6218\uff09"},"content":{"rendered":"<p>\n\t\u76ee\u524d\u7f51\u4e0a\u5173\u4e8e <a href=\"https:\/\/www.xttblog.com\/?tag=openmessaging\" name=\"openmessaging\" target=\"_blank\" title=\"openmessaging\">OpenMessaging<\/a> \u7684\u6280\u672f\u6587\u7ae0\u8fd8\u5f88\u5c11\uff0c\u6211\u4e2a\u4eba\u9884\u6d4b\u5b83\u5c06\u6765\u4f1a\u5f88\u706b\u3002\u800c\u963f\u91cc\u6350\u732e\u7ed9 Apache \u7684 <a href=\"https:\/\/www.xttblog.com\/?tag=RocketMQ\" name=\"RocketMQ\" target=\"_blank\" title=\"RocketMQ\">RocketMQ<\/a> \u5bf9 <a href=\"https:\/\/www.xttblog.com\/?tag=openmessaging\" name=\"openmessaging\" target=\"_blank\" title=\"openmessaging\">OpenMessaging<\/a> \u63d0\u4f9b\u4e86\u90e8\u5206\u5b9e\u73b0\u3002\u672c\u6587\u5c06\u8bb2\u89e3\u4ed6\u4eec\u4e4b\u95f4\u7684\u5f00\u53d1\u5b9e\u6218\u3002\n<\/p>\n<p>\n\t\u524d\u9762\u6211\u5df2\u7ecf\u4ecb\u7ecd\u8fc7 \u300a<a href=\"https:\/\/www.xttblog.com\/?p=2164\" name=\"openmessaging \u67b6\u6784\u539f\u7406\" target=\"_blank\" title=\"openmessaging \u67b6\u6784\u539f\u7406\">OpenMessaging \u7684\u67b6\u6784\u539f\u7406<\/a>\u300b\uff0c\u6ca1\u9605\u8bfb\u8fc7\u7684\u53ef\u4ee5\u770b\u770b\uff0c\u52a0\u6df1\u7406\u89e3\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"openmessaging \u67b6\u6784\u56fe\" src=\"http:\/\/openmessaging.cloud\/images\/@blog\/oms-domain-design-v0.1.png\" style=\"width: 857px;height: 481px\" \/>\n<\/p>\n<p>\n\t<a href=\"https:\/\/openmessaging.github.io\/\" target=\"_blank\">OpenMessaging<\/a>\u5305\u62ec\u5efa\u7acb\u884c\u4e1a\u51c6\u5219\u548c\u6d88\u606f\u4f20\u9012\uff0c\u6d41\u5a92\u4f53\u89c4\u8303\u4e3a\u91d1\u878d\uff0c\u7535\u200b\u200b\u5b50\u5546\u52a1\uff0c\u7269\u8054\u7f51\u548c\u5927\u6570\u636e\u533a\u57df\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5171\u540c\u7684\u6846\u67b6\u3002<a href=\"https:\/\/openmessaging.github.io\/\" target=\"_blank\">\u8bbe\u8ba1\u539f\u5219\u662f\u5206\u5e03\u5f0f\u5f02\u6784\u73af\u5883\u4e2d\u9762\u5411\u4e91\uff0c\u7b80\u5355\uff0c\u7075\u6d3b\u548c\u8bed\u8a00\u65e0\u5173\u3002<\/a>\u7b26\u5408\u8fd9\u4e9b\u89c4\u8303\u5c06\u6709\u53ef\u80fd\u5728\u6240\u6709\u4e3b\u8981\u5e73\u53f0\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5f00\u53d1\u5f02\u6784\u6d88\u606f\u5e94\u7528\u7a0b\u5e8f\u3002\n<\/p>\n<p>\n\t\u4ee5\u4e0b\u793a\u4f8b\u663e\u793a\u5982\u4f55\u5728\u540c\u6b65\uff0c\u5f02\u6b65\u6216\u5355\u5411\u4f20\u8f93\u4e2d\u5411<a href=\"https:\/\/www.xttblog.com\/?tag=RocketMQ\" name=\"RocketMQ\" target=\"_blank\" title=\"RocketMQ\">RocketMQ<\/a>\u4ee3\u7406\u53d1\u9001\u6d88\u606f\u3002ProducerApp \u751f\u4ea7\u8005\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage io.openmessaging.samples.producer;\r\nimport io.openmessaging.Future;\r\nimport io.openmessaging.FutureListener;\r\nimport io.openmessaging.MessagingAccessPoint;\r\nimport io.openmessaging.MessagingAccessPointFactory;\r\nimport io.openmessaging.producer.Producer;\r\nimport io.openmessaging.producer.SendResult;\r\nimport java.nio.charset.Charset;\r\npublic class ProducerApp {\r\n    public static void main(String[] args) {\r\n        final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory\r\n            .getMessagingAccessPoint(&quot;openmessaging:rocketmq:\/\/localhost:10911\/namespace&quot;);\r\n\t\t\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n        final Producer producer = messagingAccessPoint.createProducer();\r\n        messagingAccessPoint.startup();\r\n        System.out.println(&quot;MessagingAccessPoint startup OK&quot;);\r\n        producer.startup();\r\n        System.out.println(&quot;Producer startup OK&quot;);\r\n        \/\/Add a shutdown hook\r\n        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                producer.shutdown();\r\n                messagingAccessPoint.shutdown();\r\n            }\r\n        }));\r\n        \/\/Sync www.xttblog.com\r\n        {\r\n            SendResult sendResult = producer.send(producer.createTopicBytesMessage(\r\n                &quot;HELLO_TOPIC&quot;, &quot;HELLO_BODY&quot;.getBytes(Charset.forName(&quot;UTF-8&quot;))));\r\n\r\n            System.out.println(&quot;Send sync message OK, message id is: &quot; + sendResult.messageId());\r\n        }\r\n        \/\/Async with Promise\r\n        {\r\n            final Future&lt;SendResult&gt; result = producer.sendAsync(producer.createTopicBytesMessage(\r\n                &quot;HELLO_TOPIC&quot;, &quot;HELLO_BODY&quot;.getBytes(Charset.forName(&quot;UTF-8&quot;))));\r\n\r\n            final SendResult sendResult = result.get(3000L);\r\n            System.out.println(&quot;Send async message OK, message id is: &quot; + sendResult.messageId());\r\n        }\r\n        \/\/Async with FutureListener\r\n        {\r\n            final Future&lt;SendResult&gt; result = producer.sendAsync(producer.createTopicBytesMessage(\r\n                &quot;HELLO_TOPIC&quot;, &quot;HELLO_BODY&quot;.getBytes(Charset.forName(&quot;UTF-8&quot;))));\r\n            result.addListener(new FutureListener&lt;SendResult&gt;() {\r\n                @Override\r\n                public void operationSucceeded(Future&lt;SendResult&gt; promise) {\r\n                    System.out.println(&quot;Send async message OK, message id is: &quot; + promise.get().messageId());\r\n                }\r\n                @Override\r\n                public void operationFailed(Future&lt;SendResult&gt; promise) {\r\n                    System.out.println(&quot;Send async message Failed, cause is: &quot; + promise.getThrowable().getMessage());\r\n                }\r\n            });\r\n        }\r\n        \/\/Oneway\r\n        {\r\n            producer.sendOneway(producer.createTopicBytesMessage(\r\n                &quot;HELLO_TOPIC&quot;, &quot;HELLO_BODY&quot;.getBytes(Charset.forName(&quot;UTF-8&quot;))));\r\n            System.out.println(&quot;Send oneway message OK&quot;);\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u4f7f\u7528PullConsumerApp\u8f6e\u8be2\u6765\u81ea\u6307\u5b9a\u961f\u5217\u7684\u6d88\u606f\u3002PullConsumerApp \u6d88\u8d39\u8005\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage io.openmessaging.samples.consumer;\r\nimport io.openmessaging.ResourceManager;\r\nimport io.openmessaging.Message;\r\nimport io.openmessaging.MessagingAccessPoint;\r\nimport io.openmessaging.MessagingAccessPointFactory;\r\nimport io.openmessaging.OMS;\r\nimport io.openmessaging.consumer.PullConsumer;\r\npublic class PullConsumerApp {\r\n    public static void main(String[] args) {\r\n        final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory\r\n            .getMessagingAccessPoint(&quot;openmessaging:rocketmq:\/\/localhost:10911\/namespace&quot;);\r\n        messagingAccessPoint.startup();\r\n        System.out.println(&quot;MessagingAccessPoint startup OK&quot;);\r\n        ResourceManager resourceManager = messagingAccessPoint.getResourceManager();\r\n\t\t\/\/ www.xttblog.com\r\n        resourceManager.createQueue(&quot;NS1&quot;, &quot;HELLO_QUEUE&quot;, OMS.newKeyValue());\r\n        \/\/PullConsumer only can pull messages from one queue.\r\n        final PullConsumer pullConsumer = messagingAccessPoint.createPullConsumer(&quot;HELLO_QUEUE&quot;);\r\n        pullConsumer.startup();\r\n        \/\/Poll one message from queue.\r\n        Message message = pullConsumer.poll();\r\n        \/\/Acknowledges the consumed message\r\n        pullConsumer.ack(message.sysHeaders().getString(Message.BuiltinKeys.MessageId));\r\n        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                pullConsumer.shutdown();\r\n                messagingAccessPoint.shutdown();\r\n            }\r\n        }));\r\n    }\r\n}<\/pre>\n<p>\n\t\u5c06PushConsumer\u9644\u52a0\u5230\u6307\u5b9a\u7684\u961f\u5217\uff0c\u5e76\u901a\u8fc7MessageListener\u6d88\u8d39\u6d88\u606f\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage io.openmessaging.samples.consumer;\r\nimport io.openmessaging.ResourceManager;\r\nimport io.openmessaging.Message;\r\nimport io.openmessaging.consumer.MessageListener;\r\nimport io.openmessaging.MessagingAccessPoint;\r\nimport io.openmessaging.MessagingAccessPointFactory;\r\nimport io.openmessaging.OMS;\r\nimport io.openmessaging.consumer.PushConsumer;\r\nimport io.openmessaging.exception.OMSResourceNotExistException;\r\npublic class PushConsumerApp {\r\n    public static void main(String[] args) throws OMSResourceNotExistException {\r\n        final MessagingAccessPoint messagingAccessPoint = MessagingAccessPointFactory\r\n            .getMessagingAccessPoint(&quot;openmessaging:rocketmq:\/\/localhost:10911\/namespace&quot;);\r\n        messagingAccessPoint.startup();\r\n        System.out.println(&quot;MessagingAccessPoint startup OK&quot;);\r\n        ResourceManager resourceManager = messagingAccessPoint.getResourceManager();\r\n        final PushConsumer consumer = messagingAccessPoint.createPushConsumer();\r\n        \/\/ Consume messages from a simple queue.\r\n        {   \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n            String simpleQueue = &quot;HELLO_QUEUE&quot;;\r\n            resourceManager.createQueue(&quot;NS1&quot;, simpleQueue, OMS.newKeyValue());\r\n            \/\/This queue doesn&#039;t has a source topic, so only the message delivered to the queue directly can\r\n            \/\/be consumed by this consumer.\r\n            consumer.attachQueue(simpleQueue, new MessageListener() {\r\n                @Override\r\n                public void onReceived(Message message, Context context) {\r\n                    System.out.println(&quot;Received one message: &quot; + message);\r\n                    context.ack();\r\n                }\r\n\r\n            });\r\n            consumer.startup();\r\n            System.out.println(&quot;Consumer startup OK&quot;);\r\n        }\r\n        \/\/Consume messages from a complex queue.\r\n        final PushConsumer anotherConsumer = messagingAccessPoint.createPushConsumer();\r\n        {\r\n            String complexQueue = &quot;QUEUE_HAS_SOURCE_TOPIC&quot;;\r\n            String sourceTopic = &quot;SOURCE_TOPIC&quot;;\r\n\r\n            \/\/Create the complex queue.\r\n            resourceManager.createQueue(&quot;NS_01&quot;, complexQueue, OMS.newKeyValue());\r\n            \/\/Create the source topic.\r\n            resourceManager.createTopic(&quot;NS_01&quot;, sourceTopic, OMS.newKeyValue());\r\n\r\n        }\r\n        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                consumer.shutdown();\r\n                anotherConsumer.shutdown();\r\n                messagingAccessPoint.shutdown();\r\n            }\r\n        }));\r\n    }\r\n}<\/pre>\n<p>\n\t\u76f8\u5173\u6e90\u4ee3\u7801\uff0c\u8bf7\u4e00\u6b65\u9605\u8bfb\uff1a<a href=\"https:\/\/github.com\/openmessaging\/openmessaging-java\" name=\"openmessaging java\" target=\"_blank\" title=\"openmessaging java\">https:\/\/github.com\/openmessaging\/openmessaging-java<\/a>\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2166\">OpenMessaging\u8bbf\u95eeRocketMQ\u6559\u7a0b\uff08\u5f00\u53d1\u5b9e\u6218\uff09<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u76ee\u524d\u7f51\u4e0a\u5173\u4e8e OpenMessaging \u7684\u6280\u672f\u6587\u7ae0\u8fd8\u5f88\u5c11\uff0c\u6211\u4e2a\u4eba\u9884\u6d4b\u5b83\u5c06\u6765\u4f1a\u5f88\u706b\u3002\u800c\u963f\u91cc\u6350\u732e\u7ed9 Apache [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,1538,1541],"class_list":["post-2166","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-openmessaging","tag-rocketmq"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2166"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2166\/revisions"}],"predecessor-version":[{"id":2167,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2166\/revisions\/2167"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}