-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…ew reimplementation.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.eventuate.examples.tram.ordersandcustomers.commondomain; | ||
|
||
public class CustomerSnapshotEvent { | ||
private Long id; | ||
private String name; | ||
private Money creditLimit; | ||
|
||
public CustomerSnapshotEvent() { | ||
} | ||
|
||
public CustomerSnapshotEvent(Long id, String name, Money creditLimit) { | ||
this.id = id; | ||
this.name = name; | ||
this.creditLimit = creditLimit; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Money getCreditLimit() { | ||
return creditLimit; | ||
} | ||
|
||
public void setCreditLimit(Money creditLimit) { | ||
this.creditLimit = creditLimit; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.eventuate.examples.tram.ordersandcustomers.commondomain; | ||
|
||
import io.eventuate.tram.events.common.DomainEvent; | ||
|
||
public class CustomerSnapshotStartingOffsetEvent implements DomainEvent { | ||
private String topic; | ||
private int partition; | ||
private long offset; | ||
|
||
public CustomerSnapshotStartingOffsetEvent() { | ||
} | ||
|
||
public CustomerSnapshotStartingOffsetEvent(String topic, int partition, long offset) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dartartem
Author
Owner
|
||
this.topic = topic; | ||
this.partition = partition; | ||
this.offset = offset; | ||
} | ||
|
||
public String getTopic() { | ||
return topic; | ||
} | ||
|
||
public void setTopic(String topic) { | ||
this.topic = topic; | ||
} | ||
|
||
public int getPartition() { | ||
return partition; | ||
} | ||
|
||
public void setPartition(int partition) { | ||
this.partition = partition; | ||
} | ||
|
||
public long getOffset() { | ||
return offset; | ||
} | ||
|
||
public void setOffset(long offset) { | ||
this.offset = offset; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,67 @@ | ||
package io.eventuate.examples.tram.ordersandcustomers.customers.service; | ||
|
||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.CustomerSnapshotEvent; | ||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.CustomerSnapshotStartingOffsetEvent; | ||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.Money; | ||
import io.eventuate.examples.tram.ordersandcustomers.customers.domain.Customer; | ||
import io.eventuate.examples.tram.ordersandcustomers.customers.domain.CustomerRepository; | ||
import io.eventuate.javaclient.commonimpl.JSonMapper; | ||
import io.eventuate.local.java.kafka.producer.EventuateKafkaProducer; | ||
import io.eventuate.tram.events.ResultWithEvents; | ||
import io.eventuate.tram.events.publisher.DomainEventPublisher; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class CustomerService { | ||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private CustomerRepository customerRepository; | ||
|
||
@Autowired | ||
private DomainEventPublisher domainEventPublisher; | ||
|
||
@Autowired | ||
private EventuateKafkaProducer eventuateKafkaProducer; | ||
|
||
public Customer createCustomer(String name, Money creditLimit) { | ||
ResultWithEvents<Customer> customerWithEvents = Customer.create(name, creditLimit); | ||
Customer customer = customerRepository.save(customerWithEvents.result); | ||
domainEventPublisher.publish(Customer.class, customer.getId(), customerWithEvents.events); | ||
return customer; | ||
} | ||
|
||
public void exportSnapshots() { | ||
AtomicReference<CompletableFuture<?>> metadataFuture = new AtomicReference<>(); | ||
|
||
StreamSupport | ||
.stream(customerRepository.findAll().spliterator(), false) | ||
.forEach(customer -> { | ||
CustomerSnapshotEvent customerSnapshotEvent = new CustomerSnapshotEvent(customer.getId(), customer.getName(), customer.getCreditLimit()); | ||
|
||
CompletableFuture<?> metadata = eventuateKafkaProducer.send("CustomerSnapshot", null, JSonMapper.toJson(customerSnapshotEvent)); | ||
This comment has been minimized.
Sorry, something went wrong.
dartartem
Author
Owner
|
||
metadataFuture.compareAndSet(null, metadata); | ||
}); | ||
|
||
try { | ||
RecordMetadata metadata = (RecordMetadata)metadataFuture.get().get(); | ||
|
||
eventuateKafkaProducer.send("CustomerSnapshotStartingOffset", | ||
null, | ||
This comment has been minimized.
Sorry, something went wrong.
dartartem
Author
Owner
|
||
JSonMapper.toJson(new CustomerSnapshotStartingOffsetEvent(metadata.topic(), | ||
metadata.partition(), | ||
This comment has been minimized.
Sorry, something went wrong. |
||
metadata.offset()))); | ||
|
||
} catch (InterruptedException | ExecutionException e) { | ||
logger.error(e.getMessage(), e); | ||
//TODO: report error to client | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,68 @@ | ||
package io.eventuate.examples.tram.ordersandcustomers.orderhistory.backend; | ||
|
||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.CustomerCreatedEvent; | ||
import io.eventuate.tram.events.subscriber.DomainEventEnvelope; | ||
import io.eventuate.tram.events.subscriber.DomainEventHandlers; | ||
import io.eventuate.tram.events.subscriber.DomainEventHandlersBuilder; | ||
import com.google.common.collect.ImmutableList; | ||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.CustomerSnapshotEvent; | ||
import io.eventuate.examples.tram.ordersandcustomers.commondomain.CustomerSnapshotStartingOffsetEvent; | ||
import io.eventuate.javaclient.commonimpl.JSonMapper; | ||
import io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.UUID; | ||
|
||
|
||
public class CustomerHistoryEventConsumer { | ||
|
||
@Autowired | ||
private OrderHistoryViewService orderHistoryViewService; | ||
|
||
@Value("${eventuatelocal.kafka.bootstrap.servers}") | ||
private String kafkaBootstrapServers; | ||
|
||
public DomainEventHandlers domainEventHandlers() { | ||
return DomainEventHandlersBuilder | ||
.forAggregateType("io.eventuate.examples.tram.ordersandcustomers.customers.domain.Customer") | ||
.onEvent(CustomerCreatedEvent.class, this::customerCreatedEventHandler) | ||
.build(); | ||
} | ||
// @Autowired | ||
// private EventuateKafkaConsumer eventuateKafkaConsumer; | ||
// public DomainEventHandlers domainEventHandlers() { | ||
// return DomainEventHandlersBuilder | ||
// .forAggregateType("io.eventuate.examples.tram.ordersandcustomers.customers.domain.Customer") | ||
// .onEvent(CustomerCreatedEvent.class, this::customerCreatedEventHandler) | ||
// .build(); | ||
// } | ||
// | ||
// private void customerCreatedEventHandler(DomainEventEnvelope<CustomerCreatedEvent> domainEventEnvelope) { | ||
// CustomerCreatedEvent customerCreatedEvent = domainEventEnvelope.getEvent(); | ||
// orderHistoryViewService.createCustomer(Long.parseLong(domainEventEnvelope.getAggregateId()), | ||
// customerCreatedEvent.getName(), customerCreatedEvent.getCreditLimit()); | ||
// } | ||
|
||
@PostConstruct | ||
public void init() { | ||
EventuateKafkaConsumer offsetConsumer = new EventuateKafkaConsumer(UUID.randomUUID().toString(), (offsetRecord, offsetCallback) -> { | ||
This comment has been minimized.
Sorry, something went wrong.
cer
|
||
CustomerSnapshotStartingOffsetEvent snapshotStartingOffsetEvent = JSonMapper.fromJson(offsetRecord.value(), | ||
CustomerSnapshotStartingOffsetEvent.class); | ||
|
||
EventuateKafkaConsumer snapshotConsumer = new EventuateKafkaConsumer(UUID.randomUUID().toString(), | ||
(snapshotRecord, snapshotCallback) -> { | ||
CustomerSnapshotEvent customerSnapshotEvent = JSonMapper.fromJson(snapshotRecord.value(), CustomerSnapshotEvent.class); | ||
|
||
orderHistoryViewService.createCustomer(customerSnapshotEvent.getId(), | ||
customerSnapshotEvent.getName(), | ||
customerSnapshotEvent.getCreditLimit()); | ||
|
||
offsetCallback.accept(null, null); | ||
}, | ||
ImmutableList.of("CustomerSnapshot"), | ||
kafkaBootstrapServers); | ||
|
||
//TODO: fix consumer fix (error: No current assignment for partition CustomerSnapshot-1) | ||
// snapshotConsumer.setTopicPartitionOffsets(ImmutableMap.of(new TopicPartition(snapshotStartingOffsetEvent.getTopic(), | ||
// snapshotStartingOffsetEvent.getPartition()), snapshotStartingOffsetEvent.getOffset())); | ||
|
||
snapshotConsumer.start(); | ||
This comment has been minimized.
Sorry, something went wrong.
dartartem
Author
Owner
|
||
|
||
offsetCallback.accept(null, null); | ||
}, ImmutableList.of("CustomerSnapshotStartingOffset"), kafkaBootstrapServers); | ||
|
||
private void customerCreatedEventHandler(DomainEventEnvelope<CustomerCreatedEvent> domainEventEnvelope) { | ||
CustomerCreatedEvent customerCreatedEvent = domainEventEnvelope.getEvent(); | ||
orderHistoryViewService.createCustomer(Long.parseLong(domainEventEnvelope.getAggregateId()), | ||
customerCreatedEvent.getName(), customerCreatedEvent.getCreditLimit()); | ||
offsetConsumer.start(); | ||
} | ||
} |
This event is published to a topic, partition, offset.
It does not need to contain this information.