Search in sources :

Example 1 with JoynrDelayMessageException

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

the class CcMessageRouterTest method retryRoutingWith1msDelay.

private ImmutableMessage retryRoutingWith1msDelay(MessageRouter messageRouter, int ttlMs) throws Exception {
    doThrow(new JoynrDelayMessageException(1, "test")).when(messagingStubMock).transmit(any(ImmutableMessage.class), any(SuccessAction.class), any(FailureAction.class));
    joynrMessage.setTtlMs(ExpiryDate.fromRelativeTtl(ttlMs).getValue());
    joynrMessage.setTtlAbsolute(true);
    ImmutableMessage immutableMessage = joynrMessage.getImmutableMessage();
    messageRouter.route(immutableMessage);
    Thread.sleep(100);
    return immutableMessage;
}
Also used : SuccessAction(io.joynr.messaging.SuccessAction) FailureAction(io.joynr.messaging.FailureAction) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) ImmutableMessage(joynr.ImmutableMessage)

Example 2 with JoynrDelayMessageException

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

the class WebSocketJettyClient method writeBytes.

@Override
public synchronized void writeBytes(Address to, byte[] message, long timeout, TimeUnit unit, final SuccessAction successAction, final FailureAction failureAction) {
    if (messageListener == null) {
        throw new JoynrDelayMessageException(20, "WebSocket write failed: receiver has not been set yet");
    }
    if (sessionFuture == null) {
        try {
            reconnect();
        } catch (Exception e) {
            throw new JoynrDelayMessageException(10, "WebSocket reconnect failed. Will try later", e);
        }
    }
    try {
        Session session = sessionFuture.get(timeout, unit);
        session.getRemote().sendBytes(ByteBuffer.wrap(message), new WriteCallback() {

            @Override
            public void writeSuccess() {
                successAction.execute();
            }

            @Override
            public void writeFailed(Throwable error) {
                if (error instanceof WebSocketException) {
                    reconnect();
                    failureAction.execute(new JoynrDelayMessageException(reconnectDelay, error.getMessage()));
                } else {
                    failureAction.execute(error);
                }
            }
        });
    } catch (WebSocketException | ExecutionException e) {
        reconnect();
        throw new JoynrDelayMessageException(10, "WebSocket write timed out", e);
    } catch (TimeoutException e) {
        throw new JoynrDelayMessageException("WebSocket write timed out", e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) ExecutionException(java.util.concurrent.ExecutionException) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) TimeoutException(java.util.concurrent.TimeoutException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) Session(org.eclipse.jetty.websocket.api.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with JoynrDelayMessageException

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

the class MqttPahoClient method publishMessage.

@Override
public void publishMessage(String topic, byte[] serializedMessage, int qosLevel) {
    if (messagingSkeleton == null) {
        throw new JoynrDelayMessageException("MQTT Publish failed: messagingSkeleton has not been set yet");
    }
    if (maxMsgSizeBytes != 0 && serializedMessage.length > maxMsgSizeBytes) {
        throw new JoynrMessageNotSentException("MQTT Publish failed: maximum allowed message size of " + maxMsgSizeBytes + " bytes exceeded, actual size is " + serializedMessage.length + " bytes");
    }
    try {
        MqttMessage message = new MqttMessage();
        message.setPayload(serializedMessage);
        message.setQos(qosLevel);
        message.setRetained(false);
        logger.debug("MQTT Publish to: {}", topic);
        mqttClient.publish(topic, message);
    } catch (MqttException e) {
        logger.debug("MQTT Publish failed: {}. Error code {}", e.getMessage(), e.getReasonCode(), e);
        switch(e.getReasonCode()) {
            case MqttException.REASON_CODE_CLIENT_EXCEPTION:
                Throwable cause = e.getCause();
                if (cause != null) {
                    throw new JoynrDelayMessageException("MqttException: " + cause.getMessage());
                } else {
                    throw new JoynrDelayMessageException("MqttException: " + e.getMessage());
                }
            case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
            case MqttException.REASON_CODE_CLIENT_DISCONNECTING:
            case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED:
            case MqttException.REASON_CODE_CLIENT_TIMEOUT:
            case MqttException.REASON_CODE_CONNECTION_LOST:
            case MqttException.REASON_CODE_MAX_INFLIGHT:
            case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE:
            case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
            case MqttException.REASON_CODE_UNEXPECTED_ERROR:
            case MqttException.REASON_CODE_WRITE_TIMEOUT:
            case MqttException.REASON_CODE_CONNECT_IN_PROGRESS:
                throw new JoynrDelayMessageException("MqttException: " + e.getMessage());
            default:
                throw new JoynrMessageNotSentException(e.getMessage());
        }
    } catch (Exception e) {
        throw new JoynrMessageNotSentException(e.getMessage(), e);
    }
    logger.debug("Published message: " + new String(serializedMessage, Charsets.UTF_8));
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) URISyntaxException(java.net.URISyntaxException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Example 4 with JoynrDelayMessageException

use of io.joynr.exceptions.JoynrDelayMessageException 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 5 with JoynrDelayMessageException

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

the class WebSocketJettyServer method writeBytes.

@Override
public synchronized void writeBytes(Address toAddress, byte[] message, long timeout, TimeUnit unit, final SuccessAction successAction, final FailureAction failureAction) {
    if (!(toAddress instanceof WebSocketClientAddress)) {
        throw new JoynrIllegalStateException("Web Socket Server can only send to WebSocketClientAddresses");
    }
    WebSocketClientAddress toClientAddress = (WebSocketClientAddress) toAddress;
    Session session = sessionMap.get(toClientAddress.getId());
    if (session == null) {
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException("no active session for WebSocketClientAddress: " + toClientAddress.getId());
    }
    try {
        session.getRemote().sendBytes(ByteBuffer.wrap(message), new WriteCallback() {

            @Override
            public void writeSuccess() {
                successAction.execute();
            }

            @Override
            public void writeFailed(Throwable error) {
                if (shutdown) {
                    return;
                }
                failureAction.execute(error);
            }
        });
    } catch (WebSocketException e) {
        // Jetty throws WebSocketException when expecting [OPEN or CONNECTED] but found a different state
        // The client must reconnect, but the message can be queued in the mean time.
        sessionMap.remove(toClientAddress.getId());
        // TODO We need a delay with invalidation of the stub
        throw new JoynrDelayMessageException(e.getMessage(), e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) WebSocketClientAddress(joynr.system.RoutingTypes.WebSocketClientAddress) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) Session(org.eclipse.jetty.websocket.api.Session)

Aggregations

JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)8 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)4 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)4 FailureAction (io.joynr.messaging.FailureAction)4 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)3 SuccessAction (io.joynr.messaging.SuccessAction)3 ImmutableMessage (joynr.ImmutableMessage)3 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)2 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)2 IOException (java.io.IOException)2 Session (org.eclipse.jetty.websocket.api.Session)2 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)2 WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)1 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)1 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 CcMessageRouter (io.joynr.messaging.routing.CcMessageRouter)1