12
12
import java .util .concurrent .ConcurrentHashMap ;
13
13
import java .util .concurrent .ExecutorService ;
14
14
import java .util .concurrent .Executors ;
15
+ import java .util .concurrent .TimeUnit ;
15
16
import java .util .function .Consumer ;
16
17
17
18
/**
@@ -23,6 +24,7 @@ class EventSupport {
23
24
// we use a v4 uuid as a "placeholder" for anonymous clients, since
24
25
// ConcurrentHashMap doesn't support nulls
25
26
private static final String defaultClientUuid = UUID .randomUUID ().toString ();
27
+ private static final int SHUTDOWN_TIMEOUT_SECONDS = 3 ;
26
28
private final Map <String , HandlerStore > handlerStores = new ConcurrentHashMap <>();
27
29
private final HandlerStore globalHandlerStore = new HandlerStore ();
28
30
private final ExecutorService taskExecutor = Executors .newCachedThreadPool (runnable -> {
@@ -146,13 +148,19 @@ public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails
146
148
}
147
149
148
150
/**
149
- * Stop the event handler task executor.
151
+ * Stop the event handler task executor and block until either termination has completed
152
+ * or timeout period has elapsed.
150
153
*/
151
154
public void shutdown () {
155
+ taskExecutor .shutdown ();
152
156
try {
153
- taskExecutor .shutdown ();
154
- } catch (Exception e ) {
155
- log .warn ("Exception while attempting to shutdown task executor" , e );
157
+ if (!taskExecutor .awaitTermination (SHUTDOWN_TIMEOUT_SECONDS , TimeUnit .SECONDS )) {
158
+ log .warn ("Task executor did not terminate before the timeout period had elapsed" );
159
+ taskExecutor .shutdownNow ();
160
+ }
161
+ } catch (InterruptedException e ) {
162
+ taskExecutor .shutdownNow ();
163
+ Thread .currentThread ().interrupt ();
156
164
}
157
165
}
158
166
0 commit comments