4747import java .util .List ;
4848import java .util .Optional ;
4949import java .util .concurrent .CompletableFuture ;
50- import java .util .concurrent .atomic .AtomicBoolean ;
5150import java .util .function .BiConsumer ;
5251import java .util .function .Function ;
5352import java .util .stream .Collectors ;
5453
5554public abstract class ListenExecutor extends RegularTaskExecutor <ListenTask > {
5655
5756 protected final EventRegistrationBuilderCollection regBuilders ;
58- protected final EventRegistrationBuilderCollection untilRegBuilders ;
59- protected final Optional <WorkflowFilter > until ;
6057 protected final Optional <TaskExecutor <?>> loop ;
6158 protected final Function <CloudEvent , JsonNode > converter ;
6259 protected final EventConsumer eventConsumer ;
63- protected final AtomicBoolean untilEvent = new AtomicBoolean (true );
6460
6561 private static record EventRegistrationBuilderCollection (
6662 Collection <EventRegistrationBuilder > registrations , boolean isAnd ) {}
@@ -177,22 +173,37 @@ protected void internalProcessCe(
177173 arrayNode .add (node );
178174 future .complete (node );
179175 }
180-
181- @ Override
182- protected CompletableFuture <?> combine (CompletableFuture <JsonNode >[] completables ) {
183- return CompletableFuture .allOf (completables );
184- }
185176 }
186177
187178 public static class OrListenExecutor extends ListenExecutor {
188179
180+ private final Optional <WorkflowFilter > until ;
181+ private final EventRegistrationBuilderCollection untilRegBuilders ;
182+
189183 public OrListenExecutor (ListenExecutorBuilder builder ) {
190184 super (builder );
185+ this .until = Optional .ofNullable (builder .until );
186+ this .untilRegBuilders = builder .untilRegistrations ;
191187 }
192188
193189 @ 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 ;
196207 }
197208
198209 protected void internalProcessCe (
@@ -206,14 +217,12 @@ protected void internalProcessCe(
206217 || until
207218 .filter (u -> u .apply (workflow , taskContext , arrayNode ).asBoolean ())
208219 .isPresent ())
209- && untilEvent . get () ) {
220+ && untilRegBuilders == null ) {
210221 future .complete (arrayNode );
211222 }
212223 }
213224 }
214225
215- protected abstract CompletableFuture <?> combine (CompletableFuture <JsonNode >[] completables );
216-
217226 protected abstract void internalProcessCe (
218227 JsonNode node ,
219228 ArrayNode arrayNode ,
@@ -226,48 +235,37 @@ protected CompletableFuture<JsonNode> internalExecute(
226235 WorkflowContext workflow , TaskContext taskContext ) {
227236 ArrayNode output = JsonUtils .mapper ().createArrayNode ();
228237 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 (
241245 v -> {
242246 registrations .forEach (reg -> eventConsumer .unregister (reg ));
243247 return output ;
244248 });
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 ;
262249 }
263250
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 (
265259 EventRegistrationBuilderCollection regCollection ,
266260 Collection <EventRegistration > registrations ,
267261 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 );
271269 }
272270
273271 private <T > CompletableFuture <T > toCompletable (
@@ -307,9 +305,7 @@ protected ListenExecutor(ListenExecutorBuilder builder) {
307305 super (builder );
308306 this .eventConsumer = builder .application .eventConsumer ();
309307 this .regBuilders = builder .registrations ;
310- this .until = Optional .ofNullable (builder .until );
311308 this .loop = Optional .ofNullable (builder .loop );
312309 this .converter = builder .converter ;
313- this .untilRegBuilders = builder .untilRegistrations ;
314310 }
315311}
0 commit comments