@Override public Optional<DispatchedEvent<Event>> toDispatchedEvent(SerializedEvent se) { String eventType = se.getEventType(); Class<Event> eventClass = toEventClass(eventType); Event event = JSonMapper.fromJson(se.getEventData(), eventClass); return Optional.of(new DispatchedEvent<>(se.getEntityId(), se.getId(), event, se.getSwimLane(), se.getOffset(), se.getEventContext(), se.getMetadata() == null ? Optional.empty() : se.getMetadata().map(md -> JSonMapper.fromJson(md, Map.class)))); }
@Override public CompletableFuture<?> subscribe(String subscriberId, Map<String, Set<String>> aggregatesAndEvents, SubscriberOptions subscriberOptions, Function<SerializedEvent, CompletableFuture<?>> handler) { List<String> topics = aggregatesAndEvents.keySet() .stream() .map(AggregateTopicMapping::aggregateTypeToTopic) .collect(toList()); EventuateKafkaConsumer consumer = new EventuateKafkaConsumer(subscriberId, (record, callback) -> { SerializedEvent se = toSerializedEvent(record); if (aggregatesAndEvents.get(se.getEntityType()).contains(se.getEventType())) { handler.apply(se).whenComplete((result, t) -> { callback.accept(null, t); }); } else { callback.accept(null, null); } }, topics, eventuateLocalAggregateStoreConfiguration.getBootstrapServers(), eventuateKafkaConsumerConfigurationProperties); addConsumer(consumer); consumer.start(); return CompletableFuture.completedFuture(null); }
@Override protected void publish(PublishableEvents publishableEvents) { String aggregateType = publishableEvents.getAggregateType(); String aggregateId = publishableEvents.getEntityId(); List<EventIdTypeAndData> eventsWithIds = publishableEvents.getEventsWithIds(); List<Subscription> subscriptions; synchronized (aggregateTypeToSubscription) { List<Subscription> x = aggregateTypeToSubscription.get(aggregateType); subscriptions = x == null ? null : new ArrayList<>(x); } if (subscriptions != null) for (Subscription subscription : subscriptions) { for (EventIdTypeAndData event : eventsWithIds) { if (subscription.isInterestedIn(aggregateType, event.getEventType())) subscription.handler.apply(new SerializedEvent(event.getId(), aggregateId, aggregateType, event.getEventData(), event.getEventType(), aggregateId.hashCode() % 8, eventOffset.getAndIncrement(), new EventContext(event.getId().asString()), event.getMetadata())); } } }
private void assertContainsEventWithMetadata(Int128 expectedEventId, String expectedMetadata, LinkedBlockingQueue<SerializedEvent> events) throws InterruptedException { long now = System.currentTimeMillis(); long deadline = now + 10 * 1000; while (System.currentTimeMillis() < deadline) { SerializedEvent event = events.poll(100, TimeUnit.MILLISECONDS); if (event != null && event.getId().equals(expectedEventId)) { assertEquals(Optional.of(expectedMetadata), event.getMetadata()); return; } } fail("could not find"); }
private CompletableFuture<String> subscribeAsync(String subscribeId) { return (CompletableFuture<String>) client.subscribe(subscribeId, Collections.singletonMap("MyEntityType", Collections.singleton("MyEvent")), null, se -> { ids.add(se.getId()); return CompletableFuture.completedFuture("x"); }); }
@Override protected void publish(PublishableEvents publishableEvents) { String aggregateType = publishableEvents.getAggregateType(); String aggregateId = publishableEvents.getEntityId(); List<EventIdTypeAndData> eventsWithIds = publishableEvents.getEventsWithIds(); List<Subscription> subscriptions; synchronized (aggregateTypeToSubscription) { List<Subscription> x = aggregateTypeToSubscription.get(aggregateType); subscriptions = x == null ? null : new ArrayList<>(x); } if (subscriptions != null) for (Subscription subscription : subscriptions) { for (EventIdTypeAndData event : eventsWithIds) { if (subscription.isInterestedIn(aggregateType, event.getEventType())) subscription.handler.apply(new SerializedEvent(event.getId(), aggregateId, aggregateType, event.getEventData(), event.getEventType(), aggregateId.hashCode() % 8, eventOffset.getAndIncrement(), new EventContext(event.getId().asString()), event.getMetadata())); } } }
@Override public Optional<DispatchedEvent<Event>> toDispatchedEvent(SerializedEvent se) { String eventType = se.getEventType(); Class<Event> eventClass = toEventClass(eventType); Event event = JSonMapper.fromJson(se.getEventData(), eventClass); return Optional.of(new DispatchedEvent<>(se.getEntityId(), se.getId(), event, se.getSwimLane(), se.getOffset(), se.getEventContext(), se.getMetadata() == null ? Optional.empty() : se.getMetadata().map(md -> JSonMapper.fromJson(md, Map.class)))); }
private SerializedEvent makeSerializedEvent(StompEvent stompEvent) { return new SerializedEvent( stompEvent.getId(), stompEvent.getEntityId(), stompEvent.getEntityType(), stompEvent.getEventData(), stompEvent.getEventType(), stompEvent.getSwimlane(), stompEvent.getOffset(), new EventContext(stompEvent.getEventToken()), Optional.empty()); }
@Override public CompletableFuture<?> subscribe(String subscriberId, Map<String, Set<String>> aggregatesAndEvents, SubscriberOptions subscriberOptions, Function<SerializedEvent, CompletableFuture<?>> handler) { List<String> topics = aggregatesAndEvents.keySet() .stream() .map(AggregateTopicMapping::aggregateTypeToTopic) .collect(toList()); EventuateKafkaConsumer consumer = new EventuateKafkaConsumer(subscriberId, (record, callback) -> { SerializedEvent se = toSerializedEvent(record); if (aggregatesAndEvents.get(se.getEntityType()).contains(se.getEventType())) { handler.apply(se).whenComplete((result, t) -> { callback.accept(null, t); }); } else { callback.accept(null, null); } }, topics, eventuateLocalAggregateStoreConfiguration.getBootstrapServers(), eventuateKafkaConsumerConfigurationProperties); addConsumer(consumer); consumer.start(); return CompletableFuture.completedFuture(null); }
private SerializedEvent makeSerializedEvent(StompEvent stompEvent) { return new SerializedEvent( stompEvent.getId(), stompEvent.getEntityId(), stompEvent.getEntityType(), stompEvent.getEventData(), stompEvent.getEventType(), stompEvent.getSwimlane(), stompEvent.getOffset(), new EventContext(stompEvent.getEventToken()), Optional.empty()); }
private SerializedEvent toSerializedEvent(ConsumerRecord<String, String> record) { PublishedEvent pe = JSonMapper.fromJson(record.value(), PublishedEvent.class); return new SerializedEvent( Int128.fromString(pe.getId()), pe.getEntityId(), pe.getEntityType(), pe.getEventData(), pe.getEventType(), record.partition(), record.offset(), EtopEventContext.make(pe.getId(), record.topic(), record.partition(), record.offset()), pe.getMetadata()); }
private SerializedEvent toSerializedEvent(ConsumerRecord<String, String> record) { PublishedEvent pe = JSonMapper.fromJson(record.value(), PublishedEvent.class); return new SerializedEvent( Int128.fromString(pe.getId()), pe.getEntityId(), pe.getEntityType(), pe.getEventData(), pe.getEventType(), record.partition(), record.offset(), EtopEventContext.make(pe.getId(), record.topic(), record.partition(), record.offset()), pe.getMetadata()); }