Search in sources :

Example 6 with JoynrMessageNotSentException

use of io.joynr.exceptions.JoynrMessageNotSentException in project joynr by bmwcarit.

the class CcMessageRouterTest method testMessageProcessedListenerCalledForAbortedMessage.

@Test
public void testMessageProcessedListenerCalledForAbortedMessage() throws Exception {
    doThrow(new JoynrMessageNotSentException("test")).when(messagingStubMock).transmit(any(ImmutableMessage.class), any(SuccessAction.class), any(FailureAction.class));
    joynrMessage.setTtlMs(ExpiryDate.fromRelativeTtl(100000000).getValue());
    joynrMessage.setTtlAbsolute(true);
    testMessageProcessedListenerCalled(messageRouter, null);
}
Also used : SuccessAction(io.joynr.messaging.SuccessAction) FailureAction(io.joynr.messaging.FailureAction) ImmutableMessage(joynr.ImmutableMessage) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) Test(org.junit.Test)

Example 7 with JoynrMessageNotSentException

use of io.joynr.exceptions.JoynrMessageNotSentException in project joynr by bmwcarit.

the class CcMessageRouterTest method testNoMessageDuplicationForMulticastForMultipleAddressesWithErrorFromStub.

@Test
public void testNoMessageDuplicationForMulticastForMultipleAddressesWithErrorFromStub() throws Exception {
    ChannelAddress receiverAddress1 = new ChannelAddress("http://testUrl", "channelId1");
    ChannelAddress receiverAddress2 = new ChannelAddress("http://testUrl", "channelId2");
    prepareMulticastForMultipleAddresses(receiverAddress1, receiverAddress2);
    ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
    doAnswer(new Answer<Void>() {

        private int callCount = 0;

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            FailureAction failureAction = invocation.getArgumentAt(2, FailureAction.class);
            if (callCount < 2) {
                callCount++;
                failureAction.execute(new JoynrDelayMessageException(10, "first retry"));
            } else {
                failureAction.execute(new JoynrMessageNotSentException("do not retry twice"));
            }
            return null;
        }
    }).when(messagingStubMock).transmit(eq(immutableMessage), any(SuccessAction.class), any(FailureAction.class));
    messageRouter.route(immutableMessage);
    Thread.sleep(1000);
    verify(messagingStubMock, times(4)).transmit(eq(immutableMessage), any(SuccessAction.class), any(FailureAction.class));
    verify(middlewareMessagingStubFactoryMock, times(2)).create(receiverAddress1);
    verify(middlewareMessagingStubFactoryMock, times(2)).create(receiverAddress2);
}
Also used : FailureAction(io.joynr.messaging.FailureAction) SuccessAction(io.joynr.messaging.SuccessAction) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImmutableMessage(joynr.ImmutableMessage) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) Test(org.junit.Test)

Example 8 with JoynrMessageNotSentException

use of io.joynr.exceptions.JoynrMessageNotSentException in project joynr by bmwcarit.

the class HttpMessageSender method sendMessage.

public void sendMessage(ChannelAddress address, byte[] serializedMessage, SuccessAction successAction, FailureAction failureAction) {
    // check if messageReceiver is ready to receive replies otherwise delay request by at least 100 ms
    if (!messageReceiver.isReady()) {
        long delay_ms = DELAY_RECEIVER_NOT_STARTED_MS;
        failureAction.execute(new JoynrDelayMessageException(delay_ms, RECEIVER_NOT_STARTED_REASON));
    }
    String sendUrl = urlResolver.getSendUrl(address.getMessagingEndpointUrl());
    logger.trace("SENDING: channelId: {} message: {}", sendUrl, serializedMessage);
    HttpContext context = new BasicHttpContext();
    // execute http command to send
    CloseableHttpResponse response = null;
    try {
        HttpPost httpPost = httpRequestFactory.createHttpPost(URI.create(sendUrl));
        httpPost.addHeader(new BasicHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON() + ";charset=UTF-8"));
        httpPost.setEntity(new ByteArrayEntity(serializedMessage));
        // Clone the default config
        Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig);
        requestConfigBuilder.setConnectionRequestTimeout(httpConstants.getSEND_MESSAGE_REQUEST_TIMEOUT());
        httpPost.setConfig(requestConfigBuilder.build());
        response = httpclient.execute(httpPost, context);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        String statusText = statusLine.getReasonPhrase();
        switch(statusCode) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
                logger.trace("SENT: channelId {} message: {}", sendUrl, serializedMessage);
                successAction.execute();
                break;
            case HttpURLConnection.HTTP_BAD_REQUEST:
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender. No further reason found in message body"));
                    return;
                }
                String body = EntityUtils.toString(entity, "UTF-8");
                JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
                JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
                logger.error(error.toString());
                switch(joynrMessagingErrorCode) {
                    case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
                        failureAction.execute(new JoynrChannelMissingException("Channel does not exist. Status: " + statusCode + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                    default:
                        failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender: " + statusText + body + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                }
                break;
            default:
                failureAction.execute(new JoynrCommunicationException("Unknown Error in HttpMessageSender: " + statusText + " statusCode: " + statusCode));
                break;
        }
    } catch (JoynrShutdownException e) {
        failureAction.execute(new JoynrMessageNotSentException("Message not sent to: " + address, e));
    } catch (Exception e) {
        // An exception occured - this could still be a communication error (e.g Connection refused)
        failureAction.execute(new JoynrCommunicationException(e.getClass().getName() + "Exception while communicating. error: " + e.getMessage()));
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : HttpPost(io.joynr.messaging.http.operation.HttpPost) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Builder(org.apache.http.client.config.RequestConfig.Builder) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) IOException(java.io.IOException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) StatusLine(org.apache.http.StatusLine) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException)

Aggregations

JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)8 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)4 Test (org.junit.Test)4 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)3 FailureAction (io.joynr.messaging.FailureAction)3 ImmutableMessage (joynr.ImmutableMessage)3 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)2 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)2 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)2 SuccessAction (io.joynr.messaging.SuccessAction)2 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)1 JoynrRequestInterruptedException (io.joynr.exceptions.JoynrRequestInterruptedException)1 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)1 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 Reply (joynr.Reply)1