IC4J API Docs
  • Overview
    • Introduction
  • Quick Start
  • License
  • Reference
    • API Reference
      • Install IC4J Libraries
      • Supported Types
      • ReplicaTransport
      • Identity
      • Principal
      • AgentBuilder
      • ProxyBuilder
      • Using IDLArgs
      • QueryBuilder and UpdateBuilder
      • Using Raw Agent Methods
      • Handle Binary Payloads
      • Object Serializers and Deserializers
        • Pojo Serializer and Deserializer
        • JSON Jackson Serializer and Deserializer
        • JSON Gson Serializer and Deserializer
        • XML DOM Serializer and Deserializer
        • XML JAXB Serializer and Deserializer
        • JDBC Serializer
      • Android Development
Powered by GitBook
On this page
  • Apache HTTP 5 Client transport implementation
  • OkHttp Client transport implementation
  • Java 11 HTTP Client transport implementation
  1. Reference
  2. API Reference

ReplicaTransport

PreviousSupported TypesNextIdentity

Last updated 11 months ago

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.

interface currently supports 4 Internet Computer functions.

Calls in interface are asynchronous and return the 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);
}

Apache HTTP 5 Client transport implementation

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.

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);
ReplicaTransport transport = 
ReplicaApacheHttpTransport.create("http://localhost:4943/",asyncClientConnectionManager, int timeout);

OkHttp Client transport implementation

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.

ReplicaTransport transport = 
ReplicaOkHttpTransport.create("http://localhost:4943/");

If needed, the Connection Timeout can be explicitly defined :

ReplicaTransport transport = 
ReplicaOkHttpTransport.create("http://localhost:4943/", timeout);

Java 11 HTTP Client transport implementation

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.

implementation 'org.ic4j:ic4j-java11transport:0.7.0'
<dependency>
    <groupId>org.ic4j</groupId>
    <artifactId>ic4j-java11transport</artifactId>
    <version>0.7.0</version>
</dependency>
ReplicaTransport transport = 
ReplicaJavaHttpTransport.create("http://localhost:4943/");

If needed the connection timeout can be defined explicitly.

ReplicaTransport transport = 
ReplicaJavaHttpTransport.create("http://localhost:4943/", timeout);

The simplest way to create is to use to create the Method with the IC URL String as a parameter.

For even more complex scenarios ReplicaTransport can be created with the explicitly defined Apache HTTP Client connection manager .

Use to create the Method with the IC URL String as a parameter to create OkHttp ReplicaTransport

Use to create the Method with the IC URL String as a parameter to create Java 11 ReplicaTransport

ReplicaTransport
ReplicaApacheHttpTransport
AsyncClientConnectionManager
ReplicaOkHttpTransport
ReplicaJavaHttpTransport
ReplicaTransport
ReplicaTransport
CompletableFuture
Apache HttpComponents – HttpClient Overview
Logo
Overview - OkHttp
Logo