rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lizhan...@apache.org
Subject [rocketmq] branch develop_oms_0.3.0 updated: Fix producer example so that it quits normally
Date Sun, 15 Apr 2018 09:33:01 GMT
This is an automated email from the ASF dual-hosted git repository.

lizhanhui pushed a commit to branch develop_oms_0.3.0
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop_oms_0.3.0 by this push:
     new 7b0b9d1  Fix producer example so that it quits normally
7b0b9d1 is described below

commit 7b0b9d1560d66c21145b4d5c215c7f8afdb63c4c
Author: shutian.lzh <shutian.lzh@alibaba-inc.com>
AuthorDate: Sun Apr 15 17:32:48 2018 +0800

    Fix producer example so that it quits normally
---
 .../rocketmq/example/openmessaging/SimpleProducer.java | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/example/src/main/java/org/apache/rocketmq/example/openmessaging/SimpleProducer.java
b/example/src/main/java/org/apache/rocketmq/example/openmessaging/SimpleProducer.java
index c785504..f993225 100644
--- a/example/src/main/java/org/apache/rocketmq/example/openmessaging/SimpleProducer.java
+++ b/example/src/main/java/org/apache/rocketmq/example/openmessaging/SimpleProducer.java
@@ -24,6 +24,7 @@ import io.openmessaging.OMS;
 import io.openmessaging.producer.Producer;
 import io.openmessaging.producer.SendResult;
 import java.nio.charset.Charset;
+import java.util.concurrent.CountDownLatch;
 
 public class SimpleProducer {
     public static void main(String[] args) {
@@ -38,14 +39,6 @@ public class SimpleProducer {
         producer.startup();
         System.out.printf("Producer startup OK%n");
 
-        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
-            @Override
-            public void run() {
-                producer.shutdown();
-                messagingAccessPoint.shutdown();
-            }
-        }));
-
         {
             Message message = producer.createBytesMessage("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8")));
             SendResult sendResult = producer.send(message);
@@ -53,6 +46,7 @@ public class SimpleProducer {
             System.out.printf("Send async message OK, msgId: %s%n", sendResult.messageId());
         }
 
+        final CountDownLatch countDownLatch = new CountDownLatch(1);
         {
             final Future<SendResult> result = producer.sendAsync(producer.createBytesMessage("OMS_HELLO_TOPIC",
"OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
             result.addListener(new FutureListener<SendResult>() {
@@ -63,6 +57,7 @@ public class SimpleProducer {
                     } else {
                         System.out.printf("Send async message OK, msgId: %s%n", future.get().messageId());
                     }
+                    countDownLatch.countDown();
                 }
             });
         }
@@ -71,5 +66,12 @@ public class SimpleProducer {
             producer.sendOneway(producer.createBytesMessage("OMS_HELLO_TOPIC", "OMS_HELLO_BODY".getBytes(Charset.forName("UTF-8"))));
             System.out.printf("Send oneway message OK%n");
         }
+
+        try {
+            countDownLatch.await();
+        } catch (InterruptedException ignore) {
+        }
+
+        producer.shutdown();
     }
 }

-- 
To stop receiving notification emails like this one, please contact
lizhanhui@apache.org.

Mime
View raw message