@@ -42,10 +42,12 @@ public class BatchEventProcessor implements EventProcessor, AutoCloseable {
42
42
43
43
public static final String CONFIG_BATCH_SIZE = "event.processor.batch.size" ;
44
44
public static final String CONFIG_BATCH_INTERVAL = "event.processor.batch.interval" ;
45
+ public static final String CONFIG_CLOSE_TIMEOUT = "event.processor.close.timeout" ;
45
46
46
47
public static final int DEFAULT_QUEUE_CAPACITY = 1000 ;
47
48
public static final int DEFAULT_BATCH_SIZE = 50 ;
48
49
public static final long DEFAULT_BATCH_INTERVAL = TimeUnit .MINUTES .toMillis (1 );
50
+ public static final long DEFAULT_TIMEOUT_INTERVAL = TimeUnit .SECONDS .toMillis (5 );
49
51
50
52
private static final Object SHUTDOWN_SIGNAL = new Object ();
51
53
@@ -54,17 +56,19 @@ public class BatchEventProcessor implements EventProcessor, AutoCloseable {
54
56
55
57
private final int batchSize ;
56
58
private final long flushInterval ;
59
+ private final long timeoutMillis ;
57
60
private final ExecutorService executor ;
58
61
private final NotificationCenter notificationCenter ;
59
62
60
63
private Future <?> future ;
61
64
private boolean isStarted = false ;
62
65
63
- private BatchEventProcessor (BlockingQueue <Object > eventQueue , EventHandler eventHandler , Integer batchSize , Long flushInterval , ExecutorService executor , NotificationCenter notificationCenter ) {
66
+ private BatchEventProcessor (BlockingQueue <Object > eventQueue , EventHandler eventHandler , Integer batchSize , Long flushInterval , Long timeoutMillis , ExecutorService executor , NotificationCenter notificationCenter ) {
64
67
this .eventHandler = eventHandler ;
65
68
this .eventQueue = eventQueue ;
66
69
this .batchSize = batchSize == null ? PropertyUtils .getInteger (CONFIG_BATCH_SIZE , DEFAULT_BATCH_SIZE ) : batchSize ;
67
70
this .flushInterval = flushInterval == null ? PropertyUtils .getLong (CONFIG_BATCH_INTERVAL , DEFAULT_BATCH_INTERVAL ) : flushInterval ;
71
+ this .timeoutMillis = timeoutMillis == null ? PropertyUtils .getLong (CONFIG_CLOSE_TIMEOUT , DEFAULT_TIMEOUT_INTERVAL ) : timeoutMillis ;
68
72
this .notificationCenter = notificationCenter ;
69
73
70
74
if (executor == null ) {
@@ -97,10 +101,12 @@ public void close() throws Exception {
97
101
logger .info ("Start close" );
98
102
eventQueue .put (SHUTDOWN_SIGNAL );
99
103
try {
100
- future .get (5 , TimeUnit .SECONDS );
104
+ future .get (timeoutMillis , TimeUnit .MILLISECONDS );
101
105
} catch (InterruptedException e ) {
102
106
logger .warn ("Interrupted while awaiting termination." );
103
107
Thread .currentThread ().interrupt ();
108
+ } catch (TimeoutException e ) {
109
+ logger .error ("Timeout exceeded attempting to close for {} ms" , timeoutMillis );
104
110
}
105
111
}
106
112
@@ -179,12 +185,12 @@ private boolean shouldSplit(UserEvent userEvent) {
179
185
ProjectConfig currentConfig = currentBatch .peekLast ().getUserContext ().getProjectConfig ();
180
186
ProjectConfig newConfig = userEvent .getUserContext ().getProjectConfig ();
181
187
182
- // Revisions should match
188
+ // Projects should match
183
189
if (!currentConfig .getProjectId ().equals (newConfig .getProjectId ())) {
184
190
return true ;
185
191
}
186
192
187
- // Projects should match
193
+ // Revisions should match
188
194
if (!currentConfig .getRevision ().equals (newConfig .getRevision ())) {
189
195
return true ;
190
196
}
@@ -223,6 +229,7 @@ public static class Builder {
223
229
private Long flushInterval = null ;
224
230
private ExecutorService executor = null ;
225
231
private NotificationCenter notificationCenter = null ;
232
+ private Long timeoutMillis = null ;
226
233
227
234
public Builder withEventHandler (EventHandler eventHandler ) {
228
235
this .eventHandler = eventHandler ;
@@ -249,13 +256,18 @@ public Builder withExecutor(ExecutorService executor) {
249
256
return this ;
250
257
}
251
258
259
+ public Builder withTimeout (long duration , TimeUnit timeUnit ) {
260
+ this .timeoutMillis = timeUnit .toMillis (duration );
261
+ return this ;
262
+ }
263
+
252
264
public Builder withNotificationCenter (NotificationCenter notificationCenter ) {
253
265
this .notificationCenter = notificationCenter ;
254
266
return this ;
255
267
}
256
268
257
269
public BatchEventProcessor build () {
258
- return new BatchEventProcessor (eventQueue , eventHandler , batchSize , flushInterval , executor , notificationCenter );
270
+ return new BatchEventProcessor (eventQueue , eventHandler , batchSize , flushInterval , timeoutMillis , executor , notificationCenter );
259
271
}
260
272
}
261
273
0 commit comments