use of org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer in project mule by mulesoft.
the class Foreach method splitAndProcess.
private Publisher<CoreEvent> splitAndProcess(CoreEvent request) {
AtomicInteger count = new AtomicInteger();
final AtomicReference<CoreEvent> currentEvent = new AtomicReference<>(request);
// Split into sequence of TypedValue
return fromIterable(() -> splitRequest(request)).onErrorMap(throwable -> new MessagingException(request, throwable, Foreach.this)).transform(p -> batchSize > 1 ? from(p).buffer(batchSize).map(list -> new TypedValue<>(list, fromObject(list))) : p).flatMapSequential(typedValue -> {
EventContext parentContext = currentEvent.get().getContext();
BaseEventContext childContext = newChildContext(currentEvent.get(), ofNullable(getLocation()));
Builder partEventBuilder = builder(childContext, currentEvent.get());
if (typedValue.getValue() instanceof EventBuilderConfigurer) {
// Support EventBuilderConfigurer currently used by Batch Module
EventBuilderConfigurer configurer = (EventBuilderConfigurer) typedValue.getValue();
configurer.configure(partEventBuilder);
childContext.onResponse((e, t) -> {
configurer.eventCompleted();
});
} else if (typedValue.getValue() instanceof Message) {
// If value is a Message then use it directly conserving attributes and properties.
partEventBuilder.message((Message) typedValue.getValue());
} else {
// Otherwise create a new message
partEventBuilder.message(Message.builder().payload(typedValue).build());
}
return Mono.from(just(partEventBuilder.addVariable(counterVariableName, count.incrementAndGet()).build()).transform(nestedChain).doOnNext(completeSuccessIfNeeded(childContext, true)).switchIfEmpty(Mono.from(childContext.getResponsePublisher())).map(result -> builder(parentContext, result).build()).doOnNext(result -> currentEvent.set(CoreEvent.builder(result).build())).doOnError(MessagingException.class, me -> me.setProcessedEvent(builder(parentContext, me.getEvent()).build())).doOnSuccess(result -> {
if (result == null) {
childContext.success();
}
}));
}, // Force sequential execution of the chain for each element
1).switchIfEmpty(defer(() -> {
if (count.get() == 0) {
logger.warn("Split expression returned no results. If this is not expected please check your expression");
return just(request);
} else {
return empty();
}
})).takeLast(1).map(s -> CoreEvent.builder(currentEvent.get()).message(request.getMessage()).build()).errorStrategyStop();
}
Aggregations