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