Abacn commented on code in PR #39253:
URL: https://github.com/apache/beam/pull/39253#discussion_r3548055802


##########
sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java:
##########
@@ -783,21 +896,43 @@ public void close() {
     private void doClose() {
       try {
         closeAutoscaler();
-        closeConsumer();
-        ScheduledExecutorService executorService =
-            options.as(ExecutorOptions.class).getScheduledExecutorService();
-        executorService.schedule(
-            () -> {
-              LOG.debug("Closing connection after delay {}", 
source.spec.getCloseTimeout());
-              // Discard the checkpoints and set the reader as inactive
-              checkpointMarkPreparer.discard();
-              closeSession();
-              closeConnection();
-            },
-            source.spec.getCloseTimeout().getMillis(),
-            TimeUnit.MILLISECONDS);
+        // Discard the checkpoints and set the reader as inactive
+        checkpointMarkPreparer.discard();
+        if (source.spec.getAcknowledgeMode() == 
AcknowledgeMode.CLIENT_ACKNOWLEDGE) {
+          // checkpointMark holds session in CLIENT_ACKNOWLEDGE mode. Therefore
+          // we can close consumer and session immediately.
+          closeConsumer();
+          closeSession();
+        }
+        if (activeCheckpoints.get() == 0) {
+          closeConsumer();
+          closeSession();
+          closeConnection();
+        } else {
+          ScheduledExecutorService executorService =
+              options.as(ExecutorOptions.class).getScheduledExecutorService();
+          executorService.submit(
+              () -> {
+                long startTime = System.currentTimeMillis();
+                long timeoutMillis = source.spec.getCloseTimeout().getMillis();
+                while (activeCheckpoints.get() > 0
+                    && System.currentTimeMillis() - startTime < timeoutMillis) 
{
+                  try {
+                    Thread.sleep(1_000); // poll in 1 sec interval
+                  } catch (InterruptedException ignored) {
+                    break;
+                  }
+                }
+                LOG.debug(
+                    "Closing connection after checkpoints finalized or 
timeout: {}",
+                    source.spec.getCloseTimeout());
+                closeConsumer();
+                closeSession();
+                closeConnection();
+              });
+        }

Review Comment:
   Disagree. closeConsumer and closeSession are idempotent. It will set the 
resource to 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