/
Task Routing Provider configuration

Task Routing Provider configuration

To enable custom task routing, TASKANA provides the TaskRoutingProvider SPI.

This SPI defines an API (interface pro.taskana.spi.task.api.TaskRoutingProvider) that can be implemented by custom code.

TASKANA doesn’t allow to create a task outside of a workbasket. If the the taskana createTask() API is invoked without a Workbasket, TASKANA will call the TaskRoutingProvider SPI’s method determineWorkbasket() (if there is an implementation registered) in order to determine an appropriate Workbasket in which the Task will be created. The primary usage of this SPI is in the context of the TASKANA Adapter, but it can also be used without the Adapter.

The custom TaskRoutingProvider class must implement interface pro.taskana.task.api.TaskRoutingProvider. Especially, it must override methods initialize and determineWorkbasketId(Task task).

Method determineWorkbasketId(Task task) will be invoked by TASKANA when it is asked to create a Task that has no Workbasket assigned. The method gets passed the Task and it is the responsibility of this method to determine the id of the Workbasket in which the Task is to be created.

In order to use this SPI, you must

  • create a class that implements the pro.taskana.spi.routing.api.TaskRoutingProvider interface.

  • place that class into the classpath of your application

  • provide a control file with name pro.taskana.spi.routing.api.TaskRoutingProvider in the subdirectory META-INF/services of the classpath. This control file must contain the fully qualified classname of the class that implements the TaskRoutingProvider interface. This control file is used by the ServiceLoader to load the custom class at runtime. The control file may contain multiple classes that implement the TaskRoutingProvider interface.

If you provide a TaskRoutingProvider implementation, TASKANA will invoke the TaskRoutingProvider implementation whenever a Task with empty workbasketId and workbasketKey is about to be created.
If more than one TaskRoutingProvider class is registered, TASKANA calls them all and uses their results only if they agree on the Workbasket. This is, if more than one workbasketIds are returned, TASKANA uses them only if they are identical. If different ids are returned, TASKANA throws a InvalidArgumentException.
As an example, pro.taskana.sample.taskrouting.ExampleTaskRoutingProviderForAdapterTest routes Tasks to random Workbaskets.

 

package pro.taskana.sample.taskrouting; import java.util.List; import java.util.Random; import pro.taskana.common.api.TaskanaEngine; import pro.taskana.spi.routing.api.TaskRoutingProvider; import pro.taskana.task.api.Task; import pro.taskana.workbasket.api.WorkbasketSummary; public class ExampleTaskRoutingProviderForAdapterTest implements TaskRoutingProvider { TaskanaEngine theEngine; @Override public void initialize(TaskanaEngine taskanaEngine) { theEngine = taskanaEngine; } @Override public String determineWorkbasketId(Task task) { List<WorkbasketSummary> wbs = theEngine.getWorkbasketService().createWorkbasketQuery().list(); if (wbs != null && !(wbs.isEmpty())) { Random random = new Random(); int n = random.nextInt(wbs.size()); return wbs.get(n).getId(); } else { return null; } } }