Search in sources :

Example 1 with EventBuilderConfigurer

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();
}
Also used : MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Message(org.mule.runtime.api.message.Message) Flux.from(reactor.core.publisher.Flux.from) Mono.defer(reactor.core.publisher.Mono.defer) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) Processor(org.mule.runtime.core.api.processor.Processor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Iterators(com.google.common.collect.Iterators) Collections.singletonList(java.util.Collections.singletonList) Scope(org.mule.runtime.core.privileged.processor.Scope) MuleException(org.mule.runtime.api.exception.MuleException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProcessors.newChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.newChildContext) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) Map(java.util.Map) Mono.just(reactor.core.publisher.Mono.just) AbstractMessageProcessorOwner(org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Mono.empty(reactor.core.publisher.Mono.empty) DEFAULT_SPLIT_EXPRESSION(org.mule.runtime.core.internal.routing.ExpressionSplittingStrategy.DEFAULT_SPLIT_EXPRESSION) EventContext(org.mule.runtime.api.event.EventContext) Iterator(java.util.Iterator) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) Optional.ofNullable(java.util.Optional.ofNullable) MessageProcessors.completeSuccessIfNeeded(org.mule.runtime.core.privileged.processor.MessageProcessors.completeSuccessIfNeeded) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) EventBuilderConfigurerList(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerList) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) TypedValue(org.mule.runtime.api.metadata.TypedValue) List(java.util.List) Optional(java.util.Optional) EventBuilderConfigurerIterator(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurerIterator) MessageProcessors.getProcessingStrategy(org.mule.runtime.core.privileged.processor.MessageProcessors.getProcessingStrategy) EventContext(org.mule.runtime.api.event.EventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) EventBuilderConfigurer(org.mule.runtime.core.internal.routing.outbound.EventBuilderConfigurer) Message(org.mule.runtime.api.message.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Builder(org.mule.runtime.core.api.event.CoreEvent.Builder) AtomicReference(java.util.concurrent.atomic.AtomicReference) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

Iterators (com.google.common.collect.Iterators)1 Collections.singletonList (java.util.Collections.singletonList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 EventContext (org.mule.runtime.api.event.EventContext)1 MuleException (org.mule.runtime.api.exception.MuleException)1 Initialisable (org.mule.runtime.api.lifecycle.Initialisable)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 Message (org.mule.runtime.api.message.Message)1 DataType.fromObject (org.mule.runtime.api.metadata.DataType.fromObject)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 Builder (org.mule.runtime.core.api.event.CoreEvent.Builder)1 CoreEvent.builder (org.mule.runtime.core.api.event.CoreEvent.builder)1 AbstractMessageProcessorOwner (org.mule.runtime.core.api.processor.AbstractMessageProcessorOwner)1