Search in sources :

Example 1 with TriggeredEventSubscription

use of org.openremote.model.event.TriggeredEventSubscription in project openremote by openremote.

the class EventSubscriptions method splitForSubscribers.

@SuppressWarnings({ "unchecked", "unused" })
public <T extends SharedEvent> List<Message> splitForSubscribers(Exchange exchange) {
    List<Message> messageList = new ArrayList<>();
    T event = (T) exchange.getIn().getBody(SharedEvent.class);
    if (event == null)
        return messageList;
    Set<Map.Entry<String, SessionSubscriptions>> sessionSubscriptionsSet;
    synchronized (this.sessionSubscriptionIdMap) {
        sessionSubscriptionsSet = new HashSet<>(sessionSubscriptionIdMap.entrySet());
    }
    for (Map.Entry<String, SessionSubscriptions> entry : sessionSubscriptionsSet) {
        String sessionKey = entry.getKey();
        SessionSubscriptions subscriptions = entry.getValue();
        for (SessionSubscription<?> sessionSubscription : subscriptions) {
            if (!sessionSubscription.matches(event))
                continue;
            SessionSubscription<T> sessionSub = (SessionSubscription<T>) sessionSubscription;
            if (sessionSub.subscription.getFilter() == null || sessionSub.subscription.getFilter().apply(event)) {
                LOG.finer("Creating message for subscribed session '" + sessionKey + "': " + event);
                List<T> events = Collections.singletonList(event);
                TriggeredEventSubscription<T> triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSub.subscriptionId);
                if (sessionSub.subscription.getInternalConsumer() == null) {
                    Message msg = new DefaultMessage();
                    // Don't copy the event, use same reference
                    msg.setBody(triggeredEventSubscription);
                    // Copy headers
                    msg.setHeaders(new HashMap<>(exchange.getIn().getHeaders()));
                    msg.setHeader(ConnectionConstants.SESSION_KEY, sessionKey);
                    messageList.add(msg);
                } else {
                    if (triggeredEventSubscription.getEvents() != null) {
                        triggeredEventSubscription.getEvents().forEach(e -> sessionSub.subscription.getInternalConsumer().accept(e));
                    }
                }
            }
        }
    }
    return messageList;
}
Also used : DefaultMessage(org.apache.camel.impl.DefaultMessage) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) SharedEvent(org.openremote.model.event.shared.SharedEvent) TriggeredEventSubscription(org.openremote.model.event.TriggeredEventSubscription)

Aggregations

Message (org.apache.camel.Message)1 DefaultMessage (org.apache.camel.impl.DefaultMessage)1 TriggeredEventSubscription (org.openremote.model.event.TriggeredEventSubscription)1 SharedEvent (org.openremote.model.event.shared.SharedEvent)1