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.Extension
net.osomahe.esk.EventStoreExtension
Producing events
- Extend
AbstractEvent
class - 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
AbstractEvent
class 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
@Produces
and correct qualifier see example:
java.util.Properties
with qualifiers@KafkaProducerConfig
for 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.Properties
with qualifiers@KafkaConsumerConfig
for 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
EventStorePublisherConfig
and (or)EventStoreConsumerConfig
to provide non-default properties
Customization
@TopicName
annotation for an event class to set different topic for given event(s)@EventName
annotation for an event class to set different event name@AsyncEvent
annotation for an event class to set asynchronous processing of events@ObeservesAsync
required for handling async events