From 6d946faf424fe307a3850ba28bd828a4b421bea6 Mon Sep 17 00:00:00 2001 From: duhengforever Date: Wed, 31 Oct 2018 17:39:13 +0800 Subject: [PATCH] Polish async send message sample --- .../apache/rocketmq/example/simple/AsyncProducer.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 aa15caff..d40739c8 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 class AsyncProducer { 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 class AsyncProducer { 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 class AsyncProducer { e.printStackTrace(); } } + countDownLatch.await(5, TimeUnit.SECONDS); producer.shutdown(); } } -- GitLab