In the context of the Internet Computer blockchain, a Replica refers to the Internet Computer protocol processes running on a node.
To be able to connect remotely to the Internet Computer Canister IC4J implements ReplicaTransport interface over different Java HTTP Client libraries. Developers can choose specific implementations based on their Java application use cases.
ReplicaTransport interface currently supports 4 Internet Computer functions.
Calls in ReplicaTransport interface are asynchronous and return the CompletableFuture response type.
public interface ReplicaTransport {
public CompletableFuture<byte[]> status();
public CompletableFuture<byte[]> query(Principal canisterId, byte[] envelope);
public CompletableFuture<byte[]> call(Principal canisterId, byte[] envelope, RequestId requestId);
public CompletableFuture<byte[]> readState(Principal canisterId, byte[] envelope);
}
The Apache HTTP 5 library is a robust, stable Java implementation of the HTTP protocol. It allows developers to define advanced features like connection pooling.
{% embed url="https://hc.apache.org/httpcomponents-client-5.1.x/index.html" %}
The simplest way to create ReplicaTransport is to use ReplicaApacheHttpTransport to create the Method with the IC URL String as a parameter.
ReplicaTransport transport =
ReplicaApacheHttpTransport.create("http://localhost:4943/");
For advanced use cases, for example, to create Java server type applications handling a large number of clients and canisters, additional parameters can be defined.
Parameter | |
---|---|
url | Canister URL |
maxTotal | Maximum total connections |
maxPerRoute | Maximum connections per route |
connectionTimeToLive | Time to live for connection in seconds |
timeout | Connection timeout in seconds |
ReplicaTransport transport =
ReplicaApacheHttpTransport.create("http://localhost:4943/", maxTotal, maxPerRoute,
connectionTimeToLive, int timeout);
For even more complex scenarios ReplicaTransport can be created with the explicitly defined Apache HTTP Client connection manager AsyncClientConnectionManager.
ReplicaTransport transport =
ReplicaApacheHttpTransport.create("http://localhost:4943/",asyncClientConnectionManager, int timeout);
For Android development it is recommended to use the OkHttp Client Implementation.
OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications.
{% embed url="https://square.github.io/okhttp" %}
Use ReplicaOkHttpTransport to create the Method with the IC URL String as a parameter to create OkHttp ReplicaTransport
ReplicaTransport transport =
ReplicaOkHttpTransport.create("http://localhost:4943/");
If needed, the Connection Timeout can be explicitly defined :
ReplicaTransport transport =
ReplicaOkHttpTransport.create("http://localhost:4943/", timeout);
From Java version 11 and higher, Oracle significantly improved functionality of Java default HTTP Client.
To make core libraries compatible with Java version 1.8, it is recommended that this version of transport is explicitly imported in the Graven or Maven build script.
{% tabs %} {% tab title="Gradle" %}
implementation 'org.ic4j:ic4j-java11transport:0.7.0'
{% endtab %}
{% tab title="Maven" %}
<dependency>
<groupId>org.ic4j</groupId>
<artifactId>ic4j-java11transport</artifactId>
<version>0.7.0</version>
</dependency>
{% endtab %} {% endtabs %}
Use ReplicaJavaHttpTransport to create the Method with the IC URL String as a parameter to create Java 11 ReplicaTransport
ReplicaTransport transport =
ReplicaJavaHttpTransport.create("http://localhost:4943/");
If needed the connection timeout can be defined explicitly.
ReplicaTransport transport =
ReplicaJavaHttpTransport.create("http://localhost:4943/", timeout);