Search in sources :

Example 1 with ResponseTimeoutException

use of org.mule.runtime.core.privileged.routing.ResponseTimeoutException 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)

Example 2 with ResponseTimeoutException

use of org.mule.runtime.core.privileged.routing.ResponseTimeoutException in project mule by mulesoft.

the class AbstractAsyncRequestReplyRequester method receiveAsyncReply.

private PrivilegedEvent receiveAsyncReply(CoreEvent event) throws MuleException {
    String asyncReplyCorrelationId = getAsyncReplyCorrelationId(event);
    System.out.println("receiveAsyncReply: " + asyncReplyCorrelationId);
    Latch asyncReplyLatch = getLatch(asyncReplyCorrelationId);
    // flag for catching the interrupted status of the Thread waiting for a
    // result
    boolean interruptedWhileWaiting = false;
    boolean resultAvailable = false;
    PrivilegedEvent result;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Waiting for async reply message with id: " + asyncReplyCorrelationId);
        }
        // how long should we wait for the lock?
        if (timeout <= 0) {
            asyncReplyLatch.await();
            resultAvailable = true;
        } else {
            resultAvailable = asyncReplyLatch.await(timeout, MILLISECONDS);
        }
        if (!resultAvailable) {
            asyncReplyLatch.await(1000, MILLISECONDS);
            resultAvailable = asyncReplyLatch.getCount() == 0;
        }
    } catch (InterruptedException e) {
        interruptedWhileWaiting = true;
    } finally {
        locks.remove(asyncReplyCorrelationId);
        result = responseEvents.remove(asyncReplyCorrelationId);
        if (interruptedWhileWaiting) {
            Thread.currentThread().interrupt();
            return null;
        }
    }
    if (resultAvailable) {
        if (result == null) {
            // this should never happen, just using it as a safe guard for now
            throw new IllegalStateException("Response MuleEvent is null");
        }
        // Copy event because the async-reply message was received by a different
        // receiver thread (or the senders dispatcher thread in case of vm
        // with queueEvents="false") and the current thread may need to mutate
        // the even. See MULE-4370
        setCurrentEvent(result);
        return result;
    } else {
        addProcessed(new ProcessedEvents(asyncReplyCorrelationId, EndReason.FINISHED_BY_TIMEOUT));
        if (failOnTimeout) {
            notificationFirer.dispatch(new RoutingNotification(event.getMessage(), null, ASYNC_REPLY_TIMEOUT));
            throw new ResponseTimeoutException(responseTimedOutWaitingForId((int) timeout, asyncReplyCorrelationId), null);
        } else {
            return null;
        }
    }
}
Also used : ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Latch(org.mule.runtime.api.util.concurrent.Latch) RoutingNotification(org.mule.runtime.api.notification.RoutingNotification)

Aggregations

ResponseTimeoutException (org.mule.runtime.core.privileged.routing.ResponseTimeoutException)2 Test (org.junit.Test)1 MuleException (org.mule.runtime.api.exception.MuleException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 RoutingNotification (org.mule.runtime.api.notification.RoutingNotification)1 Latch (org.mule.runtime.api.util.concurrent.Latch)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 AsyncDelegateMessageProcessor (org.mule.runtime.core.internal.processor.AsyncDelegateMessageProcessor)1 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)1 SensingNullMessageProcessor (org.mule.tck.SensingNullMessageProcessor)1