|
21 | 21 | import java.util.Arrays;
|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.UUID;
|
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.concurrent.TimeoutException; |
24 | 27 | import java.util.function.Consumer;
|
25 | 28 |
|
26 | 29 | import com.amazonaws.services.schemaregistry.deserializers.GlueSchemaRegistryDeserializer;
|
@@ -174,6 +177,8 @@ public class KclMessageDrivenChannelAdapter extends MessageProducerSupport
|
174 | 177 |
|
175 | 178 | private long pollingIdleTime = 1500L;
|
176 | 179 |
|
| 180 | + private long gracefulShutdownTimeout; |
| 181 | + |
177 | 182 | public KclMessageDrivenChannelAdapter(String... streams) {
|
178 | 183 | this(KinesisAsyncClient.create(), CloudWatchAsyncClient.create(), DynamoDbAsyncClient.create(), streams);
|
179 | 184 | }
|
@@ -393,6 +398,17 @@ public void setPollingIdleTime(long pollingIdleTime) {
|
393 | 398 | this.pollingIdleTime = pollingIdleTime;
|
394 | 399 | }
|
395 | 400 |
|
| 401 | + /** |
| 402 | + * The timeout for {@link Scheduler#startGracefulShutdown()}. |
| 403 | + * Defaults to {@code 0} with the meaning to call {@link Scheduler#shutdown()}. |
| 404 | + * @param gracefulShutdownTimeout the timeout for {@link Scheduler#startGracefulShutdown()}. |
| 405 | + * @since 3.0.8 |
| 406 | + * @see Scheduler#startGracefulShutdown() |
| 407 | + */ |
| 408 | + public void setGracefulShutdownTimeout(long gracefulShutdownTimeout) { |
| 409 | + this.gracefulShutdownTimeout = gracefulShutdownTimeout; |
| 410 | + } |
| 411 | + |
396 | 412 | @Override
|
397 | 413 | protected void onInit() {
|
398 | 414 | super.onInit();
|
@@ -494,14 +510,25 @@ protected void doStart() {
|
494 | 510 | @Override
|
495 | 511 | protected void doStop() {
|
496 | 512 | super.doStop();
|
497 |
| - this.scheduler.shutdown(); |
| 513 | + if (this.gracefulShutdownTimeout == 0) { |
| 514 | + this.scheduler.shutdown(); |
| 515 | + } |
| 516 | + else { |
| 517 | + try { |
| 518 | + logger.info("Start graceful shutdown for KCL..."); |
| 519 | + this.scheduler.startGracefulShutdown().get(this.gracefulShutdownTimeout, TimeUnit.MILLISECONDS); |
| 520 | + } |
| 521 | + catch (InterruptedException | ExecutionException | TimeoutException ex) { |
| 522 | + throw new RuntimeException("Graceful shutdown for KCL has failed.", ex); |
| 523 | + } |
| 524 | + } |
498 | 525 | }
|
499 | 526 |
|
500 | 527 | @Override
|
501 | 528 | public void destroy() {
|
502 | 529 | super.destroy();
|
503 | 530 | if (isRunning()) {
|
504 |
| - this.scheduler.shutdown(); |
| 531 | + stop(); |
505 | 532 | }
|
506 | 533 | }
|
507 | 534 |
|
|
0 commit comments