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,
Ping
andPong
- two parties,
Alice
andBob
The logic of the application goes like this:
- The application injects a contract of type
Ping
forAlice
. Alice
sees this contract and exercises the consuming choiceRespondPong
to create a contract of typePong
forBob
.Bob
sees this contract and exercises the consuming choiceRespondPing
to create a contract of typePing
forAlice
.- 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
DamlLedgerClient
connecting 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
Ping
orPong
contracts.)run the
PingPongProcessor
forever 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:
Bob
is exercising theRespondPong
choice on the contract with ID#1:0
for the workflowPing-Alice-1
.- Count
0
means that this is the first choice after the initialPing
contract. - The workflow ID
Ping-Alice-1
conveys that this is the workflow triggered by the second initialPing
contract that was created byAlice
.
The second line is analogous to the first one.