Search in sources :

Example 66 with DefaultMessage

use of org.apache.camel.impl.DefaultMessage 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

DefaultMessage (org.apache.camel.impl.DefaultMessage)66 Message (org.apache.camel.Message)59 Exchange (org.apache.camel.Exchange)32 Test (org.junit.Test)31 DefaultExchange (org.apache.camel.impl.DefaultExchange)24 HashMap (java.util.HashMap)19 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)11 CamelContext (org.apache.camel.CamelContext)7 Producer (org.apache.camel.Producer)6 VolumeProducer (org.apache.camel.component.openstack.cinder.producer.VolumeProducer)6 GlanceProducer (org.apache.camel.component.openstack.glance.GlanceProducer)6 ProjectProducer (org.apache.camel.component.openstack.keystone.producer.ProjectProducer)6 NetworkProducer (org.apache.camel.component.openstack.neutron.producer.NetworkProducer)6 KeypairProducer (org.apache.camel.component.openstack.nova.producer.KeypairProducer)6 ContainerProducer (org.apache.camel.component.openstack.swift.producer.ContainerProducer)6 DefaultHeadersMapFactory (org.apache.camel.impl.DefaultHeadersMapFactory)6 NotificationMessage (se.inera.intyg.common.support.modules.support.api.notification.NotificationMessage)5 IOException (java.io.IOException)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Method (java.lang.reflect.Method)3