Documentation Index
Fetch the complete documentation index at: https://docs.canton.network/llms.txt
Use this file to discover all available pages before exploring further.
How to work with contracts and transactions in Java
When writing Canton Network applications in Java, it is convenient to work with a representation of Daml templates and data types in Java that closely resemble the original Daml code while still being as close to the native types of Java as possible. To achieve this, use the Daml Codegen for Java to generate Java types based on a Daml model. You can then use these types in your Java code when reading information from and sending data to the ledger.Setup the codegen
Run and configure the code generator for Java according to Daml Codegen for Java to use and generate the Java classes for your project. See also in Generated code how the generated code looks for the Daml built-in and user-defined types.Use the generated classes in your project
In order to compile the resulting Java classes, you need to add the Java bindings library as dependency to your build tools. Add the following Maven dependency to your project:Replace
YOUR_SDK_VERSION with the version of your SDK.Access the gRPC Ledger API using Java bindings
Thebindings-java library comes pre-packaged with the generated gRPC stubs allowing you to access the Ledger API.
For each Ledger API service, there is a dedicated Java class with a matching name. For instance, the gRPC counterpart of CommandSubmissionService is CommandSubmissionServiceGrpc.
Connect to the ledger
To establish a connection to the ledger and access the Ledger API services create an instance of aManagedChannel using the static NettyChannelBuilder.forAddress(..) method. Then, call the factory method on the respective service to create a stub e.g. CommandSubmissionServiceGrpc.newFutureStub. Use one of the helper classes provided by the bindings-java to create an object representing the request service request arguments, convert them to a proto message. Finally, call the desired method offered by the service.
Perform authorization
Some ledgers enforce authorization and require to send an access token along with each request. For more details on authorization, read the Authorization overview. To use the same token for all Ledger API requests, use thewithCallCredentials method of the service stub class. As an argument, this method takes a class that derives from CallCredentials. Implement your own derivation that provides the token in the header.
Connect securely
The builders created byNettyChannelBuilder.forAddress default to a tls connection, where the keys are taken from the configured Java Keystore. You can override this behavior by providing your own cryptographic settings. To do so, invoke sslContext and pass an SslContext.
SslContext with the provided certificates for client authentication, please consult the gRPC documentation on TLS with OpenSSL as well as the HelloWorldClientTls example of the grpc-java project.
Use asynchronous stubs
The classes generated for the Ledger API gRPC services come in several flavors: blocking, future-based and asynchronous. The latter is the recommended way of interacting with the gRPC layer. In the example of theCommandService utilized above they are called CommandServiceBlockingStub, CommandServiceFutureStub and CommandServiceStub respectively.
For each gRPC endpoint that you want to call from your Java application, create a gRPC StreamObserver providing implementations of the onNext, onError and onComplete observer methods. To decode and encode the gRPC messages, use the fromProto and toProto methods of the generated classes of the Java bindings library.