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;
}
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();
}
}
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));
}
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;
}
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);
}
}
Aggregations