Skip to content

Commit

Permalink
GH-246: Add gracefulShutdownTimeout for KCL
Browse files Browse the repository at this point in the history
Fixes: #246
Issue link: #246
  • Loading branch information
artembilan committed Sep 26, 2024
1 parent ab2f9b8 commit 297a1c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

import com.amazonaws.services.schemaregistry.deserializers.GlueSchemaRegistryDeserializer;
Expand Down Expand Up @@ -174,6 +177,8 @@ public class KclMessageDrivenChannelAdapter extends MessageProducerSupport

private long pollingIdleTime = 1500L;

private long gracefulShutdownTimeout;

public KclMessageDrivenChannelAdapter(String... streams) {
this(KinesisAsyncClient.create(), CloudWatchAsyncClient.create(), DynamoDbAsyncClient.create(), streams);
}
Expand Down Expand Up @@ -393,6 +398,17 @@ public void setPollingIdleTime(long pollingIdleTime) {
this.pollingIdleTime = pollingIdleTime;
}

/**
* The timeout for {@link Scheduler#startGracefulShutdown()}.
* Defaults to {@code 0} with the meaning to call {@link Scheduler#shutdown()}.
* @param gracefulShutdownTimeout the timeout for {@link Scheduler#startGracefulShutdown()}.
* @since 3.0.8
* @see Scheduler#startGracefulShutdown()
*/
public void setGracefulShutdownTimeout(long gracefulShutdownTimeout) {
this.gracefulShutdownTimeout = gracefulShutdownTimeout;
}

@Override
protected void onInit() {
super.onInit();
Expand Down Expand Up @@ -494,14 +510,25 @@ protected void doStart() {
@Override
protected void doStop() {
super.doStop();
this.scheduler.shutdown();
if (this.gracefulShutdownTimeout == 0) {
this.scheduler.shutdown();
}
else {
try {
logger.info("Start graceful shutdown for KCL...");
this.scheduler.startGracefulShutdown().get(this.gracefulShutdownTimeout, TimeUnit.MILLISECONDS);
}
catch (InterruptedException | ExecutionException | TimeoutException ex) {
throw new RuntimeException("Graceful shutdown for KCL has failed.", ex);
}
}
}

@Override
public void destroy() {
super.destroy();
if (isRunning()) {
this.scheduler.shutdown();
stop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public KclMessageDrivenChannelAdapter kclMessageDrivenChannelAdapter() {
adapter.setBindSourceRecord(true);
adapter.setEmptyRecordList(true);
adapter.setPollingMaxRecords(99);
adapter.setGracefulShutdownTimeout(100);
return adapter;
}

Expand Down

0 comments on commit 297a1c4

Please sign in to comment.