areyouok commented on a change in pull request #3639:
URL: https://github.com/apache/rocketmq/pull/3639#discussion_r768311983



##########
File path: 
client/src/main/java/org/apache/rocketmq/client/producer/RequestFutureHolder.java
##########
@@ -37,19 +38,13 @@
     private static final RequestFutureHolder INSTANCE = new 
RequestFutureHolder();
     private ConcurrentHashMap<String, RequestResponseFuture> 
requestFutureTable = new ConcurrentHashMap<String, RequestResponseFuture>();
     private final AtomicInteger producerNum = new AtomicInteger(0);

Review comment:
       there is no need use AtomicInteger.
   
   I suggest change to HashSet<DefaultMQProducerImpl> and 
startScheduledTask/shutdown support re-entry. 
   
   eg: called DefaultMQProducerImpl.shutdown got exception, and the use may 
call shutdown again.

##########
File path: 
client/src/main/java/org/apache/rocketmq/client/producer/RequestFutureHolder.java
##########
@@ -74,14 +69,40 @@ public void scanExpiredRequest() {
         }
     }
 
-    private RequestFutureHolder() {
+    public synchronized void startScheduledTask() {
+        if (this.producerNum.incrementAndGet() == 1) {
+            this.getScheduledExecutorService().scheduleAtFixedRate(new 
Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        RequestFutureHolder.getInstance().scanExpiredRequest();
+                    } catch (Throwable e) {
+                        log.error("scan RequestFutureTable exception", e);
+                    }
+                }
+            }, 1000 * 3, 1000, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    public synchronized void shutdown() {
+        if (this.producerNum.decrementAndGet() == 0) {
+            this.scheduledExecutorService.shutdown();
+            this.scheduledExecutorService = null;
+        }
     }
 
-    public AtomicInteger getProducerNum() {
-        return producerNum;
+    private RequestFutureHolder() {
     }
 
-    public ScheduledExecutorService getScheduledExecutorService() {
+    private ScheduledExecutorService getScheduledExecutorService() {
+        if (null == scheduledExecutorService || 
scheduledExecutorService.isShutdown()) {

Review comment:
       no need to check isShutdown

##########
File path: 
client/src/main/java/org/apache/rocketmq/client/producer/RequestFutureHolder.java
##########
@@ -74,14 +69,40 @@ public void scanExpiredRequest() {
         }
     }
 
-    private RequestFutureHolder() {
+    public synchronized void startScheduledTask() {
+        if (this.producerNum.incrementAndGet() == 1) {
+            this.getScheduledExecutorService().scheduleAtFixedRate(new 
Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        RequestFutureHolder.getInstance().scanExpiredRequest();
+                    } catch (Throwable e) {
+                        log.error("scan RequestFutureTable exception", e);
+                    }
+                }
+            }, 1000 * 3, 1000, TimeUnit.MILLISECONDS);
+        }
+    }
+
+    public synchronized void shutdown() {
+        if (this.producerNum.decrementAndGet() == 0) {
+            this.scheduledExecutorService.shutdown();

Review comment:
       check null




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to