gitPratikSingh opened a new pull request, #29516:
URL: https://github.com/apache/camel/pull/29516
## Problem
When a timer route is (re)started at runtime on an already started
CamelContext (via `RouteController.startRoute`, or by adding the route later),
the first fire of a `delay=0` timer can be dropped silently. The next fire then
only happens after a full `period`. This does not happen at context start.
The reproducer in CAMEL-26746 loses the immediate fire in about 2.6% of
restarts under CPU load (77 of 3,000).
## Root cause
On a runtime route start, `TimerConsumer.doStart()` calls `configureTask()`
while the service is still STARTING: `BaseService.start()` flips the status to
STARTED only after `doStart()` returns. With `delay=0`, `java.util.Timer` can
run the task inside that window. The task checks `isTaskRunAllowed()`, which
was `isStarted()`, gets `false`, and the fire is skipped with only a debug log
line.
Context start is immune because there the task is configured from
`onCamelContextStarted`, after the consumers have been started.
## Fix
This is option (a) from the issue: let `isTaskRunAllowed()` accept STARTING,
the same way `ScheduledPollConsumer.isPollAllowed()` does.
```java
protected boolean isTaskRunAllowed() {
return isRunAllowed() && !isSuspended();
}
```
The `!isSuspended()` term keeps timers from firing while the route/consumer
is suspended, which a plain switch to `isRunAllowed()` would otherwise have
allowed.
## Test
New test `TimerFirstFireAfterRouteRestartTest` (in camel-core, where the
timer tests live) uses the deterministic variant from the issue: a custom
`java.util.Timer` bound as `timer=#eager` that runs the task synchronously when
it is scheduled. It covers both scenarios: restarting a route at runtime, and
adding a route at runtime.
- Without the fix, the restart scenario fails: after
`stopRoute`/`startRoute` the immediate fire never arrives (expected 1 message,
got 0).
- With the fix, both scenarios pass.
- The full timer test sweep (`-Dtest=Timer*Test`, 20 test classes including
`TimerSuspendCamelContextTest` and `TimerRestartTest`) passes: 38 tests, 0
failures.
--
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]