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