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