<commit_before># Event Store JVM Client [![Build Status](https://travis-ci.org/EventStore/eventstorejvmclient.png?branch=master)](https://travis-ci.org/EventStore/eventstorejvmclient)

**JVM Client works nicely with EventStore 2.0.1**

<table border="0">
  <tr>
    <td>Scala </td>
    <td>2.10.3</td>
  </tr>
  <tr>
    <td>Akka </td>
    <td>2.2.3</td>
  </tr>
</table>

## Status

Currently the only API available is Actor-like via sending/receiving messages to actors.
This is not a user friendliest one, but if you need as much performance as possible, then use Actor-like api.

However we will provide more convenient API based on `scala.concurrent.Future`.

## Java Examples

### Read event

```java
import akka.actor.*;
import akka.actor.Status.Failure;
import akka.event.*;
import eventstore.*;
import eventstore.j.*;
import eventstore.tcp.ConnectionActor;

import java.net.InetSocketAddress;


public class ReadEventExample {

    public static void main(String[] args) {
        final ActorSystem system = ActorSystem.create();
        final Settings settings = new SettingsBuilder()
                .address(new InetSocketAddress("127.0.0.1", 1113))
                .defaultCredentials("admin", "changeit")
                .requireMaster(true)
                .build();
        final ActorRef connection = system.actorOf(ConnectionActor.props(settings));
        final ActorRef readEventActor = system.actorOf(Props.create(ReadEventActor.class, connection));
    }


    public static class ReadEventActor extends UntypedActor {
        final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

        ActorRef connection;

        public ReadEventActor(ActorRef connection) {
            this.connection = connection;
        }

        @Override
        public void preStart() throws Exception {
            final ReadEvent readEvent = new ReadEventBuilder("my-stream")
                    .eventNumberFirst()
                    .resolveLinkTos(false)
                    .requireMaster(true)
                    .build();

            connection.tell(readEvent, getSelf());
        }

        public void onReceive(Object message) throws Exception {
            if (message instanceof ReadEventCompleted) {
                final ReadEventCompleted completed = (ReadEventCompleted) message;
                final Event event = completed.event();
                log.info("EVENT: " + event.toString());
            } else if (message instanceof Failure) {
                final Failure failure = ((Failure) message);
                final EventStoreException exception = (EventStoreException) failure.cause();
                log.error("FAILED: reason: {}, message: {}", exception.reason(), exception.message());
            } else
                unhandled(message);
        }
    }
}
```

## Scala Examples

### Read event

```scala
import akka.actor.Status.Failure
import akka.actor._
import eventstore._
import eventstore.tcp.ConnectionActor
import java.net.InetSocketAddress

object ReadEventExample extends App {
  val system = ActorSystem()

  val settings = Settings(
    address = new InetSocketAddress("127.0.0.1", 1113),
    defaultCredentials = Some(UserCredentials("admin", "<PASSWORD>")))

  val connection = system.actorOf(ConnectionActor.props(settings))
  system.actorOf(Props(classOf[ReadEventActor], connection))
}

class ReadEventActor(connection: ActorRef) extends Actor with ActorLogging {

  connection ! ReadEvent(
    streamId = EventStream.Id("my-stream"),
    eventNumber = EventNumber.First)

  def receive = {
    case ReadEventCompleted(event)                        => log.info(s"SUCCEED: $event")
    case Failure(EventStoreException(reason, message, _)) => log.error(s"FAILED: reason $reason, message: $message")
  }
}
```

### Start transaction

```scala
package eventstore.examples

import akka.actor.ActorSystem
import eventstore.TransactionActor._
import eventstore.tcp.ConnectionActor
import eventstore.{ EventData, TransactionActor, EventStream, TransactionStart }

object StartTransactionExample extends App {
  val system = ActorSystem()
  val connection = system.actorOf(ConnectionActor.props())

  val kickoff = Start(TransactionStart(EventStream.Id("my-stream")))
  val transaction = system.actorOf(TransactionActor.props(connection, kickoff))

  transaction ! GetTransactionId // replies with `TransactionId(transactionId)`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Commit // replies with `CommitCompleted`
}
```

### Continue transaction

```scala
import akka.actor.ActorSystem
import eventstore.TransactionActor._
import eventstore.tcp.ConnectionActor<commit_msg>fix README<commit_after># Event Store JVM Client [![Build Status](https://travis-ci.org/EventStore/eventstorejvmclient.png?branch=master)](https://travis-ci.org/EventStore/eventstorejvmclient)

**JVM Client works nicely with EventStore 2.0.1**

<table border="0">
  <tr>
    <td>Scala </td>
    <td>2.10.3</td>
  </tr>
  <tr>
    <td>Akka </td>
    <td>2.2.3</td>
  </tr>
</table>

## Status

Currently the only API available is Actor-like via sending/receiving messages to actors.
This is not a user friendliest one, but if you need as much performance as possible, then use Actor-like api.

However we will provide more convenient API based on `scala.concurrent.Future`.

## Java Examples

### Read event

```java
import akka.actor.*;
import akka.actor.Status.Failure;
import akka.event.*;
import eventstore.*;
import eventstore.j.*;
import eventstore.tcp.ConnectionActor;

import java.net.InetSocketAddress;


public class ReadEventExample {

    public static void main(String[] args) {
        final ActorSystem system = ActorSystem.create();
        final Settings settings = new SettingsBuilder()
                .address(new InetSocketAddress("127.0.0.1", 1113))
                .defaultCredentials("admin", "changeit")
                .requireMaster(true)
                .build();
        final ActorRef connection = system.actorOf(ConnectionActor.props(settings));
        final ActorRef readEventActor = system.actorOf(Props.create(ReadEventActor.class, connection));
    }


    public static class ReadEventActor extends UntypedActor {
        final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

        ActorRef connection;

        public ReadEventActor(ActorRef connection) {
            this.connection = connection;
        }

        @Override
        public void preStart() throws Exception {
            final ReadEvent readEvent = new ReadEventBuilder("my-stream")
                    .eventNumberFirst()
                    .resolveLinkTos(false)
                    .requireMaster(true)
                    .build();

            connection.tell(readEvent, getSelf());
        }

        public void onReceive(Object message) throws Exception {
            if (message instanceof ReadEventCompleted) {
                final ReadEventCompleted completed = (ReadEventCompleted) message;
                final Event event = completed.event();
                log.info("EVENT: " + event.toString());
            } else if (message instanceof Failure) {
                final Failure failure = ((Failure) message);
                final EventStoreException exception = (EventStoreException) failure.cause();
                log.error("FAILED: reason: {}, message: {}", exception.reason(), exception.message());
            } else
                unhandled(message);
        }
    }
}
```

## Scala Examples

### Read event

```scala
import akka.actor.Status.Failure
import akka.actor._
import eventstore._
import eventstore.tcp.ConnectionActor
import java.net.InetSocketAddress

object ReadEventExample extends App {
  val system = ActorSystem()

  val settings = Settings(
    address = new InetSocketAddress("127.0.0.1", 1113),
    defaultCredentials = Some(UserCredentials("admin", "<PASSWORD>")))

  val connection = system.actorOf(ConnectionActor.props(settings))
  system.actorOf(Props(classOf[ReadEventActor], connection))
}

class ReadEventActor(connection: ActorRef) extends Actor with ActorLogging {

  connection ! ReadEvent(
    streamId = EventStream.Id("my-stream"),
    eventNumber = EventNumber.First)

  def receive = {
    case ReadEventCompleted(event)                        => log.info(s"SUCCEED: $event")
    case Failure(EventStoreException(reason, message, _)) => log.error(s"FAILED: reason $reason, message: $message")
  }
}
```

### Start transaction

```scala
import akka.actor.ActorSystem
import eventstore.TransactionActor._
import eventstore.tcp.ConnectionActor
import eventstore.{ EventData, TransactionActor, EventStream, TransactionStart }

object StartTransactionExample extends App {
  val system = ActorSystem()
  val connection = system.actorOf(ConnectionActor.props())

  val kickoff = Start(TransactionStart(EventStream.Id("my-stream")))
  val transaction = system.actorOf(TransactionActor.props(connection, kickoff))

  transaction ! GetTransactionId // replies with `TransactionId(transactionId)`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Write(EventData(eventType = "transaction-event")) // replies with `WriteCompleted`
  transaction ! Commit // replies with `CommitCompleted`
}
```

### Continue transaction

```scala
import akka.actor.ActorSystem
import eventstore.TransactionActor._
import eventstore.tcp.ConnectionActor
{'fraction_non_alphanumeric': 0.11055532870559412, 'fraction_numerical': 0.006329113924050633, 'mean_word_length': 3.3349557522123896, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1127531', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2555, 'n_words': 668}