How to restore connection if an error occurs when using ledgerClient.getTransactionsClient()

When using the following code:

ledgerClient.getTransactionsClient().getTransactions(offsetBegin, filtersByParty, true)
.subscribe(t → {
String wageringPoolOffset = t.getOffset();
t.getEvents().forEach(event → {
someFunction(event,wageringPoolOffset );
});
});

When the following errors occur, our program cannot continue to listen:


io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: Error Handling · ReactiveX/RxJava Wiki · GitHub | io.grpc.StatusRuntimeException: UNAVAILABLE: io exception

How can I optimize the code and try to restore connection again when an exception occurs,where can i add onError()

You might want to have a look at this answer. Furthermore, the RxJava documentation is a great place to find answers specific to the library, including error handling (here).

In the case of the code you shared, which I’m re-writing here for readability (I definitely recommend using the code block feature to show code with indentation):

ledgerClient.getTransactionsClient().
  getTransactions(offsetBegin, filtersByParty, true).
  subscribe(t → {
    String wageringPoolOffset = t.getOffset();
    t.getEvents().forEach(event → {
    someFunction(event,wageringPoolOffset );
  });
});

the error callbacks can be added to the Observable returned by getTransactions by calling for example the onErrorResumeNext on it (but of course do use the error handling operator that makes more sense for you).

Thank you

1 Like

After I added retry(), the code no longer has connection exceptions, and it can listen normally when it starts running, but after running for a while, can see the log of the successful creation and modification of the contract in canton, but the program will no longer continue to listen , the program log is no longer updated

ledgerClient.getTransactionsClient().
  getTransactions(offsetBegin, filtersByParty, true)
  .retry()
  .subscribe(t → {
    String wageringPoolOffset = t.getOffset();
    t.getEvents().forEach(event → {
    someFunction(event,wageringPoolOffset );
  });
});

This problem only occurs when the project connects to canton2.5.0 deployed on AWS. If retry() is not used, an exception will be reported. If retry() is used, no exception will be reported but the listen will stop.

When connecting to the locally deployed canton2.5.0, there is no exception , if retry() is used, even if the local canton is directly closed and restarted, there will be no exception and only re-listen.