Search in sources :

Example 1 with FailureAction

use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.

the class CcMessageRouterTest method testFailedTransmitDoesNotLeadToThreadStarvation.

@Test(timeout = 3000)
public void testFailedTransmitDoesNotLeadToThreadStarvation() throws Exception {
    final int MESSAGE_LOAD = 10;
    ImmutableMessage failingMessage = mock(ImmutableMessage.class);
    when(failingMessage.isTtlAbsolute()).thenReturn(true);
    when(failingMessage.getTtlMs()).thenReturn(ExpiryDate.fromRelativeTtl(1000L).getValue());
    when(failingMessage.getRecipient()).thenReturn("to");
    when(routingTable.get("to")).thenReturn(channelAddress);
    Set<Address> addressSet = new HashSet<>();
    addressSet.add(channelAddress);
    Mockito.doReturn(addressSet).when(addressManager).getAddresses(failingMessage);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertEquals(invocation.getArguments().length, 3);
            FailureAction failureAction = (FailureAction) invocation.getArguments()[2];
            failureAction.execute(new Exception("Some error"));
            return null;
        }
    }).when(messagingStubMock).transmit(eq(failingMessage), any(SuccessAction.class), any(FailureAction.class));
    for (int i = 0; i < MESSAGE_LOAD; i++) {
        messageRouter.route(failingMessage);
    }
    Thread.sleep(2000);
    verify(messagingStubMock, atLeast(MESSAGE_LOAD * 3)).transmit(eq(failingMessage), any(SuccessAction.class), any(FailureAction.class));
    ImmutableMessage anotherMessage = mock(ImmutableMessage.class);
    when(anotherMessage.isTtlAbsolute()).thenReturn(true);
    when(anotherMessage.getTtlMs()).thenReturn(ExpiryDate.fromRelativeTtl(1000L).getValue());
    when(anotherMessage.getRecipient()).thenReturn("to");
    Mockito.doReturn(addressSet).when(addressManager).getAddresses(anotherMessage);
    final Semaphore semaphore = new Semaphore(0);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertEquals(invocation.getArguments().length, 3);
            SuccessAction successAction = (SuccessAction) invocation.getArguments()[1];
            successAction.execute();
            semaphore.release();
            return null;
        }
    }).when(messagingStubMock).transmit(eq(anotherMessage), any(SuccessAction.class), any(FailureAction.class));
    messageRouter.route(anotherMessage);
    assertTrue(semaphore.tryAcquire(100, TimeUnit.MILLISECONDS));
}
Also used : FailureAction(io.joynr.messaging.FailureAction) Address(joynr.system.RoutingTypes.Address) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) Semaphore(java.util.concurrent.Semaphore) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) SuccessAction(io.joynr.messaging.SuccessAction) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 2 with FailureAction

use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.

the class AbstractMessageRouter method createFailureAction.

private FailureAction createFailureAction(final ImmutableMessage message, final int retriesCount) {
    final FailureAction failureAction = new FailureAction() {

        final String messageId = message.getId();

        private boolean failureActionExecutedOnce = false;

        @Override
        public void execute(Throwable error) {
            synchronized (this) {
                if (failureActionExecutedOnce) {
                    logger.trace("Failure action for message with id {} already executed once. Ignoring further call.", messageId);
                    return;
                }
                failureActionExecutedOnce = true;
            }
            if (error instanceof JoynrShutdownException) {
                logger.warn("{}", error.getMessage());
                return;
            } else if (error instanceof JoynrMessageNotSentException) {
                logger.error(" ERROR SENDING:  aborting send of messageId: {}. Error: {}", new Object[] { messageId, error.getMessage() });
                callMessageProcessedListeners(messageId);
                return;
            }
            logger.warn("PROBLEM SENDING, will retry. messageId: {}. Error: {} Message: {}", new Object[] { messageId, error.getClass().getName(), error.getMessage() });
            long delayMs;
            if (error instanceof JoynrDelayMessageException) {
                delayMs = ((JoynrDelayMessageException) error).getDelayMs();
            } else {
                delayMs = createDelayWithExponentialBackoff(sendMsgRetryIntervalMs, retriesCount);
            }
            logger.error("Rescheduling messageId: {} with delay {} ms, TTL is: {}", messageId, delayMs, dateFormatter.format(message.getTtlMs()));
            try {
                routeInternal(message, delayMs, retriesCount + 1);
            } catch (Exception e) {
                logger.warn("Rescheduling of message failed (messageId {})", messageId);
                callMessageProcessedListeners(messageId);
            }
            return;
        }
    };
    return failureAction;
}
Also used : FailureAction(io.joynr.messaging.FailureAction) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException)

Example 3 with FailureAction

use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.

the class WebSocketTest method sendMessage.

private void sendMessage() throws Throwable {
    OneWayRequest request = new OneWayRequest("method", new Object[0], new Class<?>[0]);
    MessagingQos messagingQos = new MessagingQos(100000);
    ImmutableMessage msg = messageFactory.createOneWayRequest("fromID", "toID", request, messagingQos).getImmutableMessage();
    SuccessAction successAction = mock(SuccessAction.class);
    webSocketMessagingStub.transmit(msg, successAction, new FailureAction() {

        @Override
        public void execute(Throwable error) {
            Assert.fail(error.getMessage());
        }
    });
    Mockito.verify(messageRouterMock, Mockito.timeout(1000)).route(argThat(new SerializedDataOfImmutableMessageMatcher(msg)));
    Mockito.verify(successAction).execute();
}
Also used : OneWayRequest(joynr.OneWayRequest) MessagingQos(io.joynr.messaging.MessagingQos) SuccessAction(io.joynr.messaging.SuccessAction) FailureAction(io.joynr.messaging.FailureAction) ImmutableMessage(joynr.ImmutableMessage)

Example 4 with FailureAction

use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.

the class MqttPahoClient method messageArrived.

@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
    logger.debug("MQTT message received: id {}, topic {}, payload\n{}", mqttMessage.getId(), topic, new String(mqttMessage.getPayload(), Charsets.UTF_8));
    if (messagingSkeleton == null) {
        logger.error("MQTT message not processed: messagingSkeleton has not been set yet");
        return;
    }
    messagingSkeleton.transmit(mqttMessage.getPayload(), new FailureAction() {

        @Override
        public void execute(Throwable error) {
            logger.error("MQTT message not processed");
        }
    });
}
Also used : FailureAction(io.joynr.messaging.FailureAction)

Example 5 with FailureAction

use of io.joynr.messaging.FailureAction in project joynr by bmwcarit.

the class WebSocketJettyClient method onWebSocketBinary.

@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
    logger.trace(this.getClass().getSimpleName() + ": Received message: " + new String(payload, CHARSET));
    messageListener.transmit(Arrays.copyOfRange(payload, offset, offset + len), new FailureAction() {

        @Override
        public void execute(Throwable error) {
            logger.error("WebSocket message not processed: {}", error.getMessage());
        }
    });
}
Also used : FailureAction(io.joynr.messaging.FailureAction)

Aggregations

FailureAction (io.joynr.messaging.FailureAction)6 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)3 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)3 SuccessAction (io.joynr.messaging.SuccessAction)3 ImmutableMessage (joynr.ImmutableMessage)3 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)2 ChannelAddress (joynr.system.RoutingTypes.ChannelAddress)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)1 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)1 MessagingQos (io.joynr.messaging.MessagingQos)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Semaphore (java.util.concurrent.Semaphore)1 OneWayRequest (joynr.OneWayRequest)1 Address (joynr.system.RoutingTypes.Address)1