Sharing Resources (e.g. DB connection) for Activities in Java

Hi, I need to create Activities that access external resources such as cassandra and elastic. To avoid creating new connections each time an Activity is called I wanted to understand if it is possible to share the connections across all Activities. Would this be achieved by passing the connections through when the Activities are created and registered with the worker?

The activity implementation object is constructed by your code. So you can inject any dependencies into it before registering with a worker.

Thanks Maxim.

So if I had something similar to this:

// create a connection to a database to be used by the Activity
DatabaseClient dbClient = new DatabaseClient(connectionParam1, connectionParam2,…);
worker.registerActivitiesImplementations(new MyTestActivityWithDBConnectionImpl(dbClient));

If the worker node goes down for some reason Temporal is able to re-create the DatabaseClient used by the Activity? Does the DatabaseClient need to be structured in a particular way in order for Temporal to be able to re-create it? Thanks.

Temporal recovers state of workflow executions only. The activity implementation object will have to be recreated by your code on the process startup.

Could you clarify what you mean by “process startup”?

E.g. if I create a simple workflow that is setup to run every hour as a cron and has a single Activity that checks the status of a database I would:

  1. Create the worker
  2. Register the workflow implementation with the worker
  3. Initialise the Activity and register it with the worker

The workflow starts running every hour and then at some point temporal is restarted. Do steps 1- 3 need to be re-run again at this point for the workflows to continue running?

The above 3 steps are preparing worker to execute activities and workflows. But you still have to start a workflow execution explicitly, possibly from a different process. The steps 1-3 should be reexecuted on the process restart that is not related to the workflow execution in any way.

Thanks Maxim - are you referring to Temporal when you talk about the process restart?

No, I refer to your worker processes.

Thanks Maxim - my initial thoughts on how the workers ran was incorrect. This makes sense now.