Search in sources :

Example 11 with Processor

use of org.mule.runtime.core.api.processor.Processor in project mule by mulesoft.

the class PolicyNextActionMessageProcessor method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    return from(publisher).doOnNext(coreEvent -> logExecuteNextEvent("Before execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId())).flatMap(event -> {
        Processor nextOperation = policyStateHandler.retrieveNextOperation(event.getContext().getCorrelationId());
        if (nextOperation == null) {
            return error(new MuleRuntimeException(createStaticMessage("There's no next operation configured for event context id " + event.getContext().getCorrelationId())));
        }
        popBeforeNextFlowFlowStackElement().accept(event);
        notificationHelper.notification(BEFORE_NEXT).accept(event);
        return from(processWithChildContext(event, nextOperation, ofNullable(getLocation()))).doOnSuccessOrError(notificationHelper.successOrErrorNotification(AFTER_NEXT).andThen((ev, t) -> pushAfterNextFlowStackElement().accept(event))).onErrorResume(MessagingException.class, t -> {
            PolicyStateId policyStateId = new PolicyStateId(event.getContext().getCorrelationId(), muleContext.getConfiguration().getId());
            policyStateHandler.getLatestState(policyStateId).ifPresent(latestStateEvent -> t.setProcessedEvent(policyEventConverter.createEvent((PrivilegedEvent) t.getEvent(), (PrivilegedEvent) latestStateEvent)));
            // Given we've used child context to ensure AFTER_NEXT notifications are fired at exactly the right time we need
            // to propagate the error to parent context manually.
            ((BaseEventContext) event.getContext()).error(resolveMessagingException(t.getFailingComponent(), muleContext).apply(t));
            return empty();
        }).doOnNext(coreEvent -> logExecuteNextEvent("After execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId()));
    });
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) BEFORE_NEXT(org.mule.runtime.api.notification.PolicyNotification.BEFORE_NEXT) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) Mono.from(reactor.core.publisher.Mono.from) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) Mono.error(reactor.core.publisher.Mono.error) AFTER_NEXT(org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT) EventContext(org.mule.runtime.api.event.EventContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Consumer(java.util.function.Consumer) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) DefaultFlowCallStack(org.mule.runtime.core.internal.context.notification.DefaultFlowCallStack) FlowStackElement(org.mule.runtime.core.api.context.notification.FlowStackElement) Processor(org.mule.runtime.core.api.processor.Processor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 12 with Processor

use of org.mule.runtime.core.api.processor.Processor in project mule by mulesoft.

the class AbstractMessageProcessorChain method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    List<BiFunction<Processor, ReactiveProcessor, ReactiveProcessor>> interceptors = resolveInterceptors();
    Flux<CoreEvent> stream = from(publisher);
    for (Processor processor : getProcessorsToExecute()) {
        // Perform assembly for processor chain by transforming the existing publisher with a publisher function for each processor
        // along with the interceptors that decorate it.
        stream = stream.transform(applyInterceptors(interceptors, processor)).subscriberContext(context -> context.put(REACTOR_ON_OPERATOR_ERROR_LOCAL, getLocalOperatorErrorHook(processor))).errorStrategyContinue(getContinueStrategyErrorHandler(processor));
    }
    return stream.subscriberContext(ctx -> {
        ClassLoader tccl = currentThread().getContextClassLoader();
        if (tccl == null || tccl.getParent() == null || appClClass == null || !appClClass.isAssignableFrom(tccl.getClass())) {
            return ctx;
        } else {
            return ctx.put(TCCL_ORIGINAL_REACTOR_CTX_KEY, tccl).put(TCCL_REACTOR_CTX_KEY, tccl.getParent());
        }
    });
}
Also used : InterceptedReactiveProcessor(org.mule.runtime.core.internal.processor.chain.InterceptedReactiveProcessor) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Processor(org.mule.runtime.core.api.processor.Processor) BiFunction(java.util.function.BiFunction) CoreEvent(org.mule.runtime.core.api.event.CoreEvent)

Example 13 with Processor

use of org.mule.runtime.core.api.processor.Processor in project mule by mulesoft.

the class AbstractMessageProcessorChain method toString.

@Override
public String toString() {
    StringBuilder string = new StringBuilder();
    string.append(getClass().getSimpleName());
    if (!isBlank(name)) {
        string.append(format(" '%s' ", name));
    }
    Iterator<Processor> mpIterator = processors.iterator();
    final String nl = format("%n");
    // TODO have it print the nested structure with indents increasing for nested MPCs
    if (mpIterator.hasNext()) {
        string.append(format("%n[ "));
        while (mpIterator.hasNext()) {
            Processor mp = mpIterator.next();
            final String indented = replace(mp.toString(), nl, format("%n  "));
            string.append(format("%n  %s", indented));
            if (mpIterator.hasNext()) {
                string.append(", ");
            }
        }
        string.append(format("%n]"));
    }
    return string.toString();
}
Also used : InterceptedReactiveProcessor(org.mule.runtime.core.internal.processor.chain.InterceptedReactiveProcessor) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Processor(org.mule.runtime.core.api.processor.Processor)

Example 14 with Processor

use of org.mule.runtime.core.api.processor.Processor in project mule by mulesoft.

the class MessageProcessorChainObjectFactory method doGetObject.

@Override
public MessageProcessorChain doGetObject() throws Exception {
    MessageProcessorChainBuilder builder = getBuilderInstance();
    for (Object processor : processors) {
        if (processor instanceof Processor) {
            builder.chain((Processor) processor);
        } else {
            throw new IllegalArgumentException(format("MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured. Found a %s", processor.getClass().getName()));
        }
    }
    MessageProcessorChain messageProcessorChain = builder.build();
    messageProcessorChain.setMuleContext(muleContext);
    return messageProcessorChain;
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Processor(org.mule.runtime.core.api.processor.Processor) DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) MessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChainBuilder)

Example 15 with Processor

use of org.mule.runtime.core.api.processor.Processor in project mule by mulesoft.

the class ProcessorChainRouter method initialise.

@Override
public void initialise() throws InitialisationException {
    DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
    builder.setName("processor chain '" + name + "'");
    for (Object processor : processors) {
        if (processor instanceof Processor) {
            builder.chain((Processor) processor);
        } else if (processor instanceof MessageProcessorBuilder) {
            builder.chain((MessageProcessorBuilder) processor);
        } else {
            throw new IllegalArgumentException("MessageProcessorBuilder should only have MessageProcessor's or MessageProcessorBuilder's configured");
        }
    }
    processorChain = builder.build();
    initialiseIfNeeded(processorChain, muleContext);
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Processor(org.mule.runtime.core.api.processor.Processor)

Aggregations

Processor (org.mule.runtime.core.api.processor.Processor)58 Test (org.junit.Test)31 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)24 MuleException (org.mule.runtime.api.exception.MuleException)15 ReactiveProcessor (org.mule.runtime.core.api.processor.ReactiveProcessor)14 Component (org.mule.runtime.api.component.Component)12 ArrayList (java.util.ArrayList)11 InternalProcessor (org.mule.runtime.core.privileged.processor.InternalProcessor)11 Publisher (org.reactivestreams.Publisher)11 Message (org.mule.runtime.api.message.Message)10 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)10 Map (java.util.Map)9 Arrays.asList (java.util.Arrays.asList)8 Flow (org.mule.runtime.core.api.construct.Flow)8 Inject (javax.inject.Inject)7 Assert.assertThat (org.junit.Assert.assertThat)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.when (org.mockito.Mockito.when)7 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)7 Disposable (org.mule.runtime.api.lifecycle.Disposable)7