Search in sources :

Example 1 with ClientNotificationDispatcher

use of org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher in project scout.rt by eclipse.

the class ClientHttpServiceTunnel method dispatchClientNotifications.

/**
 * dispatch notifications in a client job and ensure to wait for dispatched notifications
 *
 * @param notifications
 *          the notifications to dispatch
 */
protected void dispatchClientNotifications(final List<ClientNotificationMessage> notifications) {
    if (CollectionUtility.isEmpty(notifications)) {
        return;
    }
    final IBlockingCondition cond = Jobs.newBlockingCondition(true);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            ClientNotificationDispatcher notificationDispatcher = BEANS.get(ClientNotificationDispatcher.class);
            notificationDispatcher.dispatchNotifications(notifications);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.copyCurrent())).whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            cond.setBlocking(false);
        }
    }, null);
    cond.waitFor();
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)

Example 2 with ClientNotificationDispatcher

use of org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher in project scout.rt by eclipse.

the class BridgeToServerBeanDecorator method ensureRunInServerContext.

protected Object ensureRunInServerContext(final IBeanInvocationContext<T> context) {
    if (PropertyMap.isSet(PropertyMap.PROP_SERVER_SCOPE)) {
        // already in a server scope
        return continueCall(context);
    }
    // bridge to server scope
    ClientNotificationCollector collector = new ClientNotificationCollector();
    ServerRunContext bridgeRunContext = ServerRunContexts.copyCurrent().withClientNotificationCollector(collector).withClientNodeId(INode.ID);
    ISession currentSession = ISession.CURRENT.get();
    IServerSession bridgeSession = null;
    if (currentSession != null) {
        bridgeSession = BEANS.get(ServerSessionProviderWithCache.class).provide(currentSession.getId(), bridgeRunContext);
    }
    Object result = bridgeRunContext.withSession(bridgeSession).call(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                return continueCall(context);
            } catch (Exception e) {
                ITransaction.CURRENT.get().addFailure(e);
                throw e;
            }
        }
    });
    ClientNotificationDispatcher clientNotificationDispatcher = BEANS.get(ClientNotificationDispatcher.class);
    List<ClientNotificationMessage> values = collector.consume();
    if (!values.isEmpty()) {
        clientNotificationDispatcher.dispatchNotifications(values);
    }
    return result;
}
Also used : ISession(org.eclipse.scout.rt.shared.ISession) ServerRunContext(org.eclipse.scout.rt.server.context.ServerRunContext) IServerSession(org.eclipse.scout.rt.server.IServerSession) ClientNotificationCollector(org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector) ClientNotificationMessage(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationMessage) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)

Example 3 with ClientNotificationDispatcher

use of org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher in project scout.rt by eclipse.

the class NotificationDispatcherTest method testStringNotification.

@Test
public void testStringNotification() {
    final IBlockingCondition cond = Jobs.newBlockingCondition(true);
    final String stringNotification = "A simple string notification";
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            final ClientNotificationDispatcher dispatcher = BEANS.get(ClientNotificationDispatcher.class);
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), stringNotification, mock(ClientNotificationAddress.class));
            waitForPendingNotifications(dispatcher);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.copyCurrent())).whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            cond.setBlocking(false);
        }
    }, null);
    cond.waitFor();
    Mockito.verify(m_globalNotificationHanlder, Mockito.times(1)).handleNotification(Mockito.any(Serializable.class));
    Mockito.verify(m_groupNotificationHanlder, Mockito.times(0)).handleNotification(Mockito.any(INotificationGroup.class));
}
Also used : Serializable(java.io.Serializable) ClientNotificationAddress(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationAddress) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher) Test(org.junit.Test)

Example 4 with ClientNotificationDispatcher

use of org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher in project scout.rt by eclipse.

the class NotificationDispatcherTest method testSuperClassNotification.

@Test
public void testSuperClassNotification() {
    final IBlockingCondition cond = Jobs.newBlockingCondition(true);
    Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            ClientNotificationDispatcher dispatcher = BEANS.get(ClientNotificationDispatcher.class);
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification01(), mock(ClientNotificationAddress.class));
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification02(), mock(ClientNotificationAddress.class));
            dispatcher.dispatchForSession((IClientSession) IClientSession.CURRENT.get(), new Notification02(), mock(ClientNotificationAddress.class));
            waitForPendingNotifications(dispatcher);
        }
    }, Jobs.newInput().withRunContext(ClientRunContexts.copyCurrent())).whenDone(new IDoneHandler<Void>() {

        @Override
        public void onDone(DoneEvent<Void> event) {
            cond.setBlocking(false);
        }
    }, null);
    cond.waitFor();
    Mockito.verify(m_globalNotificationHanlder, Mockito.times(3)).handleNotification(Mockito.any(Serializable.class));
    Mockito.verify(m_groupNotificationHanlder, Mockito.times(2)).handleNotification(Mockito.any(INotificationGroup.class));
}
Also used : Serializable(java.io.Serializable) ClientNotificationAddress(org.eclipse.scout.rt.shared.clientnotification.ClientNotificationAddress) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) ClientNotificationDispatcher(org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher) Test(org.junit.Test)

Aggregations

ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)4 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)3 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)3 Serializable (java.io.Serializable)2 ClientNotificationAddress (org.eclipse.scout.rt.shared.clientnotification.ClientNotificationAddress)2 Test (org.junit.Test)2 IServerSession (org.eclipse.scout.rt.server.IServerSession)1 ClientNotificationCollector (org.eclipse.scout.rt.server.clientnotification.ClientNotificationCollector)1 ServerRunContext (org.eclipse.scout.rt.server.context.ServerRunContext)1 ISession (org.eclipse.scout.rt.shared.ISession)1 ClientNotificationMessage (org.eclipse.scout.rt.shared.clientnotification.ClientNotificationMessage)1