Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ListenerUtils.stoppableSleep() for idleBetweenPolls #1845

Closed
garyrussell opened this issue Jun 25, 2021 · 1 comment
Closed

Use ListenerUtils.stoppableSleep() for idleBetweenPolls #1845

garyrussell opened this issue Jun 25, 2021 · 1 comment
Assignees
Milestone

Comments

@garyrussell
Copy link
Contributor

2.7 introduced this utility method so that error handlers with a BackOff would be responsive to a container.stop() operation; previously, the stop() would be delayed.

/**
* Sleep for the desired timeout, as long as the container continues to run.
* @param container the container.
* @param interval the timeout.
* @throws InterruptedException if the thread is interrupted.
* @since 2.7
*/
public static void stoppableSleep(MessageListenerContainer container, long interval) throws InterruptedException {
long timeout = System.currentTimeMillis() + interval;
do {
Thread.sleep(SLEEP_INTERVAL);
if (!container.isRunning()) {
break;
}
}
while (System.currentTimeMillis() < timeout);
}

This can also be used with idleBetweenPolls to avoid delaying the stop() there too.

if (idleBetweenPolls > 0) {
try {
TimeUnit.MILLISECONDS.sleep(idleBetweenPolls);
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Consumer Thread [" + this + "] has been interrupted", ex);
}
}

Enhance the method to use a regular sleep() if the idle interval is less than 500ms.

@garyrussell garyrussell added this to the 2.7.4 milestone Jun 25, 2021
@garyrussell garyrussell self-assigned this Jun 25, 2021
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Jun 28, 2021
spring-projects#1845

- don't idle until partitions are assigned - it caused delayed assignment
- use `ListenerUtils.stoppableSleep()` - don't hold up container stop while idle
- `ListenerUtils.stoppableSleep()` - reduce sleep time for small intervals to
  improve resolution
artembilan pushed a commit that referenced this issue Jun 28, 2021
#1845

- don't idle until partitions are assigned - it caused delayed assignment
- use `ListenerUtils.stoppableSleep()` - don't hold up container stop while idle
- `ListenerUtils.stoppableSleep()` - reduce sleep time for small intervals to
  improve resolution
@garyrussell
Copy link
Contributor Author

Closed by #1848

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant