Search in sources :

Example 1 with DolphinContext

use of com.canoo.dp.impl.server.context.DolphinContext in project dolphin-platform by canoo.

the class AbstractEventBus method subscribe.

@Override
public <T extends Serializable> Subscription subscribe(final Topic<T> topic, final MessageListener<? super T> listener, final Predicate<MessageEventContext<T>> filter) {
    checkInitialization();
    Assert.requireNonNull(topic, "topic");
    Assert.requireNonNull(listener, "listener");
    final DolphinContext subscriptionContext = getCurrentContext();
    if (subscriptionContext == null) {
        throw new IllegalStateException("Subscription can only be done from Dolphin Context!");
    }
    final String subscriptionSessionId = subscriptionContext.getId();
    LOG.trace("Adding subscription for topic {} in Dolphin Platform context {}", topic.getName(), subscriptionSessionId);
    List<ListenerWithFilter<?>> listeners = topicToListenerMap.get(topic);
    if (listeners == null) {
        listeners = new CopyOnWriteArrayList<>();
        topicToListenerMap.put(topic, listeners);
    }
    final ListenerWithFilter listenerWithFilter = new ListenerWithFilter(listener, filter);
    listeners.add(listenerWithFilter);
    listenerToSessionMap.put(listener, subscriptionSessionId);
    final Subscription subscription = new Subscription() {

        @Override
        public void unsubscribe() {
            LOG.trace("Removing subscription for topic {} in Dolphin Platform context {}", topic.getName(), subscriptionSessionId);
            final List<ListenerWithFilter<?>> listeners = topicToListenerMap.get(topic);
            if (listeners != null) {
                listeners.remove(listenerWithFilter);
            }
            listenerToSessionMap.remove(listener);
            removeSubscriptionForSession(this, subscriptionSessionId);
        }
    };
    addSubscriptionForSession(subscription, subscriptionSessionId);
    return subscription;
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) Subscription(com.canoo.platform.core.functional.Subscription)

Example 2 with DolphinContext

use of com.canoo.dp.impl.server.context.DolphinContext in project dolphin-platform by canoo.

the class DefaultDolphinEventBusTest method create.

private DefaultDolphinEventBus create(final DolphinContext context) {
    DefaultDolphinEventBus eventBus = new DefaultDolphinEventBus();
    eventBus.init(new DolphinContextProvider() {

        @Override
        public DolphinContext getContext(ClientSession clientSession) {
            return getContextById(clientSession.getId());
        }

        @Override
        public DolphinContext getContextById(String clientSessionId) {
            if (context != null && context.getId().equals(clientSessionId)) {
                return context;
            }
            return null;
        }

        @Override
        public DolphinContext getCurrentDolphinContext() {
            return context;
        }
    }, new ClientSessionLifecycleHandlerImpl());
    return eventBus;
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) ClientSessionLifecycleHandlerImpl(com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) ClientSession(com.canoo.platform.server.client.ClientSession)

Example 3 with DolphinContext

use of com.canoo.dp.impl.server.context.DolphinContext in project dolphin-platform by canoo.

the class RemotingCdiBeanFactory method createRemotingContext.

@Produces
@ClientScoped
public RemotingContext createRemotingContext(RemotingEventBus eventBus) {
    Assert.requireNonNull(eventBus, "eventBus");
    final DolphinContextProvider contextProvider = PlatformBootstrap.getServerCoreComponents().getInstance(DolphinContextProvider.class);
    Assert.requireNonNull(contextProvider, "contextProvider");
    final DolphinContext context = contextProvider.getCurrentDolphinContext();
    Assert.requireNonNull(context, "context");
    return new RemotingContextImpl(context, eventBus);
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) RemotingContextImpl(com.canoo.dp.impl.server.context.RemotingContextImpl) Produces(javax.enterprise.inject.Produces) ClientScoped(com.canoo.platform.server.javaee.ClientScoped)

Example 4 with DolphinContext

use of com.canoo.dp.impl.server.context.DolphinContext in project dolphin-platform by canoo.

the class RemotingSpringBeanFactory method createRemotingContext.

@Bean(name = "remotingContext")
@ClientScope
protected RemotingContext createRemotingContext(RemotingEventBus eventBus) {
    Assert.requireNonNull(eventBus, "eventBus");
    final DolphinContextProvider contextProvider = PlatformBootstrap.getServerCoreComponents().getInstance(DolphinContextProvider.class);
    Assert.requireNonNull(contextProvider, "contextProvider");
    final DolphinContext context = contextProvider.getCurrentDolphinContext();
    Assert.requireNonNull(context, "context");
    return new RemotingContextImpl(context, eventBus);
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) DolphinContextProvider(com.canoo.dp.impl.server.context.DolphinContextProvider) RemotingContextImpl(com.canoo.dp.impl.server.context.RemotingContextImpl) ClientScope(com.canoo.platform.server.spring.ClientScope) Bean(org.springframework.context.annotation.Bean)

Example 5 with DolphinContext

use of com.canoo.dp.impl.server.context.DolphinContext in project dolphin-platform by canoo.

the class AbstractEventBus method publish.

@Override
public <T extends Serializable> void publish(final Topic<T> topic, final T data) {
    checkInitialization();
    final DolphinEvent event = new DolphinEvent(topic, System.currentTimeMillis(), data);
    event.addMetadata(EventConstants.TYPE_PARAM, EventConstants.TYPE_PLATFORM);
    final DolphinContext currentContext = getCurrentContext();
    if (currentContext != null) {
        final ClientSession clientSession = currentContext.getClientSession();
        if (clientSession != null) {
            event.addMetadata(EventConstants.CLIENT_SESSION_PARAM, clientSession.getId());
            final HttpSession httpSession = clientSession.getHttpSession();
            if (httpSession != null) {
                event.addMetadata(EventConstants.HTTP_SESSION_PARAM, httpSession.getId());
            }
        }
    }
    // Handle listener in same session
    if (currentContext != null) {
        final List<ListenerWithFilter<T>> listenersInCurrentSession = getListenersForSessionAndTopic(currentContext.getId(), topic);
        for (ListenerWithFilter<T> listenerAndFilter : listenersInCurrentSession) {
            final Predicate<MessageEventContext<T>> filter = listenerAndFilter.getFilter();
            final MessageListener<T> listener = listenerAndFilter.getListener();
            if (filter == null || filter.test(event.getMessageEventContext())) {
                listener.onMessage(event);
            }
        }
    }
    publishForOtherSessions(event);
}
Also used : DolphinContext(com.canoo.dp.impl.server.context.DolphinContext) MessageEventContext(com.canoo.platform.remoting.server.event.MessageEventContext) HttpSession(javax.servlet.http.HttpSession) ClientSession(com.canoo.platform.server.client.ClientSession)

Aggregations

DolphinContext (com.canoo.dp.impl.server.context.DolphinContext)6 DolphinContextProvider (com.canoo.dp.impl.server.context.DolphinContextProvider)3 RemotingContextImpl (com.canoo.dp.impl.server.context.RemotingContextImpl)2 ClientSession (com.canoo.platform.server.client.ClientSession)2 ClientSessionLifecycleHandlerImpl (com.canoo.dp.impl.server.client.ClientSessionLifecycleHandlerImpl)1 Subscription (com.canoo.platform.core.functional.Subscription)1 MessageEventContext (com.canoo.platform.remoting.server.event.MessageEventContext)1 ClientScoped (com.canoo.platform.server.javaee.ClientScoped)1 ClientScope (com.canoo.platform.server.spring.ClientScope)1 Produces (javax.enterprise.inject.Produces)1 HttpSession (javax.servlet.http.HttpSession)1 Bean (org.springframework.context.annotation.Bean)1