EventStore / EventStoreDB-Client-Java

Official Asynchronous Java 8+ Client Library for EventStoreDB 20.6+
https://eventstore.com
Apache License 2.0
63 stars 19 forks source link

Connection issue with client version 4.1.1 #229

Closed cdevarenne closed 1 year ago

cdevarenne commented 1 year ago

I ran into a connection issue with client version 4.1.1.

I am working on OS X (Intel) and I run the db server in docker with:

docker run --name esdb-node -it -p 2113:2113 -p 1113:1113 \
    eventstore/eventstore:latest --insecure --run-projections=All

I see that the database is up and running and can access the EventStore dashboard at: http://localhost:2113/

After I build the code below, I run the JAR file with java -jar target/esdb-demo-1.0.jar and I get the following exception:

Exception in thread "main" java.util.concurrent.ExecutionException: com.eventstore.dbclient.ConnectionShutdownException: The connection is closed
    at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
    at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)
    at com.eventstore.EsdbDemo.main(EsdbDemo.java:37)
Caused by: com.eventstore.dbclient.ConnectionShutdownException: The connection is closed
    at com.eventstore.dbclient.GrpcClient.handleMsg(GrpcClient.java:230)
    at com.eventstore.dbclient.GrpcClient.messageLoop(GrpcClient.java:296)
    at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.lang.Thread.run(Thread.java:833)

Here is what I did

Maven pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.eventstore</groupId>
  <artifactId>esdb-demo</artifactId>
  <version>1.0</version>
  <name>docs-sample</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
        <groupId>com.eventstore</groupId>
        <artifactId>db-client-java</artifactId>
        <version>4.1.1</version>
    </dependency>    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.eventstore.EsdbDemo</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      </plugins>
  </build>
</project>

EsdbDemo.java

package com.eventstore;

import java.util.UUID;

/*
import com.eventstore.dbclient.EventStoreDBClient;
import com.eventstore.dbclient.EventStoreDBClientSettings;
import com.eventstore.dbclient.EventStoreDBConnectionString;
import com.eventstore.dbclient.EventData;
import com.eventstore.dbclient.ReadStreamOptions;
import com.eventstore.dbclient.ResolvedEvent;
import com.eventstore.dbclient.WriteResult;
import com.eventstore.dbclient.ReadResult;
*/
import com.eventstore.dbclient.*;

public class EsdbDemo {
    public static void main(String args[]) throws java.lang.InterruptedException, java.io.IOException, java.util.concurrent.ExecutionException /*, com.eventstore.dbclient.ConnectionStringParsingException */ {

        //final String connectionString = "esdb://127.0.0.1:2113?tls=false&defaultdeadline=60000&keepAliveTimeout=10000&keepAliveInterval=10000";
        String connectionString = "esdb://127.0.0.1:2113?tls=false"; // 0.0.0.0:2113&defaultdeadline=60000 127.0.0.1:2113 localhost:2113

        // From README 
        //EventStoreDBClientSettings settings = EventStoreDBConnectionString.parseOrThrow(connectionString);
        //EventStoreDBClient client = EventStoreDBClient.create(settings);

        // From https://github.com/EventStore/samples/blob/main/CQRS_Flow/Java/event-sourcing-esdb-simple/src/main/java/io/eventdriven/ecommerce/core/config/EventStoreDBConfig.java
        EventStoreDBClientSettings settings;
        EventStoreDBClient client;
        try {
                settings = EventStoreDBConnectionString.parse(connectionString);

                client = EventStoreDBClient.create(settings);
        } catch (Throwable e) {
                throw new RuntimeException(e);
        }        

        // From test/java/testcontainers/module/EventStoreDB.java
        //EventStoreDBClientSettings settings;
        //EventStoreDBClient client;
        EventStoreDBPersistentSubscriptionsClient persistentSubscriptionsClient;
        EventStoreDBProjectionManagementClient projectionClient;        

        //settings = EventStoreDBConnectionString.parseOrThrow(connectionString);
        //client = EventStoreDBClient.create(settings); // EventStoreDBClient.create(EventStoreDBConnectionString.parseOrThrow(connectionString)
        persistentSubscriptionsClient = EventStoreDBPersistentSubscriptionsClient.create(settings);        
        projectionClient = EventStoreDBProjectionManagementClient.create(settings);

        AccountCreated createdEvent = new AccountCreated();

        createdEvent.setId(UUID.randomUUID());
        createdEvent.setLogin("ouros");

        EventData event = EventData
                .builderAsJson("account-created", createdEvent)
                .build();

        WriteResult writeResult = client
                .appendToStream("accounts", event)
                .get();

        ReadStreamOptions readStreamOptions = ReadStreamOptions.get()
                .fromStart()
                .notResolveLinkTos();

        ReadResult readResult = client
                .readStream("accounts", readStreamOptions)
                .get();

        ResolvedEvent resolvedEvent = readResult
                .getEvents()
                .get(0);

        AccountCreated writtenEvent = resolvedEvent.getOriginalEvent()
                .getEventDataAs(AccountCreated.class);

        // Do some work...
    }
}

AccountCreated.java

package com.eventstore;

import java.util.UUID;

class AccountCreated {
    private UUID id;
    private String login;

    public UUID getId() {
        return id;
    }

    public String getLogin() {
        return login;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public void setLogin(String login) {
        this.login = login;
    }
}
cdevarenne commented 1 year ago

Here is the code in a zip archive.

java-client-4-1-1-demo.zip

cdevarenne commented 1 year ago

I did some more testing and this looks like this is due to the build as I am able to connect with a gradle build.

YoEight commented 1 year ago

Thanks @cdevarenne for the heads up. Would you mind sharing more details on this? I'm wondering if something could be done on our side still.