Giter Site home page Giter Site logo

eventstore-client's Introduction

Build Status Coverage Status Codacy Badge MavenCentral

Eventstore Client

The official Event Store page can be found at https://eventstore.org/

This project uses the Java 8 client for the EventStore available at https://github.com/msemys/esjc

Our adapter implementation is generic and can be used for any kind of event. Trivial mapping between domain objects and json-serializable objects should be implemented, if the domain objects themselves can not be serialized and deserialized.

In order to consume all events from a stream, method EventListeners.subscribeStream() should be called. There is a variant EventListenerssubscribeStreamStartingAt(...) when the stream should be consumed starting from a particular event number. The client ensures to reconnect to the eventstore after being disconnected for any reason.

The known eventstores for the client are configured by setting application properties as in the following (YML) example:

eventstores:
  - name: eventstore-team-a
    host: eventstore.host.name
    port: 1113
    username: admin
    password: changeit

Usage example

Common

Event

@Value.Immutable
@DataClass
public interface SomethingHappened extends Event {

    EventStreamId EVENT_STREAM_ID = EventStreamId.of(EventStoreName.of("default"), EventStreamName.of("test"));

    EventType EVENT_TYPE = EventType.of("something-happened");

    EventVersion EVENT_VERSION = EventVersion.of(3);

    EventSchemaRef EVENT_SCHEMA_REF = EventSchemaRef.of(URI.create("https://test.com/ref"));

    static ImmutableSomethingHappened.Builder builder() {
        return ImmutableSomethingHappened.builder();
    }

    String foo();
    
    Integer bar();
    
    @Override
    default EventType eventType() {
        return EVENT_TYPE;
    }
}

Serializable Event

@Value.Immutable
@DataClass
@JsonSerialize(as = ImmutableSomethingHappenedData.class)
@JsonDeserialize(as = ImmutableSomethingHappenedData.class)
public interface SomethingHappenedData extends EventData {

    static ImmutableSomethingHappenedData.Builder builder() {
        return ImmutableSomethingHappenedData.builder();
    }
    
    String foo();
    
    Integer bar();
}

For consuming events of a particular stream

@Component
@AllArgsConstructor
@Slf4j
public class SomethingHappenedEventConsumer implements EventConsumer<SomethingHappenedData> {
    
    @Override
    public EventStreamId eventStreamId() {
        return SomethingHappened.EVENT_STREAM_ID;
    }

    @Override
    public void onEvent(SomethingHappenedData data, EventMetadata metadata) {
        val event = SomethingHappened.builder()
                .eventId(metadata.eventId())
                .timestamp(data.timestamp())
                .foo(data.foo())
                .bar(data.bar)
                .build();
        
        log.info("received event {} with metadata {}", event, metadata);
        
        // code to consume event should be placed here
    }

    @Override
    public Class<? extends SomethingHappenedData> getSerializableDataType() {
        return SomethingHappenedData.class;
    }

    @Override
    public EventType eventType() {
        return SomethingHappened.EVENT_TYPE;
    }

}

For writing events to a particular stream

@Component
class SomethingHappenedEventConfiguration implements EventConfiguration<SomethingHappened> {

    public SomethingHappenedData map(SomethingHappened somethingHappened) {
        return SomethingHappenedData.builder()
                .timestamp(somethingHappened.timestamp())
                .foo(somethingHappened.foo())
                .bar(somethingHappened.bar())
                .build();
    }

    @Override
    public EventStreamId eventStreamId() {
        return EVENT_STREAM_ID;
    }

    @Override
    public EventType getType() {
        return EVENT_TYPE;
    }

    @Override
    public EventVersion eventVersion() {
        return EVENT_VERSION;
    }

    @Override
    public EventSchemaRef eventSchemaRef() {
        return EVENT_SCHEMA_REF;
    }

    @Override
    public Function1<SomethingHappened, Object> mapper() {
        return this::map;
    }

}

eventstore-client's People

Contributors

wuan avatar ewal31 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.