Event Store Kafka
CDI extension for Java EE 8 application using Apache Kafka as Event Store.
Five minute start
How to quickly start using the Event Store Kafka with your Java EE 8 project.
Setup
- Add maven dependencies to your pom.xml
<dependency> <groupId>net.osomahe</groupId> <artifactId>eventstore-kafka</artifactId> <version>0.4.3</version> </dependency>
- Added extensions
src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extensionnet.osomahe.esk.EventStoreExtension
Producing events
- Extend
AbstractEventclass - Use
EventStorePublisher#publish(event)method for publishing event to Apache Kafka synchronously. or - Use
EventStorePublisher#publishAsync(event)method for publishing event to Apache Kafka asynchronously.
Consuming events
- Extend
AbstractEventclass or use the same as for publishing. - Observes for CDI event
public void handleCreate(@Observes TodoCreatedEvent event) { // do some magic with event }
Advanced
Configuration possibilities and default values. Unfortunately JavaEE 8 does not have any standard means of configuration and I didn't want to
Configuration
Unfortunately JavaEE 8 does not provide any standard way. There are two ways of configuring the eventstore-kafka.
- Application has to produce using
@Producesand correct qualifier see example:
java.util.Propertieswith qualifiers@KafkaProducerConfigfor overriding producer properties otherwise default values will be in place.Properties props = new Properties(); props.put(BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ACKS_CONFIG, "all");
java.util.Propertieswith qualifiers@KafkaConsumerConfigfor overriding consumer properties otherwise default values will be in place.Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(GROUP_ID_CONFIG, "client-application"); props.put(AUTO_OFFSET_RESET_CONFIG, "earliest");
- Application has to implement
EventStorePublisherConfigand (or)EventStoreConsumerConfigto provide non-default properties
Customization
@TopicNameannotation for an event class to set different topic for given event(s)@EventNameannotation for an event class to set different event name@AsyncEventannotation for an event class to set asynchronous processing of events@ObeservesAsyncrequired for handling async events