Use ODF 2 Data Stores in Java Native Worker
If your project is based on the ODF 2 framework, it uses automatically named Data Stores. Names of those Data Stores are created using the pattern that contains the code and version of the AI Agent or AI Digital Worker (AI DW) to which they belong. The approach allows having multiple versions of the same AI DW to run on the same Control Tower without interference and to avoid potential conflicts with Data Stores of other AI DWs.
ODF 2, as a development framework, imposes such rules on Data Store names and handles these names automatically and transparently. The Java Native Worker (JNW) is not a framework and provides no limitations or suggestions for any details of an AI DW implementation.
When a project is ported to the JNW, you can access Data Stores as in ODF 2. To help with this, ODF 2 offers a utility that allows using repository classes initially written for ODF 2 inside JNW Task Processors with the same automatic name handling. It is available in ODF 2, starting with version 10.2.8.50.
Add a required dependency to the Maven module you are working with. The version of the dependency is handled by
odf2-bom.<dependency>
<groupId>com.workfusion.odf2</groupId>
<artifactId>odf2-datastores-for-jnw</artifactId>
</dependency>Inject
ConnectionSourceProviderintoTaskProcessoror another Spring component that requires it.@Autowired
ConnectionSourceProvider connectionSourceProvider;Use
connectionSourceProvider.newConnectionSource()to create aConnectionSourceobject. It uses theDataSourceavailable in the Spring context, for example, configured byjnw-toolkit-datastores. The object handles Data Store names in the same way as in ODF 2.Create a
RepositorywithConnectionSourceand use it where needed.
ConnectionSource created by ConnectionSourceProvider must not be reused between different executions of TaskProcessor or shared between different TaskProcessors. This is also true for Repositories using this ConnectionSource.
Mind that ConnectionSource is AutoCloseable and must be closed after usage.
See an example of a Task Processor that uses ConnectionSourceProvider:
@Component
public class DatastoreAccessTask implements ITaskProcessor {
@Autowired
ConnectionSourceProvider connectionSourceProvider;
@Override
public TaskOutputData process(TaskInputData input) throws TaskProcessorException {
try (final ConnectionSource connectionSource = connectionSourceProvider.newConnectionSource()) {
final SomeRepository someRepository = new SomeRepository(connectionSource);
// do something with the repository
} catch (SQLException | IOException e) {
// do some error handling
}
}
}