duhengforever closed pull request #510: [ISSUE478]Polish async send message 
sample
URL: https://github.com/apache/rocketmq/pull/510
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/example/src/main/java/org/apache/rocketmq/example/simple/AsyncProducer.java 
b/example/src/main/java/org/apache/rocketmq/example/simple/AsyncProducer.java
index aa15cafff..d40739c81 100644
--- 
a/example/src/main/java/org/apache/rocketmq/example/simple/AsyncProducer.java
+++ 
b/example/src/main/java/org/apache/rocketmq/example/simple/AsyncProducer.java
@@ -17,6 +17,8 @@
 package org.apache.rocketmq.example.simple;
 
 import java.io.UnsupportedEncodingException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 import org.apache.rocketmq.client.exception.MQClientException;
 import org.apache.rocketmq.client.producer.DefaultMQProducer;
 import org.apache.rocketmq.client.producer.SendCallback;
@@ -32,7 +34,9 @@ public static void main(
         producer.start();
         producer.setRetryTimesWhenSendAsyncFailed(0);
 
-        for (int i = 0; i < 10000000; i++) {
+        int messageCount = 100;
+        final CountDownLatch countDownLatch = new CountDownLatch(messageCount);
+        for (int i = 0; i < messageCount; i++) {
             try {
                 final int index = i;
                 Message msg = new Message("Jodie_topic_1023",
@@ -42,11 +46,13 @@ public static void main(
                 producer.send(msg, new SendCallback() {
                     @Override
                     public void onSuccess(SendResult sendResult) {
+                        countDownLatch.countDown();
                         System.out.printf("%-10d OK %s %n", index, 
sendResult.getMsgId());
                     }
 
                     @Override
                     public void onException(Throwable e) {
+                        countDownLatch.countDown();
                         System.out.printf("%-10d Exception %s %n", index, e);
                         e.printStackTrace();
                     }
@@ -55,6 +61,7 @@ public void onException(Throwable e) {
                 e.printStackTrace();
             }
         }
+        countDownLatch.await(5, TimeUnit.SECONDS);
         producer.shutdown();
     }
 }


 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to