Search in sources :

Example 1 with JoynrShutdownException

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

the class LongPollingChannelLifecycle method startLongPolling.

public synchronized void startLongPolling(final MessageArrivedListener messageArrivedListener, final ReceiverStatusListener... receiverStatusListeners) {
    if (channelMonitorExecutorService == null) {
        throw new JoynrShutdownException("Channel Monitor already shutdown");
    }
    if (started == true) {
        throw new IllegalStateException("only one long polling thread can be started per ChannelMonitor");
    }
    started = true;
    Callable<Void> channelLifecycleCallable = new Callable<Void>() {

        @Override
        public Void call() {
            try {
                checkServerTime();
                final int maxRetries = settings.getMaxRetriesCount();
                while (true) {
                    channelCreated = false;
                    // Create the channel with maximal "maxRetries" retries
                    createChannelLoop(maxRetries);
                    // signal that the channel has been created
                    for (ReceiverStatusListener statusListener : receiverStatusListeners) {
                        statusListener.receiverStarted();
                    }
                    // If it was not possible to create the channel exit
                    if (!isChannelCreated()) {
                        String message = "registerMessageReceiver channelId: " + channelId + " error occured. Exiting.";
                        logger.error(message);
                        // signal that the channel is in exception
                        for (ReceiverStatusListener statusListener : receiverStatusListeners) {
                            statusListener.receiverException(new JoynrShutdownException(message));
                        }
                    }
                    // Start LONG POLL lifecycle. The future will only
                    // return when the long poll loop has ended,
                    // otherwise will terminate with a JoynrShutdownException
                    longPollLoop(messageArrivedListener, maxRetries);
                }
            } finally {
                started = false;
            }
        }
    };
    longPollingFuture = channelMonitorExecutorService.submit(channelLifecycleCallable);
}
Also used : JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) ReceiverStatusListener(io.joynr.messaging.ReceiverStatusListener) Callable(java.util.concurrent.Callable)

Example 2 with JoynrShutdownException

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

the class LongPollingMessageReceiver method start.

@Override
public synchronized Future<Void> start(MessageArrivedListener messageListener, ReceiverStatusListener... receiverStatusListeners) {
    synchronized (shutdownSynchronizer) {
        if (shutdown) {
            throw new JoynrShutdownException("Cannot register Message Listener: " + messageListener + ": LongPollingMessageReceiver is already shutting down");
        }
    }
    if (isStarted()) {
        return Futures.immediateFailedFuture(new IllegalStateException("receiver is already started"));
    }
    final SettableFuture<Void> channelCreatedFuture = SettableFuture.create();
    ReceiverStatusListener[] statusListeners = ObjectArrays.concat(new ReceiverStatusListener() {

        @Override
        public // Register the ChannelUrl once the receiver is started
        void receiverStarted() {
            if (channelMonitor.isChannelCreated()) {
                for (ChannelCreatedListener listener : channelCreatedListeners) {
                    listener.channelCreated(channelMonitor.getChannelUrl());
                }
                // Signal that the channel is now created for anyone blocking on the future
                channelCreatedFuture.set(null);
            }
        }

        @Override
        public // Shutdown the receiver if an exception is thrown
        void receiverException(Throwable e) {
            channelCreatedFuture.setException(e);
            channelMonitor.shutdown();
        }
    }, receiverStatusListeners);
    channelMonitor.startLongPolling(messageListener, statusListeners);
    return channelCreatedFuture;
}
Also used : JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) ReceiverStatusListener(io.joynr.messaging.ReceiverStatusListener)

Example 3 with JoynrShutdownException

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

the class WebSocketJettyClient method start.

@Override
public synchronized void start() {
    if (jettyClient == null) {
        jettyClient = new WebSocketClient();
        jettyClient.getPolicy().setMaxTextMessageSize(maxMessageSize);
        jettyClient.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
        jettyClient.setMaxIdleTimeout(websocketIdleTimeout);
    }
    try {
        jettyClient.start();
        sessionFuture = jettyClient.connect(this, toUrl(serverAddress));
        sendInitializationMessage();
    } catch (JoynrShutdownException | JoynrIllegalStateException e) {
        logger.error("unrecoverable error starting WebSocket client: {}", e);
        return;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        // TODO which exceptions are recoverable? Only catch those ones
        // JoynrCommunicationExeption is thrown if the initialization message could not be sent
        logger.debug("error starting WebSocket client. Will retry", e);
        if (shutdown) {
            return;
        }
        if (reconnectTimerRunning.compareAndSet(false, true)) {
            reconnectTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    reconnectTimerRunning.set(false);
                    start();
                }
            }, reconnectDelay);
        }
    }
}
Also used : TimerTask(java.util.TimerTask) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) 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)

Example 4 with JoynrShutdownException

use of io.joynr.exceptions.JoynrShutdownException 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 JoynrShutdownException

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

the class RequestReplyManagerImpl method sendSyncRequest.

@Override
public Object sendSyncRequest(String fromParticipantId, DiscoveryEntryWithMetaInfo toDiscoveryEntry, Request request, SynchronizedReplyCaller synchronizedReplyCaller, MessagingQos messagingQos) {
    if (!running) {
        throw new IllegalStateException("Request: " + request.getRequestReplyId() + " failed. SenderImpl ID: " + System.identityHashCode(this) + ": joynr is shutting down");
    }
    final ArrayList<Object> responsePayloadContainer = new ArrayList<Object>(1);
    // the synchronizedReplyCaller will call notify on the responsePayloadContainer when a message arrives
    synchronizedReplyCaller.setResponseContainer(responsePayloadContainer);
    sendRequest(fromParticipantId, toDiscoveryEntry, request, messagingQos);
    long entryTime = System.currentTimeMillis();
    // saving all calling threads so that they can be interrupted at shutdown
    outstandingRequestThreads.add(Thread.currentThread());
    synchronized (responsePayloadContainer) {
        while (running && responsePayloadContainer.isEmpty() && entryTime + messagingQos.getRoundTripTtl_ms() > System.currentTimeMillis()) {
            try {
                responsePayloadContainer.wait(messagingQos.getRoundTripTtl_ms());
            } catch (InterruptedException e) {
                if (running) {
                    throw new JoynrRequestInterruptedException("Request: " + request.getRequestReplyId() + " interrupted.");
                }
                throw new JoynrShutdownException("Request: " + request.getRequestReplyId() + " interrupted by shutdown");
            }
        }
    }
    outstandingRequestThreads.remove(Thread.currentThread());
    if (responsePayloadContainer.isEmpty()) {
        throw new JoynrCommunicationException("Request: " + request.getRequestReplyId() + " failed. The response didn't arrive in time");
    }
    Object response = responsePayloadContainer.get(0);
    if (response instanceof Throwable) {
        Throwable error = (Throwable) response;
        throw new JoynrMessageNotSentException("Request: " + request.getRequestReplyId() + " failed: " + error.getMessage(), error);
    }
    return response;
}
Also used : JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) ArrayList(java.util.ArrayList) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrRequestInterruptedException(io.joynr.exceptions.JoynrRequestInterruptedException) JoynrRequestInterruptedException(io.joynr.exceptions.JoynrRequestInterruptedException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException)

Aggregations

JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)10 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)4 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)3 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)3 IOException (java.io.IOException)3 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)2 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)2 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)2 ReceiverStatusListener (io.joynr.messaging.ReceiverStatusListener)2 JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)2 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)2 HttpEntity (org.apache.http.HttpEntity)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JoynrRequestInterruptedException (io.joynr.exceptions.JoynrRequestInterruptedException)1 FailureAction (io.joynr.messaging.FailureAction)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 EncodingException (io.joynr.smrf.EncodingException)1 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)1 ArrayList (java.util.ArrayList)1 TimerTask (java.util.TimerTask)1