Putting it all together
Now that we have everything set up, its time to wiring it together:
public class App {
public static void main(String[] args) {
Properties streamsConfig = new Properties();
streamsConfig.put(StreamsConfig.APPLICATION_ID_CONFIG, "customer-service");
streamsConfig.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
EasySourcing app = new EasySourcing.builder()
.streamsConfig(streamsConfig)
.registerHandler(new CustomerCommandHandler())
.registerHandler(new CustomerEventSourcingHandler())
.registerHandler(new CustomerEventHandler())
.build();
app.start();
}
}
Spring Boot
If you are using Spring Boot with the easysourcing-spring-boot-starter
dependency, your handlers will be registered automatically:
@Configuration
public class EasySourcingConfig {
@Bean
public EasySourcing easySourcing() {
Properties streamsConfig = new Properties();
streamsConfig.put(StreamsConfig.APPLICATION_ID_CONFIG, "customer-service");
streamsConfig.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
return new EasySourcing.builder()
.streamsConfig(streamsConfig)
.build();
}
@Bean
public CommandGateway commandGateway() {
Properties producerConfig = new Properties();
producerConfig.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
return new CommandGateway.builder()
.producerConfig(producerConfig)
.replyTopic("my-reply-topic")
.build();
}
}