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

**URL:** <https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386>\
**Category:** App Development\
**Tags:** daml, features and bugs\
**Created:** [October 28, 2021, 8:54am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386 "2021-10-28T08:54:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sourabh\_Nimkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sourabh_nimkar/32/2726_2.png) [@Sourabh\_Nimkar](https://forum.canton.network/u/Sourabh_Nimkar)\
**Post date:** [October 28, 2021, 8:54am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/1 "2021-10-28T08:54:50Z")

</div>

The issue can be easily reproduced with the official Quickstart-java example: [IOU Quickstart Tutorial — Daml SDK 1.17.1 documentation](https://docs.daml.com/app-dev/bindings-java/quickstart.html). 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.

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [October 28, 2021, 10:26am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/3 "2021-10-28T10:26:06Z")

</div>

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)](http://reactivex.io/RxJava/2.x/javadoc/).

---

<div class="post-metadata">

**Author:** ![Richard\_Kapolnai](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/richard_kapolnai/32/89_2.png) [@Richard\_Kapolnai](https://forum.canton.network/u/Richard_Kapolnai)\
**Post date:** [October 28, 2021, 10:33am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/4 "2021-10-28T10:33:38Z")

</div>

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?

```haskell
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.

---

<div class="post-metadata">

**Author:** ![Sourabh\_Nimkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sourabh_nimkar/32/2726_2.png) [@Sourabh\_Nimkar](https://forum.canton.network/u/Sourabh_Nimkar)\
**Post date:** [October 28, 2021, 11:11am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/5 "2021-10-28T11:11:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [October 28, 2021, 1:03pm UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/6 "2021-10-28T13:03:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sourabh\_Nimkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sourabh_nimkar/32/2726_2.png) [@Sourabh\_Nimkar](https://forum.canton.network/u/Sourabh_Nimkar)\
**Post date:** [October 29, 2021, 9:30am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/7 "2021-10-29T09:30:15Z")

</div>

doOnError works. thanks

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [October 29, 2021, 11:25am UTC](https://forum.canton.network/t/i-want-to-close-java-application-as-soon-as-grpc-connection-down/3386/8 "2021-10-29T11:25:04Z")

</div>

Good to know, thanks for letting the community know.
