You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/01/18 02:55:09 UTC

[GitHub] wlliqipeng commented on a change in pull request #655: [RIP-9] Add the simple example description

wlliqipeng commented on a change in pull request #655: [RIP-9] Add the simple example description
URL: https://github.com/apache/rocketmq/pull/655#discussion_r248913886
 
 

 ##########
 File path: docs/cn/RocketMQ_Example.md
 ##########
 @@ -0,0 +1,957 @@
+样例(sample)
+============
+
+基本样例
+--------
+
+在基本样例中我们提供如下的功能场景:
+
+* 使用RocketMQ发送三种类型的消息:同步消息,异步消息和单向消息。其中前两种消息是可靠的,因为会有发送是否成功的应答。
+* 使用RocketMQ来消费接收到的消息。
+
+### 1、加入依赖:
+
+`maven:`
+```
+<dependency> 
+    <groupId>org.apache.rocketmq</groupId>
+    <artifactId>rocketmq-client</artifactId>
+    <version>4.3.0</version>  
+</dependency>
+```
+`gradle`
+```
+compile 'org.apache.rocketmq:rocketmq-client:4.3.0'
+```
+### 2、消息发送
+
+#### 1. Producer端发送同步消息
+
+这种可靠性同步地发送方式使用的比较广泛,比如:重要的消息通知,短信通知。
+```java
+public class SyncProducer {
+	public static void main(String[] args) throws Exception {
+    	// 实例化消息生产者Producer
+        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
+    	// 设置NameServer的地址
+    	producer.setNamesrvAddr("localhost:9876");
+    	// 启动Producer实例
+        producer.start();
+    	for (int i = 0; i < 100; i++) {
+        	// 创建消息,并指定Topic,Tag和消息体
+        	Message msg = new Message("TopicTest" /* Topic */,
+        	"TagA" /* Tag */,
+        	("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
+        	);
+        	// 发送消息到一个Broker
+            SendResult sendResult = producer.send(msg);
+            // 通过sendResult返回消息是否成功送达
+        	System.out.printf("%s%n", sendResult);
+    	}
+    	// 如果不再发送消息,关闭Producer实例。
+        producer.shutdown();
+	}
 
 Review comment:
   麻烦优化下缩进格式

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services