Search in sources :

Example 41 with Processor

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

the class RoundRobin method route.

/**
 * Process the event using the next target route in sequence
 */
@Override
public CoreEvent route(CoreEvent event) throws MuleException {
    int modulo = getAndIncrementModuloN(routes.size());
    if (modulo < 0) {
        throw new CouldNotRouteOutboundMessageException(this);
    }
    Processor mp = routes.get(modulo);
    try {
        return doProcessRoute(mp, event);
    } catch (MuleException ex) {
        throw new RoutingException(this, ex);
    }
}
Also used : CouldNotRouteOutboundMessageException(org.mule.runtime.core.privileged.routing.CouldNotRouteOutboundMessageException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) Processor(org.mule.runtime.core.api.processor.Processor) MuleException(org.mule.runtime.api.exception.MuleException)

Example 42 with Processor

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

the class AbstractMessageProcessorChain method start.

@Override
public void start() throws MuleException {
    List<Processor> startedProcessors = new ArrayList<>();
    try {
        for (Processor processor : getMessageProcessorsForLifecycle()) {
            if (processor instanceof Startable) {
                ((Startable) processor).start();
                startedProcessors.add(processor);
            }
        }
    } catch (MuleException e) {
        stopIfNeeded(getMessageProcessorsForLifecycle());
        throw e;
    }
}
Also used : Startable(org.mule.runtime.api.lifecycle.Startable) 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) ArrayList(java.util.ArrayList) MuleException(org.mule.runtime.api.exception.MuleException)

Example 43 with Processor

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

the class DefaultMessageProcessorChainBuilder method build.

/**
 * This builder supports the chaining together of message processors that intercept and also those that don't. While one can
 * iterate over message processor intercepting message processors need to be chained together. One solution is make all message
 * processors intercepting (via adaption) and chain them all together, this results in huge stack traces and recursive calls
 * with adaptor. The alternative is to build the chain in such a way that we iterate when we can and chain where we need to.
 * <br>
 * We iterate over the list of message processor to be chained together in reverse order collecting up those that can be
 * iterated over in a temporary list, as soon as we have an intercepting message processor we create a
 * DefaultMessageProcessorChain using the temporary list and set it as a listener of the intercepting message processor and then
 * we continue with the algorithm
 */
@Override
public MessageProcessorChain build() {
    LinkedList<Processor> tempList = new LinkedList<>();
    final LinkedList<Processor> processorsForLifecycle = new LinkedList<>();
    // Start from last but one message processor and work backwards
    for (int i = processors.size() - 1; i >= 0; i--) {
        Processor processor = initializeMessageProcessor(processors.get(i));
        if (processor instanceof InterceptingMessageProcessor && (!(processor instanceof ReferenceProcessor) || ((ReferenceProcessor) processor).getReferencedProcessor() instanceof InterceptingMessageProcessor)) {
            InterceptingMessageProcessor interceptingProcessor = (InterceptingMessageProcessor) processor;
            // Processor is intercepting so we can't simply iterate
            if (i + 1 < processors.size()) {
                // Wrap processors in chain, unless single processor that is already a chain
                final MessageProcessorChain innerChain = createSimpleChain(tempList, interceptingProcessor.isBlocking() ? of(BLOCKING_PROCESSING_STRATEGY_INSTANCE) : ofNullable(processingStrategy));
                processorsForLifecycle.addFirst(innerChain);
                interceptingProcessor.setListener(innerChain);
            }
            tempList = new LinkedList<>(singletonList(processor));
        } else {
            // Processor is not intercepting so we can invoke it using iteration
            // (add to temp list)
            tempList.addFirst(processor);
        }
    }
    // Create the final chain using the current tempList after reserve iteration is complete. This temp
    // list contains the first n processors in the chain that are not intercepting.. with processor n+1
    // having been injected as the listener of processor n
    Processor head = tempList.size() == 1 ? tempList.get(0) : createSimpleChain(tempList, ofNullable(processingStrategy));
    processorsForLifecycle.addFirst(head);
    return createInterceptingChain(head, processors, processorsForLifecycle);
}
Also used : Processor(org.mule.runtime.core.api.processor.Processor) ReferenceProcessor(org.mule.runtime.core.internal.processor.ReferenceProcessor) InterceptingMessageProcessor(org.mule.runtime.core.api.processor.InterceptingMessageProcessor) ReferenceProcessor(org.mule.runtime.core.internal.processor.ReferenceProcessor) InterceptingMessageProcessor(org.mule.runtime.core.api.processor.InterceptingMessageProcessor) LinkedList(java.util.LinkedList)

Example 44 with Processor

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

the class ModuleOperationMessageProcessorChainFactoryBean 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()));
        }
    }
    final MessageProcessorChain messageProcessorChain = newLazyProcessorChainBuilder((ModuleOperationMessageProcessorChainBuilder) builder, muleContext, () -> getProcessingStrategy(locator, getRootContainerLocation()).orElse(null));
    messageProcessorChain.setAnnotations(getAnnotations());
    messageProcessorChain.setMuleContext(muleContext);
    return messageProcessorChain;
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Processor(org.mule.runtime.core.api.processor.Processor) MessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChainBuilder) ModuleOperationMessageProcessorChainBuilder(org.mule.runtime.core.internal.processor.chain.ModuleOperationMessageProcessorChainBuilder)

Example 45 with Processor

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

the class ResponseMessageProcessorsFactoryBean method doGetObject.

@Override
public ResponseMessageProcessorAdapter doGetObject() throws Exception {
    DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
    builder.setName("'response' child processor chain");
    for (Object processor : messageProcessors) {
        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");
        }
    }
    ResponseMessageProcessorAdapter responseAdapter = new ResponseMessageProcessorAdapter();
    responseAdapter.setProcessor(newLazyProcessorChainBuilder(builder, muleContext, () -> getProcessingStrategy(locator, getRootContainerLocation()).orElse(null)));
    responseAdapter.setMuleContext(muleContext);
    return responseAdapter;
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) ResponseMessageProcessorAdapter(org.mule.runtime.core.internal.processor.ResponseMessageProcessorAdapter) Processor(org.mule.runtime.core.api.processor.Processor) MessageProcessorBuilder(org.mule.runtime.core.privileged.processor.MessageProcessorBuilder)

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