I want to close java application as soon as GRPC connection down

The issue can be easily reproduced with the official Quickstart-java example: IOU Quickstart Tutorial ā€” Daml SDK 1.17.1 documentation. When I restart the sandbox, while keeping the Java app running, the Java app is not closed, neither can re-establish the connection to the ledger.

You need to provide the error handling logic for that to happen. When using RxJava 2, which the Java bindings are based on, one way is to define your error handling logic to shutdown the JVM using doOnError (more on that on the RxJava 2 documentation).

1 Like

Just some additional info to the question: the exception seems to be swallowed by io.reactivex and com.daml.grpc.adapter. Would be nice to catch this somehow, perhaps to implement some OnError method as the exception name suggests?

io.reactivex.exceptions.OnErrorNotImplementedException: UNAVAILABLE: Server is shutting down
	at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
    ....
	at io.reactivex.internal.subscribers.BasicFuseableSubscriber.onError(BasicFuseableSubscriber.java:101)
	at com.daml.grpc.adapter.client.rs.BufferingResponseObserver.lambda$onError$3(BufferingResponseObserver.java:81)
    ...
Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE: Server is shutting down
	at io.grpc.Status.asRuntimeException(Status.java:534)
    ...

Interestingly, the Quickstart Java app seems to be half-resilient:
If I restart the ledger, but leave the Java app running, IOU creation is still working, but querying IOU does not seem to work. My steps:

  1. Start the ledger with the same ledger id: daml start --sandbox-option=--ledgerid=lid
  2. Start the app: mvn compile && mvn exec:java@run-quickstart
  3. Then stop the ledger and start again
  4. Sending POST to Java creates contracts, visible by Navigator: curl -X PUT -d '{"issuer":"Alice","owner":"Alice","currency":"AliceCoin","amount":8888.0,"observers":[]}' http://localhost:8080/iou
  5. Sending GET to Java to query shows Java did not update its state: curl -X GET http://localhost:8080/iou

Any help would be greatly appreciated, either stopping Java when ledger stops, or making it fully resilient. The title suggests the first option though.

Not working since if we not listening contract its not going into that method. Do you have implementation reference for that.

Iā€™m sorry, but unfortunately Iā€™m not following. If I understand your post correctly, you want to shutdown your application if the gRPC connection closes. If you are not listening to a gRPC stream, then how would you be reacting to a connection being closed?

doOnError works. thanks

Good to know, thanks for letting the community know.