Search in sources :

Example 1 with EventStreamIndexStore

use of io.muoncore.newton.query.EventStreamIndexStore in project newton by muoncore.

the class MuonClusterAwareTrackingSubscriptionManager method localTrackingSubscription.

private void localTrackingSubscription(String subscriptionName, String streamName, Consumer<NewtonEvent> onData, Consumer<Throwable> onError) {
    EventStreamIndex eventStreamIndex = getEventStreamIndex(subscriptionName, streamName);
    Long lastSeen = eventStreamIndex.getLastSeen() + 1;
    log.info("Subscribing from index {} to event stream {} '{}'", lastSeen, subscriptionName, streamName);
    Map args = new HashMap();
    args.put("from", lastSeen);
    args.put("sub-name", subscriptionName);
    eventClient.replay(streamName, EventReplayMode.REPLAY_THEN_LIVE, args, new EventSubscriber(event -> {
        log.trace("Store is {}, event is {}, time is {}", eventStreamIndexStore, event, event.getOrderId());
        Class<? extends NewtonEvent> eventType = MuonLookupUtils.getDomainClass(event);
        if (log.isTraceEnabled()) {
            log.trace("Store is {}, event is {}, time is {}", eventStreamIndexStore, event, event.getOrderId());
        }
        eventStreamIndexStore.save(new EventStreamIndex(subscriptionName, event.getOrderId() == null ? 0l : event.getOrderId()));
        NewtonEvent newtonEvent;
        if (eventType == null) {
            newtonEvent = new EventTypeNotFound(event.getOrderId(), event);
        } else {
            newtonEvent = MuonLookupUtils.decorateMeta(event.getPayload(eventType), event);
        }
        worker.execute(() -> {
            eventStreamProcessor.executeWithinEventContext(newtonEvent, onData);
        });
    }, onError));
}
Also used : java.util(java.util) StreamSubscriptionManager(io.muoncore.newton.StreamSubscriptionManager) MuonLookupUtils(io.muoncore.newton.utils.muon.MuonLookupUtils) Executor(java.util.concurrent.Executor) NewtonEventClient(io.muoncore.newton.NewtonEventClient) EventClient(io.muoncore.protocol.event.client.EventClient) Executors(java.util.concurrent.Executors) Consumer(java.util.function.Consumer) EventStreamProcessor(io.muoncore.newton.eventsource.muon.EventStreamProcessor) EventReplayMode(io.muoncore.protocol.event.client.EventReplayMode) EventStreamIndexStore(io.muoncore.newton.query.EventStreamIndexStore) Slf4j(lombok.extern.slf4j.Slf4j) EventTypeNotFound(io.muoncore.newton.eventsource.EventTypeNotFound) Subscription(org.reactivestreams.Subscription) NewtonEvent(io.muoncore.newton.NewtonEvent) EventStreamIndex(io.muoncore.newton.query.EventStreamIndex) Subscriber(org.reactivestreams.Subscriber) EventTypeNotFound(io.muoncore.newton.eventsource.EventTypeNotFound) EventStreamIndex(io.muoncore.newton.query.EventStreamIndex) NewtonEvent(io.muoncore.newton.NewtonEvent)

Aggregations

NewtonEvent (io.muoncore.newton.NewtonEvent)1 NewtonEventClient (io.muoncore.newton.NewtonEventClient)1 StreamSubscriptionManager (io.muoncore.newton.StreamSubscriptionManager)1 EventTypeNotFound (io.muoncore.newton.eventsource.EventTypeNotFound)1 EventStreamProcessor (io.muoncore.newton.eventsource.muon.EventStreamProcessor)1 EventStreamIndex (io.muoncore.newton.query.EventStreamIndex)1 EventStreamIndexStore (io.muoncore.newton.query.EventStreamIndexStore)1 MuonLookupUtils (io.muoncore.newton.utils.muon.MuonLookupUtils)1 EventClient (io.muoncore.protocol.event.client.EventClient)1 EventReplayMode (io.muoncore.protocol.event.client.EventReplayMode)1 java.util (java.util)1 Executor (java.util.concurrent.Executor)1 Executors (java.util.concurrent.Executors)1 Consumer (java.util.function.Consumer)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Subscriber (org.reactivestreams.Subscriber)1 Subscription (org.reactivestreams.Subscription)1