Search in sources :

Example 1 with MessageSource

use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.

the class DefaultFlowBuilderTestCase method buildsFullFlow.

@Test
public void buildsFullFlow() throws Exception {
    Processor processor1 = mock(Processor.class);
    Processor processor2 = mock(Processor.class);
    List<Processor> messageProcessors = new ArrayList<>();
    messageProcessors.add(processor1);
    messageProcessors.add(processor2);
    MessageSource messageSource = mock(MessageSource.class);
    ProcessingStrategyFactory processingStrategyFactory = mock(ProcessingStrategyFactory.class);
    ProcessingStrategy processingStrategy = mock(ProcessingStrategy.class);
    when(processingStrategyFactory.create(any(), any())).thenReturn(processingStrategy);
    FlowExceptionHandler exceptionListener = mock(FlowExceptionHandler.class);
    Flow flow = flowBuilder.processors(messageProcessors).source(messageSource).processingStrategyFactory(processingStrategyFactory).messagingExceptionHandler(exceptionListener).build();
    assertThat(flow.getName(), equalTo(FLOW_NAME));
    assertThat(flow.getMuleContext(), is(muleContext));
    assertThat(flow.getProcessors(), contains(processor1, processor2));
    assertThat(flow.getSource(), is(messageSource));
    assertThat(flow.getExceptionListener(), is(exceptionListener));
    assertThat(flow.getProcessingStrategy(), sameInstance(processingStrategy));
}
Also used : Processor(org.mule.runtime.core.api.processor.Processor) ArrayList(java.util.ArrayList) ProcessingStrategyFactory(org.mule.runtime.core.api.processor.strategy.ProcessingStrategyFactory) MessageSource(org.mule.runtime.core.api.source.MessageSource) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Flow(org.mule.runtime.core.api.construct.Flow) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 2 with MessageSource

use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.

the class ModuleFlowProcessingPhase method runPhase.

@Override
public void runPhase(final ModuleFlowProcessingPhaseTemplate template, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    try {
        final MessageSource messageSource = messageProcessContext.getMessageSource();
        final FlowConstruct flowConstruct = (FlowConstruct) componentLocator.find(messageSource.getRootContainerLocation()).get();
        final ComponentLocation sourceLocation = messageSource.getLocation();
        final Consumer<Either<MessagingException, CoreEvent>> terminateConsumer = getTerminateConsumer(messageSource, template);
        final CompletableFuture<Void> responseCompletion = new CompletableFuture<>();
        final CoreEvent templateEvent = createEvent(template, sourceLocation, responseCompletion, flowConstruct);
        try {
            FlowProcessor flowExecutionProcessor = new FlowProcessor(template, flowConstruct.getExceptionListener(), templateEvent);
            flowExecutionProcessor.setAnnotations(flowConstruct.getAnnotations());
            final SourcePolicy policy = policyManager.createSourcePolicyInstance(messageSource, templateEvent, flowExecutionProcessor, template);
            final PhaseContext phaseContext = new PhaseContext(template, messageProcessContext, phaseResultNotifier, terminateConsumer);
            just(templateEvent).doOnNext(onMessageReceived(template, messageProcessContext, flowConstruct)).flatMap(request -> from(policy.process(request))).flatMap(policyResult -> policyResult.reduce(policyFailure(phaseContext, flowConstruct, messageSource), policySuccess(phaseContext, flowConstruct, messageSource))).doOnSuccess(aVoid -> phaseResultNotifier.phaseSuccessfully()).doOnError(onFailure(flowConstruct, messageSource, phaseResultNotifier, terminateConsumer)).doAfterTerminate(() -> responseCompletion.complete(null)).subscribe();
        } catch (Exception e) {
            from(template.sendFailureResponseToClient(new MessagingExceptionResolver(messageProcessContext.getMessageSource()).resolve(new MessagingException(templateEvent, e), muleContext), template.getFailedExecutionResponseParametersFunction().apply(templateEvent))).doOnTerminate(() -> phaseResultNotifier.phaseFailure(e)).subscribe();
        }
    } catch (Exception t) {
        phaseResultNotifier.phaseFailure(t);
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) FunctionalUtils.safely(org.mule.runtime.core.internal.util.FunctionalUtils.safely) Map(java.util.Map) SOURCE_ERROR_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_GENERATE) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) SOURCE_ERROR_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_SEND) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) Collection(java.util.Collection) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Message.of(org.mule.runtime.api.message.Message.of) Either.left(org.mule.runtime.core.api.functional.Either.left) ErrorBuilder.builder(org.mule.runtime.core.internal.message.ErrorBuilder.builder) Either.right(org.mule.runtime.core.api.functional.Either.right) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) MESSAGE_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RESPONSE) MessageUtils.toMessage(org.mule.runtime.core.internal.util.message.MessageUtils.toMessage) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) SOURCE_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_SEND) SourcePolicySuccessResult(org.mule.runtime.core.internal.policy.SourcePolicySuccessResult) MessageSource(org.mule.runtime.core.api.source.MessageSource) MessageProcessContext(org.mule.runtime.core.privileged.execution.MessageProcessContext) CompletableFuture(java.util.concurrent.CompletableFuture) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) MessageUtils.toMessageCollection(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) MuleException(org.mule.runtime.api.exception.MuleException) SourcePolicyFailureResult(org.mule.runtime.core.internal.policy.SourcePolicyFailureResult) Mono.from(reactor.core.publisher.Mono.from) SOURCE_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_GENERATE) Mono.error(reactor.core.publisher.Mono.error) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) MESSAGE_RECEIVED(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RECEIVED) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) MESSAGE_ERROR_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_ERROR_RESPONSE) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Mono.when(reactor.core.publisher.Mono.when) TypedValue(org.mule.runtime.api.metadata.TypedValue) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) MessageProcessTemplate(org.mule.runtime.core.privileged.execution.MessageProcessTemplate) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Builder(org.mule.runtime.core.internal.message.InternalEvent.Builder) ANY(org.mule.runtime.api.metadata.MediaType.ANY) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) MessageSource(org.mule.runtime.core.api.source.MessageSource) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Either(org.mule.runtime.core.api.functional.Either) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver)

Example 3 with MessageSource

use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.

the class DefaultFlowTestCase method testFailStartingMessageSourceOnLifecycleShouldStopStartedPipelineProcesses.

@Test
public void testFailStartingMessageSourceOnLifecycleShouldStopStartedPipelineProcesses() throws Exception {
    // Need to start mule context to have endpoints started during flow start
    muleContext.start();
    MessageSource mockMessageSource = mock(MessageSource.class, withSettings().extraInterfaces(Startable.class, Stoppable.class));
    doThrow(new LifecycleException(mock(I18nMessage.class), mockMessageSource)).when(((Startable) mockMessageSource)).start();
    final List<Processor> processors = new ArrayList<>(flow.getProcessors());
    Processor mockMessageProcessor = spy(new LifecycleTrackerProcessor());
    processors.add(mockMessageProcessor);
    after();
    flow = (DefaultFlow) Flow.builder(FLOW_NAME, muleContext).source(mockMessageSource).processors(processors).build();
    flow.initialise();
    try {
        flow.start();
        fail();
    } catch (LifecycleException e) {
    }
    verify((Startable) mockMessageProcessor, times(1)).start();
    verify((Stoppable) mockMessageProcessor, times(1)).stop();
    verify((Startable) mockMessageSource, times(1)).start();
    verify((Stoppable) mockMessageSource, times(1)).stop();
}
Also used : Startable(org.mule.runtime.api.lifecycle.Startable) LifecycleException(org.mule.runtime.api.lifecycle.LifecycleException) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) LifecycleTrackerProcessor(org.mule.tck.core.lifecycle.LifecycleTrackerProcessor) Processor(org.mule.runtime.core.api.processor.Processor) LifecycleTrackerProcessor(org.mule.tck.core.lifecycle.LifecycleTrackerProcessor) ArrayList(java.util.ArrayList) MessageSource(org.mule.runtime.core.api.source.MessageSource) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) Test(org.junit.Test)

Example 4 with MessageSource

use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.

the class AsyncRequestReplyRequesterTestCase method returnsNullWhenInterruptedWhileWaitingForReply.

@Test
@Ignore("See MULE-8830")
public void returnsNullWhenInterruptedWhileWaitingForReply() throws Exception {
    final Latch fakeLatch = new Latch() {

        @Override
        public void await() throws InterruptedException {
            throw new InterruptedException();
        }
    };
    asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext) {

        @Override
        protected Latch createEventLock() {
            return fakeLatch;
        }
    };
    final CountDownLatch processingLatch = new CountDownLatch(1);
    Processor target = mock(Processor.class);
    asyncReplyMP.setListener(target);
    MessageSource messageSource = mock(MessageSource.class);
    asyncReplyMP.setReplySource(messageSource);
    asyncReplyMP.setMuleContext(muleContext);
    final boolean[] exceptionThrown = new boolean[1];
    final Object[] responseEvent = new Object[1];
    Thread thread = new Thread(() -> {
        try {
            responseEvent[0] = asyncReplyMP.process(testEvent());
        } catch (MuleException e) {
            exceptionThrown[0] = true;
        } finally {
            processingLatch.countDown();
        }
    });
    thread.start();
    assertTrue(processingLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
    assertFalse(exceptionThrown[0]);
    assertNull(responseEvent[0]);
}
Also used : SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) Processor(org.mule.runtime.core.api.processor.Processor) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) CountDownLatch(java.util.concurrent.CountDownLatch) Latch(org.mule.runtime.api.util.concurrent.Latch) MessageSource(org.mule.runtime.core.api.source.MessageSource) CountDownLatch(java.util.concurrent.CountDownLatch) MuleException(org.mule.runtime.api.exception.MuleException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with MessageSource

use of org.mule.runtime.core.api.source.MessageSource in project mule by mulesoft.

the class BackPressureConfigTestCase method assertStrategy.

private void assertStrategy(String flowName, BackPressureStrategy expected) {
    MessageSource source = (MessageSource) componentLocator.find(builderFromStringRepresentation(flowName + "/source").build()).get();
    assertThat(source.getBackPressureStrategy(), is(expected));
}
Also used : MessageSource(org.mule.runtime.core.api.source.MessageSource)

Aggregations

MessageSource (org.mule.runtime.core.api.source.MessageSource)7 Processor (org.mule.runtime.core.api.processor.Processor)5 FlowExceptionHandler (org.mule.runtime.core.api.exception.FlowExceptionHandler)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Map (java.util.Map)2 Function (java.util.function.Function)2 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)2 MuleException (org.mule.runtime.api.exception.MuleException)2 AsyncDelegateMessageProcessor (org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor)2 SensingNullMessageProcessor (org.mule.tck.SensingNullMessageProcessor)2 Boolean.parseBoolean (java.lang.Boolean.parseBoolean)1 String.valueOf (java.lang.String.valueOf)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 TreeMap (java.util.TreeMap)1