You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cs...@apache.org on 2020/07/05 08:34:49 UTC

[sling-org-apache-sling-distribution-journal] branch master updated: SLING-9563 - Use InterruptedException to exit flow (#50)

This is an automated email from the ASF dual-hosted git repository.

cschneider pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git


The following commit(s) were added to refs/heads/master by this push:
     new 45ee8cf  SLING-9563 - Use InterruptedException to exit flow (#50)
45ee8cf is described below

commit 45ee8cf8004e621e0091df4ccf12215b8cfa6f33
Author: Christian Schneider <ch...@die-schneider.net>
AuthorDate: Sun Jul 5 10:34:40 2020 +0200

    SLING-9563 - Use InterruptedException to exit flow (#50)
    
    * SLING-9563 - Use InterruptedException to exit flow
    
    * SLING-9563 - Remove docs
    
    * SLING-9563 - Reinterrupt thread
---
 .../impl/subscriber/DistributionSubscriber.java    | 73 ++++++++++------------
 1 file changed, 33 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/DistributionSubscriber.java b/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/DistributionSubscriber.java
index 6b7ddd1..1d43922 100644
--- a/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/DistributionSubscriber.java
+++ b/src/main/java/org/apache/sling/distribution/journal/impl/subscriber/DistributionSubscriber.java
@@ -273,67 +273,60 @@ public class DistributionSubscriber {
     private void processQueue() {
         LOG.info("Started Queue processor");
         while (running) {
-            fetchAndProcessQueueItem();
+            try {
+                fetchAndProcessQueueItem();
+            } catch (PreConditionTimeoutException e) {
+                // Precondition timed out. We only log this on info level as it is no error
+                LOG.info(e.getMessage());
+                delay(RETRY_DELAY);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                LOG.debug(e.getMessage());
+            } catch (Exception e) {
+                // Catch all to prevent processing from stopping
+                LOG.error("Error processing queue item", e);
+                delay(RETRY_DELAY);
+            }
         }
         LOG.info("Stopped Queue processor");
     }
 
-    private void fetchAndProcessQueueItem() {
-        try {
-            
-            if (! blockingSendStoredStatus()) {
-                return;
-            }
-
-            Optional<FullMessage<PackageMessage>> item = blockingPeekQueueItem();
-            if (!item.isPresent()) {
-                return;
-            }
-
-            try (Timer.Context context = distributionMetricsService.getProcessQueueItemDuration().time()) {
-                processQueueItem(item.get());
-                messageBuffer.remove();
-                distributionMetricsService.getItemsBufferSize().decrement();
-            }
-
-        } catch (PreConditionTimeoutException e) {
-            // Precondition timed out. We only log this on info level as it is no error
-            LOG.info(e.getMessage());
-            delay(RETRY_DELAY);
-        } catch (IllegalStateException e) {
-            throw e;
-        } catch (Exception e) {
-            // Catch all to prevent processing from stopping
-            LOG.error("Error processing queue item", e);
-            delay(RETRY_DELAY);
+    private void fetchAndProcessQueueItem() throws InterruptedException, IOException, LoginException, DistributionException {
+        blockingSendStoredStatus();
+        FullMessage<PackageMessage> item = blockingPeekQueueItem();
+        try (Timer.Context context = distributionMetricsService.getProcessQueueItemDuration().time()) {
+            processQueueItem(item);
+            messageBuffer.remove();
+            distributionMetricsService.getItemsBufferSize().decrement();
         }
     }
 
     /**
      * Send status stored in a previous run if exists
-     *
-     * @return {@code true} if the status has been sent ;
-     *         {@code false} otherwise.
      */
-    private boolean blockingSendStoredStatus() {
+    private void blockingSendStoredStatus() throws InterruptedException, IOException {
         try (Timer.Context context = distributionMetricsService.getSendStoredStatusDuration().time()) {
-            for (int retry = 0 ; running && ! bookKeeper.sendStoredStatus(retry) ; retry++);
-        } catch (IOException e) {
-            LOG.warn("Error in timer close", e);
+            int retry = 0;
+            while (running) {
+                if (bookKeeper.sendStoredStatus(retry)) {
+                    return;
+                }
+                retry++;
+            }
         }
-        return running;
+        throw new InterruptedException("Shutting down");
     }
 
-    private Optional<FullMessage<PackageMessage>> blockingPeekQueueItem() throws InterruptedException {
+    private FullMessage<PackageMessage> blockingPeekQueueItem() throws InterruptedException {
         while (running) {
             FullMessage<PackageMessage> message = messageBuffer.peek();
             if (message != null) {
-                return Optional.of(message);
+                return message;
             } else {
                 Thread.sleep(QUEUE_FETCH_DELAY);
             }
         }
-        return Optional.empty();
+        throw new InterruptedException("Shutting down");
     }
 
     private void processQueueItem(FullMessage<PackageMessage> item) throws PersistenceException, LoginException, DistributionException {