Build

Aergo client를 builder를 사용해서 설정할 수 있습니다. 같은 목적을 가진 다른 설정은 overridden됩니다.

AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withNonBlockingConnect()  // ignored
    .withBlockingConnect()     // applied
    .build();

Endpoint

연결할 노드의 endpoint를 설정할 수 있습니다. 디폴트는 localhost:7845 입니다.

// connect to 'localhost:7845'
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .build();

Connect Strategy

연결 전략을 설정할 수 있습니다.

Non-Blocking 연결은 내부적으로 netty를 사용합니다.

// connect to 'localhost:7845' with non-blocking connect
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withNonBlockingConnect()
    .build();

Blocking 연결은 내부적ㅈ으로 okhttp를 사용합니다.

// connect to 'localhost:7845' with blocking connect
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withBlockingConnect()
    .build();

Connect Type

Plaintext로 연결합니다. 별도 동작이 없는 경우 plaintext로 설정됩니다.

// connect with plain text
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withPlainText()
    .build();

Tls로 연결합니다. client key는 pkcs8형식이어야 합니다.

// prepare cert files
InputStream serverCert = loadResourceAsStream("/cert/server.crt");
InputStream clientCert = loadResourceAsStream("/cert/client.crt");
InputStream clientKey = loadResourceAsStream("/cert/client.pem"); // must be pkcs8 format

// connect with plain text
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withTransportSecurity("servername", serverCert, clientCert, clientKey)
    .build();

Retry

client로 하는 연산들에 해새 재시도 횟수를 설정할 수 있습니다. 실패할 경우 설정된 기간만큼 기다렸다가 같은 인자로 재시도합니다.

// retry 3 count with a 1000ms interval
AergoClient aergoClient = new AergoClientBuilder()
    .withRetry(3, 1000L, TimeUnit.MILLISECONDS)
    .build();

Timeout

client로 하는 연산들에 대해 timeout을 설정할 수 있습니다.

// set timeout as 5000ms for each request.
AergoClient aergoClient = new AergoClientBuilder()
    .withTimeout(5000L, TimeUnit.MILLISECONDS)
    .build();

Close

Aergo client를 close합니다. Memory leak을 방지하기 위해서 필요합니다.

close method를 직접 호출함으로써 close할 수 있습니다.

// create
AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .withBlockingConnect()
    .withTimeout(10000L, TimeUnit.MILLISECONDS)
    .build();

// ... do some operations

// close
aergoClient.close();

Java 7 이후 나온 try-with-resources 구문을 사용해서 close 할 수 도 있습니다.

// try-with-resources block
try (AergoClient aergoClient = new AergoClientBuilder()
    .withEndpoint("localhost:7845")
    .build()) {

  // ... do some operations
}