Lifecycle Observer for CordaService

March 30, 2020

In the latest release of Corda open source v4.4, we introduced a feature to monitor the lifecycle of Corda service.

Corda services are…

Corda services are long-lived instances that can trigger or be triggered by flows from within a node. A service class is limited to a single instance per node. They allow related, reusable, functions to be separated into their own class where their functionality is grouped together.

To create a Corda service:

  • Add the @CordaService annotation
  • Add a constructor with a single parameter of AppServiceHub
  • Extend SingletonSerializeAsToken

You can find simple demo at this autopayroll-CordaService sample.

We observed that…

With the flexibilities of the Corda service, we are starting to see more and more use of it. However, we have also experienced some incidences, when the service is “upset”. For example:

  • The service expects to find a state in the vault and, if not found, the service is unable to continue.
  • It starts a flow on node start-up which returns an error condition and service decided that it cannot continue.

In these critical conditions, it makes it impossible for service to function going forward.

Introducing Lifecycle Observer…

Upon instantiation of the Corda service:

Kotlin Code:

Java Code:

This code will register to the node’s service hub and monitored by the life cycle observer.
Now, when any service feels that it cannot continue, it may throw CordaServiceCriticalFailureException from lifecycle handler which will lead to node terminating JVM. This fail-fast approach will let the node operator spot the service failure explicitly, which will lead to minimizing the later operation cost.

More information can be found at docs, ServiceLifecycleObserver.kt

Thank you for reading and being part of this journey!

We’d love to hear about your experiences and answer any questions you have in our public slack channel: http://slack.corda.net/.