davsclaus commented on code in PR #30466:
URL: https://github.com/apache/camel/pull/30466#discussion_r4640216088


##########
components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConsumer.java:
##########
@@ -75,15 +104,67 @@ public void onEvent(BoxEvent event) {
 
     @Override
     public void onNextPosition(long position) {
-        // noop
+        nextPosition = position;
+        lock.lock();
+        try {
+            // the event stream works again
+            restartDelay = RESTART_DELAY;
+        } finally {
+            lock.unlock();
+        }
     }
 
+    /**
+     * Called by the event stream when it fails (for example when Box cannot 
be reached for a while). The event stream
+     * has stopped itself by then and is never started again by the Box SDK: 
listen again after a delay, else the
+     * consumer stays started without receiving any event.
+     */
     @Override
     public boolean onException(Throwable e) {
         
getExceptionHandler().handleException(RuntimeCamelException.wrapRuntimeCamelException(e));
+        scheduleRestart();
         return true;
     }
 
+    private void scheduleRestart() {
+        lock.lock();
+        try {
+            if (!isRunAllowed() || restartExecutor == null) {
+                return;
+            }
+            long delay = restartDelay;
+            restartDelay = Math.min(restartDelay * 2, MAX_RESTART_DELAY);
+            LOG.info("The Box event stream stopped, listening for events again 
in {} ms", delay);
+            restartExecutor.schedule(this::restart, delay, 
TimeUnit.MILLISECONDS);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void restart() {
+        lock.lock();
+        try {
+            if (!isRunAllowed()) {
+                return;
+            }
+            Long position = nextPosition != null ? nextPosition : 
startingPosition();
+            apiProxy.stopListening();
+            apiProxy.listen(this, position);
+            LOG.info("Listening for Box events again from position {}", 
position != null ? position : "now");

Review Comment:
   Accepted as by design: the restart is rare (only after the event stream 
failed), and holding the lock orders it with a stop of the consumer.
   
   _Claude Code on behalf of Claus Ibsen_



##########
components/camel-box/camel-box-component/src/main/java/org/apache/camel/component/box/BoxConsumer.java:
##########
@@ -75,15 +104,67 @@ public void onEvent(BoxEvent event) {
 
     @Override
     public void onNextPosition(long position) {
-        // noop
+        nextPosition = position;
+        lock.lock();
+        try {
+            // the event stream works again
+            restartDelay = RESTART_DELAY;
+        } finally {
+            lock.unlock();
+        }
     }
 
+    /**
+     * Called by the event stream when it fails (for example when Box cannot 
be reached for a while). The event stream
+     * has stopped itself by then and is never started again by the Box SDK: 
listen again after a delay, else the
+     * consumer stays started without receiving any event.
+     */
     @Override
     public boolean onException(Throwable e) {
         
getExceptionHandler().handleException(RuntimeCamelException.wrapRuntimeCamelException(e));
+        scheduleRestart();
         return true;
     }
 
+    private void scheduleRestart() {
+        lock.lock();
+        try {
+            if (!isRunAllowed() || restartExecutor == null) {
+                return;
+            }
+            long delay = restartDelay;
+            restartDelay = Math.min(restartDelay * 2, MAX_RESTART_DELAY);
+            LOG.info("The Box event stream stopped, listening for events again 
in {} ms", delay);
+            restartExecutor.schedule(this::restart, delay, 
TimeUnit.MILLISECONDS);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void restart() {
+        lock.lock();
+        try {
+            if (!isRunAllowed()) {
+                return;
+            }
+            Long position = nextPosition != null ? nextPosition : 
startingPosition();
+            apiProxy.stopListening();
+            apiProxy.listen(this, position);
+            LOG.info("Listening for Box events again from position {}", 
position != null ? position : "now");
+        } catch (Exception e) {
+            // such as Box still not reachable when the event stream starts
+            LOG.debug("Cannot listen for Box events again", e);
+            scheduleRestart();

Review Comment:
   Accepted as by design: the consumer keeps trying with a backoff up to 60 s, 
and every failure still goes to the exception handler, so it is not silent; a 
permanent failure such as revoked credentials shows there.
   
   _Claude Code on behalf of Claus Ibsen_



-- 
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