Search in sources :

Example 1 with AsyncDelegateMessageProcessor

use of org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor in project mule by mulesoft.

the class AsyncRequestReplyRequesterTestCase method testMultiple.

@Test
@Ignore("See MULE-8830")
public void testMultiple() throws Exception {
    asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
    SensingNullMessageProcessor target = getSensingNullMessageProcessor();
    target.setWaitTime(50);
    AsyncDelegateMessageProcessor asyncMP = createAsyncMessageProcessor(target);
    asyncMP.initialise();
    asyncReplyMP.setListener(asyncMP);
    asyncReplyMP.setReplySource(target.getMessageSource());
    final AtomicInteger count = new AtomicInteger();
    for (int i = 0; i < 500; i++) {
        scheduler.execute(() -> {
            try {
                CoreEvent resultEvent = asyncReplyMP.process(testEvent());
                // Can't assert same because we copy event for async currently
                assertEquals(((PrivilegedEvent) testEvent()).getMessageAsString(muleContext), ((PrivilegedEvent) resultEvent).getMessageAsString(muleContext));
                count.incrementAndGet();
                LOGGER.debug("Finished " + count.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
    new PollingProber().check(new JUnitLambdaProbe(() -> {
        assertThat(count.get(), greaterThanOrEqualTo(500));
        return true;
    }));
}
Also used : JUnitLambdaProbe(org.mule.tck.probe.JUnitLambdaProbe) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) PollingProber(org.mule.tck.probe.PollingProber) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) MuleException(org.mule.runtime.api.exception.MuleException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with AsyncDelegateMessageProcessor

use of org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor in project mule by mulesoft.

the class CompositeProcessorChainRouterTestCase method asyncDelegateChain.

@Test
@Description("Ensure that when an async scope is used as part of the execution of one of the composite chains then the chain does not complete and the next chains is not executed until the child context completes.")
public void asyncDelegateChain() throws Exception {
    Latch latch = new Latch();
    Latch asyncLatch = new Latch();
    DefaultMessageProcessorChainBuilder delegateBuilder = new DefaultMessageProcessorChainBuilder();
    delegateBuilder.chain(event -> {
        try {
            asyncLatch.countDown();
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return event;
    });
    async = new AsyncDelegateMessageProcessor(delegateBuilder);
    muleContext.getInjector().inject(async);
    async.setAnnotations(getAppleFlowComponentLocationAnnotations());
    chainRouter = createCompositeProcessorChainRouter(newChain(empty(), async), newChain(empty(), event -> event));
    chainRouter.start();
    // CompletableFuture is not returned immediately because simply invoking CompositeProcessorChainRouter there is no async
    // hand-off and so this blocks until child context completes.
    Future<CompletableFuture<Event>> future = scheduler.submit(() -> chainRouter.execute(testEvent()));
    asyncLatch.await();
    try {
        future.get(BLOCK_TIMEOUT, MILLISECONDS);
        fail("Timeout expected");
    } catch (TimeoutException te) {
    }
    latch.countDown();
    assertThat(future.get(BLOCK_TIMEOUT, MILLISECONDS).get().getMessage(), equalTo(testEvent().getMessage()));
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) Latch(org.mule.runtime.api.util.concurrent.Latch) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) TimeoutException(java.util.concurrent.TimeoutException) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 3 with AsyncDelegateMessageProcessor

use of org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor in project mule by mulesoft.

the class AsyncMessageProcessorsFactoryBean method getObject.

@Override
public AsyncDelegateMessageProcessor getObject() throws Exception {
    DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
    builder.setName("'async' child 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");
        }
    }
    AsyncDelegateMessageProcessor delegate = new AsyncDelegateMessageProcessor(builder, name);
    delegate.setAnnotations(getAnnotations());
    if (getMaxConcurrency() != null) {
        delegate.setMaxConcurrency(getMaxConcurrency());
    }
    return delegate;
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) Processor(org.mule.runtime.core.api.processor.Processor) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) NameableObject(org.mule.runtime.api.meta.NameableObject) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) MessageProcessorBuilder(org.mule.runtime.core.privileged.processor.MessageProcessorBuilder)

Example 4 with AsyncDelegateMessageProcessor

use of org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor in project mule by mulesoft.

the class AsyncRequestReplyRequesterTestCase method createAsyncMessageProcessor.

protected AsyncDelegateMessageProcessor createAsyncMessageProcessor(SensingNullMessageProcessor target) throws InitialisationException {
    DefaultMessageProcessorChainBuilder delegateBuilder = new DefaultMessageProcessorChainBuilder();
    delegateBuilder.chain(target);
    asyncMP = new AsyncDelegateMessageProcessor(delegateBuilder);
    asyncMP.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(asyncMP, true, muleContext);
    return asyncMP;
}
Also used : DefaultMessageProcessorChainBuilder(org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor)

Example 5 with AsyncDelegateMessageProcessor

use of org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor in project mule by mulesoft.

the class AsyncRequestReplyRequesterTestCase method testSingleEventTimeout.

@Test
public void testSingleEventTimeout() throws Exception {
    asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
    asyncReplyMP.setTimeout(1);
    SensingNullMessageProcessor target = getSensingNullMessageProcessor();
    target.setWaitTime(30000);
    AsyncDelegateMessageProcessor asyncMP = createAsyncMessageProcessor(target);
    initialiseIfNeeded(asyncMP, true, muleContext);
    asyncMP.start();
    asyncReplyMP.setListener(asyncMP);
    asyncReplyMP.setReplySource(target.getMessageSource());
    asyncReplyMP.setMuleContext(muleContext);
    CoreEvent event = eventBuilder(muleContext).message(of(TEST_MESSAGE)).build();
    try {
        asyncReplyMP.process(event);
        fail("ResponseTimeoutException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(ResponseTimeoutException.class));
    }
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) AsyncDelegateMessageProcessor(org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) MuleException(org.mule.runtime.api.exception.MuleException) Test(org.junit.Test)

Aggregations

AsyncDelegateMessageProcessor (org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor)6 Test (org.junit.Test)4 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 DefaultMessageProcessorChainBuilder (org.mule.runtime.core.privileged.processor.chain.DefaultMessageProcessorChainBuilder)3 SensingNullMessageProcessor (org.mule.tck.SensingNullMessageProcessor)3 MuleException (org.mule.runtime.api.exception.MuleException)2 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)2 ResponseTimeoutException (org.mule.runtime.core.privileged.routing.ResponseTimeoutException)2 Description (io.qameta.allure.Description)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Ignore (org.junit.Ignore)1 NameableObject (org.mule.runtime.api.meta.NameableObject)1 Latch (org.mule.runtime.api.util.concurrent.Latch)1 Processor (org.mule.runtime.core.api.processor.Processor)1 MessageProcessorBuilder (org.mule.runtime.core.privileged.processor.MessageProcessorBuilder)1 JUnitLambdaProbe (org.mule.tck.probe.JUnitLambdaProbe)1 PollingProber (org.mule.tck.probe.PollingProber)1