You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/02/02 12:33:34 UTC
[PR] Migrate multiple components from Thread.sleep() to Camel's Task API [camel]
gnodet opened a new pull request, #21215:
URL: https://github.com/apache/camel/pull/21215
## Description
Migrates 5 components from using `Thread.sleep()` to Camel's Task API for better integration with Camel's lifecycle management and proper shutdown handling.
## Components Migrated
1. **camel-salesforce** (`SubscriptionHelper.java`)
- Replaced `Thread.sleep()` in subscription retry logic with exponential backoff
- Uses `ForegroundTask` with `initialDelay` for proper backoff delays
2. **camel-aws2-athena** (`Athena2QueryHelper.java`)
- Replaced `Thread.sleep()` in query polling delay
- Uses `ForegroundTask` with `initialDelay` for polling intervals
3. **camel-zookeeper** (`ZooKeeperConsumer.java`)
- Replaced `Thread.sleep()` in reconnection backoff logic
- Uses `ForegroundTask` with `initialDelay` for reconnection delays
4. **camel-hazelcast** (`HazelcastSedaConsumer.java`)
- Replaced `Thread.sleep()` in error recovery delay
- Uses `ForegroundTask` with `initialDelay` for error recovery backoff
5. **camel-iggy** (`IggyFetchRecords.java`)
- Replaced `Thread.sleep()` to avoid busy-waiting when consumer is suspended
- Uses `ForegroundTask` with `initialDelay` for suspension delays
## Key Technical Details
### Pattern Used
For one-time delays (which is the use case in all these components), the correct pattern is:
```java
Tasks.foregroundTask()
.withBudget(Budgets.iterationBudget()
.withMaxIterations(1)
.withInitialDelay(Duration.ofMillis(delayInMillis))
.withInterval(Duration.ZERO)
.build())
.withName("ComponentNameDelay")
.build()
.run(camelContext, () -> true);
```
**Important:** We use `withInitialDelay()` instead of `withInterval()` because:
- `ForegroundTask` only sleeps **between** iterations, not before the first iteration
- For a one-time delay with `maxIterations(1)`, we need the delay to happen **before** the first iteration
- Using `withInterval()` would cause the task to complete immediately without any delay
### Benefits
1. **Better lifecycle management**: Tasks respect Camel's shutdown process
2. **Proper interruption handling**: Tasks can be interrupted cleanly during shutdown
3. **Consistent pattern**: All components use the same Camel API for delays
4. **No thread blocking**: Integrates with Camel's task scheduling infrastructure
## Testing
- ✅ All components compile successfully
- ✅ Code style checks pass (`-Psourcecheck`)
- ✅ Imports sorted and code formatted
- ✅ No breaking changes to existing functionality
## Related Work
This migration was inspired by feedback on PR #21209 (CAMEL-22898) where it was suggested to use Camel's Task API instead of `Thread.sleep()` for retry delays, referencing `PgEventConsumer` as an example.
There are additional components that could benefit from this migration (see components using `Thread.sleep()` for retry/backoff delays), but this PR focuses on these 5 components as an initial batch.
---
Pull Request opened by [Augment Code](https://www.augmentcode.com/) with guidance from the PR author
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] CAMEL-22949: Migrate components from Thread.sleep() to Camel's Task API [camel]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #21215:
URL: https://github.com/apache/camel/pull/21215
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Migrate multiple components from Thread.sleep() to Camel's Task API [camel]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #21215:
URL: https://github.com/apache/camel/pull/21215#issuecomment-3834860564
:star2: Thank you for your contribution to the Apache Camel project! :star2:
:robot: CI automation will test this PR automatically.
:camel: Apache Camel Committers, please review the following items:
* First-time contributors **require MANUAL approval** for the GitHub Actions to run
* You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot.
* You can label PRs using `build-all`, `build-dependents`, `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
* Build and test logs are available in the Summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary.
* :warning: Be careful when sharing logs. Review their contents before sharing them publicly.
--
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: commits-unsubscribe@camel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org