Search in sources :

Example 1 with ReceiverStatusListener

use of io.joynr.messaging.ReceiverStatusListener 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 ReceiverStatusListener

use of io.joynr.messaging.ReceiverStatusListener 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 ReceiverStatusListener

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

the class DispatcherImplTest method setUp.

@Before
public void setUp() throws NoSuchMethodException, SecurityException {
    Injector injector = Guice.createInjector(new JsonMessageSerializerModule(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(Dispatcher.class).to(DispatcherImpl.class);
            bind(RequestReplyManager.class).toInstance(requestReplyManagerMock);
            bind(SubscriptionManager.class).toInstance(subscriptionManagerMock);
            bind(PublicationManager.class).toInstance(publicationManagerMock);
            bind(MessageSender.class).toInstance(messageSenderMock);
            bind(MessageRouter.class).toInstance(messageRouterMock);
            bind(MessageReceiver.class).toInstance(messageReceiverMock);
            Multibinder.newSetBinder(binder(), new TypeLiteral<JoynrMessageProcessor>() {
            });
            requestStaticInjection(RpcUtils.class, Request.class, JoynrMessagingConnectorFactory.class);
            ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("joynr.Cleanup-%d").build();
            ScheduledExecutorService cleanupExecutor = Executors.newSingleThreadScheduledExecutor(namedThreadFactory);
            bind(ScheduledExecutorService.class).annotatedWith(Names.named(JOYNR_SCHEDULER_CLEANUP)).toInstance(cleanupExecutor);
        }
    });
    fixture = injector.getInstance(Dispatcher.class);
    messageReceiverMock.start(fixture, new ReceiverStatusListener() {

        @Override
        public void receiverStarted() {
        }

        @Override
        public void receiverException(Throwable e) {
        }
    });
    requestCallerDirectory = injector.getInstance(ProviderDirectory.class);
    messageFactory = injector.getInstance(MutableMessageFactory.class);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RpcUtils(io.joynr.dispatching.rpc.RpcUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) JsonMessageSerializerModule(io.joynr.messaging.JsonMessageSerializerModule) OneWayRequest(joynr.OneWayRequest) SubscriptionRequest(joynr.SubscriptionRequest) MulticastSubscriptionRequest(joynr.MulticastSubscriptionRequest) Request(joynr.Request) ReceiverStatusListener(io.joynr.messaging.ReceiverStatusListener) AbstractModule(com.google.inject.AbstractModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) JoynrMessagingConnectorFactory(io.joynr.proxy.JoynrMessagingConnectorFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Before(org.junit.Before)

Example 4 with ReceiverStatusListener

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

the class MessageReceiverMock method start.

@Override
public Future<Void> start(MessageArrivedListener messageArrivedListener, ReceiverStatusListener... statusListeners) {
    this.messageArrivedListener = messageArrivedListener;
    started = true;
    if (isBlockInitialisation()) {
        // Added to check if Dispatcher blocks on addReplyCaller
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {
        }
    }
    for (ReceiverStatusListener statusListener : statusListeners) {
        statusListener.receiverStarted();
    }
    return Futures.immediateFuture(null);
}
Also used : ReceiverStatusListener(io.joynr.messaging.ReceiverStatusListener)

Example 5 with ReceiverStatusListener

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

the class HttpCommunicationManagerTest method testCreateOpenAndDeleteChannel.

@Test
public void testCreateOpenAndDeleteChannel() throws Exception {
    MutableMessage mutableMessage = new MutableMessage();
    mutableMessage.setType(Message.VALUE_MESSAGE_TYPE_REQUEST);
    mutableMessage.setSender("testSender");
    mutableMessage.setRecipient("testRecipient");
    mutableMessage.setPayload(new byte[] { 0, 1, 2 });
    mutableMessage.setTtlAbsolute(true);
    mutableMessage.setTtlMs(ExpiryDate.fromRelativeTtl(30000).getValue());
    byte[] serializedMessage = mutableMessage.getImmutableMessage().getSerializedMessage();
    final Object waitForChannelCreated = new Object();
    longpollingMessageReceiver.start(dispatcher, new ReceiverStatusListener() {

        @Override
        public void receiverStarted() {
            synchronized (waitForChannelCreated) {
                waitForChannelCreated.notify();
            }
        }

        @Override
        public void receiverException(Throwable e) {
        }
    });
    synchronized (waitForChannelCreated) {
        waitForChannelCreated.wait(5000);
    }
    // post to the channel to see if it exists
    onrequest(1000).with().body(serializedMessage).expect().statusCode(201).when().post("channels/" + testChannelId + "/message/");
    longpollingMessageReceiver.shutdown(true);
    // post again; this time it should be missing (NO_CONTENT)
    onrequest(1000).with().body(serializedMessage).expect().statusCode(400).body(containsString("Channel not found")).when().post("channels/" + testChannelId + "/message/");
}
Also used : MutableMessage(joynr.MutableMessage) ReceiverStatusListener(io.joynr.messaging.ReceiverStatusListener) Test(org.junit.Test)

Aggregations

ReceiverStatusListener (io.joynr.messaging.ReceiverStatusListener)5 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 TypeLiteral (com.google.inject.TypeLiteral)1 RpcUtils (io.joynr.dispatching.rpc.RpcUtils)1 JsonMessageSerializerModule (io.joynr.messaging.JsonMessageSerializerModule)1 JoynrMessagingConnectorFactory (io.joynr.proxy.JoynrMessagingConnectorFactory)1 Callable (java.util.concurrent.Callable)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 MulticastSubscriptionRequest (joynr.MulticastSubscriptionRequest)1 MutableMessage (joynr.MutableMessage)1 OneWayRequest (joynr.OneWayRequest)1 Request (joynr.Request)1 SubscriptionRequest (joynr.SubscriptionRequest)1 Before (org.junit.Before)1 Test (org.junit.Test)1