47
47
import java .util .List ;
48
48
import java .util .Optional ;
49
49
import java .util .concurrent .CompletableFuture ;
50
- import java .util .concurrent .atomic .AtomicBoolean ;
51
50
import java .util .function .BiConsumer ;
52
51
import java .util .function .Function ;
53
52
import java .util .stream .Collectors ;
54
53
55
54
public abstract class ListenExecutor extends RegularTaskExecutor <ListenTask > {
56
55
57
56
protected final EventRegistrationBuilderCollection regBuilders ;
58
- protected final EventRegistrationBuilderCollection untilRegBuilders ;
59
- protected final Optional <WorkflowFilter > until ;
60
57
protected final Optional <TaskExecutor <?>> loop ;
61
58
protected final Function <CloudEvent , JsonNode > converter ;
62
59
protected final EventConsumer eventConsumer ;
63
- protected final AtomicBoolean untilEvent = new AtomicBoolean (true );
64
60
65
61
private static record EventRegistrationBuilderCollection (
66
62
Collection <EventRegistrationBuilder > registrations , boolean isAnd ) {}
@@ -177,22 +173,37 @@ protected void internalProcessCe(
177
173
arrayNode .add (node );
178
174
future .complete (node );
179
175
}
180
-
181
- @ Override
182
- protected CompletableFuture <?> combine (CompletableFuture <JsonNode >[] completables ) {
183
- return CompletableFuture .allOf (completables );
184
- }
185
176
}
186
177
187
178
public static class OrListenExecutor extends ListenExecutor {
188
179
180
+ private final Optional <WorkflowFilter > until ;
181
+ private final EventRegistrationBuilderCollection untilRegBuilders ;
182
+
189
183
public OrListenExecutor (ListenExecutorBuilder builder ) {
190
184
super (builder );
185
+ this .until = Optional .ofNullable (builder .until );
186
+ this .untilRegBuilders = builder .untilRegistrations ;
191
187
}
192
188
193
189
@ Override
194
- protected CompletableFuture <?> combine (CompletableFuture <JsonNode >[] completables ) {
195
- return CompletableFuture .anyOf (completables );
190
+ protected <T > CompletableFuture <?> buildFuture (
191
+ EventRegistrationBuilderCollection regCollection ,
192
+ Collection <EventRegistration > registrations ,
193
+ BiConsumer <CloudEvent , CompletableFuture <T >> consumer ) {
194
+ CompletableFuture <?> combinedFuture =
195
+ super .buildFuture (regCollection , registrations , consumer );
196
+ if (untilRegBuilders != null ) {
197
+ Collection <EventRegistration > untilRegistrations = new ArrayList <>();
198
+ CompletableFuture <?> untilFuture =
199
+ combine (untilRegBuilders , untilRegistrations , (ce , f ) -> f .complete (null ));
200
+ untilFuture .thenAccept (
201
+ v -> {
202
+ combinedFuture .complete (null );
203
+ untilRegistrations .forEach (reg -> eventConsumer .unregister (reg ));
204
+ });
205
+ }
206
+ return combinedFuture ;
196
207
}
197
208
198
209
protected void internalProcessCe (
@@ -206,14 +217,12 @@ protected void internalProcessCe(
206
217
|| until
207
218
.filter (u -> u .apply (workflow , taskContext , arrayNode ).asBoolean ())
208
219
.isPresent ())
209
- && untilEvent . get () ) {
220
+ && untilRegBuilders == null ) {
210
221
future .complete (arrayNode );
211
222
}
212
223
}
213
224
}
214
225
215
- protected abstract CompletableFuture <?> combine (CompletableFuture <JsonNode >[] completables );
216
-
217
226
protected abstract void internalProcessCe (
218
227
JsonNode node ,
219
228
ArrayNode arrayNode ,
@@ -226,48 +235,37 @@ protected CompletableFuture<JsonNode> internalExecute(
226
235
WorkflowContext workflow , TaskContext taskContext ) {
227
236
ArrayNode output = JsonUtils .mapper ().createArrayNode ();
228
237
Collection <EventRegistration > registrations = new ArrayList <>();
229
- if (untilRegBuilders != null ) {
230
- untilEvent .set (false );
231
- }
232
- CompletableFuture <?> combinedFuture =
233
- combine (
234
- toCompletables (
235
- regBuilders ,
236
- registrations ,
237
- (ce , future ) ->
238
- processCe (converter .apply (ce ), output , workflow , taskContext , future )));
239
- CompletableFuture <JsonNode > resultFuture =
240
- combinedFuture .thenApply (
238
+ return buildFuture (
239
+ regBuilders ,
240
+ registrations ,
241
+ (BiConsumer <CloudEvent , CompletableFuture <JsonNode >>)
242
+ ((ce , future ) ->
243
+ processCe (converter .apply (ce ), output , workflow , taskContext , future )))
244
+ .thenApply (
241
245
v -> {
242
246
registrations .forEach (reg -> eventConsumer .unregister (reg ));
243
247
return output ;
244
248
});
245
- if (untilRegBuilders != null ) {
246
- Collection <EventRegistration > untilRegistrations = new ArrayList <>();
247
- CompletableFuture <?>[] futures =
248
- toCompletables (
249
- untilRegBuilders , untilRegistrations , (ce , future ) -> future .complete (null ));
250
- CompletableFuture <?> untilFuture =
251
- untilRegBuilders .isAnd ()
252
- ? CompletableFuture .allOf (futures )
253
- : CompletableFuture .anyOf (futures );
254
- untilFuture .thenAccept (
255
- v -> {
256
- untilEvent .set (true );
257
- combinedFuture .complete (null );
258
- untilRegistrations .forEach (reg -> eventConsumer .unregister (reg ));
259
- });
260
- }
261
- return resultFuture ;
262
249
}
263
250
264
- private <T > CompletableFuture <T >[] toCompletables (
251
+ protected <T > CompletableFuture <?> buildFuture (
252
+ EventRegistrationBuilderCollection regCollection ,
253
+ Collection <EventRegistration > registrations ,
254
+ BiConsumer <CloudEvent , CompletableFuture <T >> consumer ) {
255
+ return combine (regCollection , registrations , consumer );
256
+ }
257
+
258
+ protected final <T > CompletableFuture <?> combine (
265
259
EventRegistrationBuilderCollection regCollection ,
266
260
Collection <EventRegistration > registrations ,
267
261
BiConsumer <CloudEvent , CompletableFuture <T >> consumer ) {
268
- return regCollection .registrations ().stream ()
269
- .map (reg -> toCompletable (reg , registrations , consumer ))
270
- .toArray (size -> new CompletableFuture [size ]);
262
+ CompletableFuture <T >[] futures =
263
+ regCollection .registrations ().stream ()
264
+ .map (reg -> toCompletable (reg , registrations , consumer ))
265
+ .toArray (size -> new CompletableFuture [size ]);
266
+ return regCollection .isAnd ()
267
+ ? CompletableFuture .allOf (futures )
268
+ : CompletableFuture .anyOf (futures );
271
269
}
272
270
273
271
private <T > CompletableFuture <T > toCompletable (
@@ -307,9 +305,7 @@ protected ListenExecutor(ListenExecutorBuilder builder) {
307
305
super (builder );
308
306
this .eventConsumer = builder .application .eventConsumer ();
309
307
this .regBuilders = builder .registrations ;
310
- this .until = Optional .ofNullable (builder .until );
311
308
this .loop = Optional .ofNullable (builder .loop );
312
309
this .converter = builder .converter ;
313
- this .untilRegBuilders = builder .untilRegistrations ;
314
310
}
315
311
}
0 commit comments