Java Bindings Example Project¶
To try out the Java bindings library, use the examples on GitHub: PingPongReactive.
The example implements the PingPong application, which consists of:
- a Daml model with two contract templates,
PingandPong - two parties,
AliceandBob
The logic of the application goes like this:
- The application injects a contract of type
PingforAlice. Alicesees this contract and exercises the consuming choiceRespondPongto create a contract of typePongforBob.Bobsees this contract and exercises the consuming choiceRespondPingto create a contract of typePingforAlice.- Points 2 and 3 are repeated until the maximum number of contracts defined in the Daml is reached.
Set Up the Example Projects¶
To set up the example projects, clone the public GitHub repository at github.com/digital-asset/ex-java-bindings and follow the setup instruction in the README file.
This project contains two examples of the PingPong application, built directly with gRPC and using the RxJava2-based Java bindings.
Example Project¶
PingPongMain.java¶
The entry point for the Java code is the main class src/main/java/examples/pingpong/grpc/PingPongMain.java. Look at this class to see:
- how to connect to and interact with a Daml Ledger via the Java bindings
- how to use the Reactive layer to build an automation for both parties.
At high level, the code does the following steps:
creates an instance of
DamlLedgerClientconnecting to an existing Ledgerconnect this instance to the Ledger with
DamlLedgerClient.connect()create two instances of
PingPongProcessor, which contain the logic of the automation(This is where the application reacts to the new
PingorPongcontracts.)run the
PingPongProcessorforever by connecting them to the incoming transactionsinject some contracts for each party of both templates
wait until the application is done
PingPongProcessor.runIndefinitely()¶
The core of the application is the PingPongProcessor.runIndefinitely().
The PingPongProcessor queries the transactions first via the TransactionsClient of the DamlLedgerClient. Then, for each transaction, it produces Commands that will be sent to the Ledger via the CommandSubmissionClient of the DamlLedgerClient.
Output¶
The application prints statements similar to these:
Bob is exercising RespondPong on #1:0 in workflow Ping-Alice-1 at count 0
Alice is exercising RespondPing on #344:1 in workflow Ping-Alice-7 at count 9
The first line shows that:
Bobis exercising theRespondPongchoice on the contract with ID#1:0for the workflowPing-Alice-1.- Count
0means that this is the first choice after the initialPingcontract. - The workflow ID
Ping-Alice-1conveys that this is the workflow triggered by the second initialPingcontract that was created byAlice.
The second line is analogous to the first one.