Example project¶
To try out the Java bindings library, use the examples on GitHub: PingPongReactive or PingPongComponents.
The former example does not use the Reactive Components, and the latter example does. Both examples implement 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 choice- RespondPongto create a contract of type- Pongfor- Bob.
- Bobsees this contract and exercises the consuming choice- RespondPingto create a contract of type- Pingfor- Alice.
- Points 2 and 3 are repeated until the maximum number of contracts defined in the Daml is reached.
Setting 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 three examples of the PingPong application, built with gRPC (non-reactive), Reactive and Reactive Component bindings respectively.
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 Ledger
- connect 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 - Pingor- Pongcontracts.)
- run the - PingPongProcessorforever by connecting them to the incoming transactions
- inject 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 the- RespondPongchoice on the contract with ID- #1:0for the workflow- Ping-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.