Search in sources :

Example 1 with NewtonEvent

use of io.muoncore.newton.NewtonEvent in project newton by muoncore.

the class MuonLookupUtils method init.

static void init(String[] packages) {
    List<URL> urls = new ArrayList<>();
    urls.addAll(ClasspathHelper.forPackage("io.muoncore.newton", MuonLookupUtils.class.getClassLoader()));
    for (String aPackage : packages) {
        log.info("Adding package {}", aPackage);
        Collection<URL> urls1 = ClasspathHelper.forPackage(aPackage, MuonLookupUtils.class.getClassLoader());
        log.info("Got {}", urls1);
        urls.addAll(urls1);
    }
    log.info("Booting Reflections with urls {}", urls);
    Reflections reflections = new Reflections(new ConfigurationBuilder().addScanners(new SubTypesScanner()).addScanners(new TypeAnnotationsScanner()).setUrls(urls));
    final Set<Class<? extends NewtonEvent>> eventTypes = reflections.getSubTypesOf(NewtonEvent.class);
    eventTypeMappings = new HashMap<>();
    for (Class<? extends NewtonEvent> newtonEvent : eventTypes) {
        eventTypeMappings.put(newtonEvent.getSimpleName(), newtonEvent);
    }
    final Set<Class<? extends AggregateRoot>> aggregateRootTypes = reflections.getSubTypesOf(AggregateRoot.class);
    aggregateRootMappings = new HashMap<>();
    for (Class<? extends AggregateRoot> root : aggregateRootTypes) {
        aggregateRootMappings.put(root.getSimpleName(), root);
    }
    final Set<Class<? extends Saga>> sagaTypes = reflections.getSubTypesOf(Saga.class);
    sagaTypeMappings = new HashMap<>();
    for (Class<? extends Saga> root : sagaTypes) {
        sagaTypeMappings.put(root.getSimpleName(), root);
    }
    ready.countDown();
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) NewtonEvent(io.muoncore.newton.NewtonEvent) URL(java.net.URL) Saga(io.muoncore.newton.saga.Saga) AggregateRoot(io.muoncore.newton.AggregateRoot) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections)

Example 2 with NewtonEvent

use of io.muoncore.newton.NewtonEvent 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)2 AggregateRoot (io.muoncore.newton.AggregateRoot)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 Saga (io.muoncore.newton.saga.Saga)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 URL (java.net.URL)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