Tasks
Registry
The brainslug context has a Registry
where singletons of service classes can be registered:
context.getRegistry().registerService(Delegate.class, new Delegate());
or retrieved:
Delegate delegateService = context.getRegistry().getService(Delegate.class);
Ways to define a task
Inline Task
FlowBuilder inlineTaskFlow = new FlowBuilder() {
@Override
public void define() {
flowId(id("task_flow"));
start(event(id("start")).display("Start"))
.execute(task(id("task"), new Task() {
@Override
public void execute(ExecutionContext ctx) {
ctx.service(ExampleService.class).doSomething();
}
}).display("Do Something"))
.end(event(id("end")).display("End"));
}
};
In Java 8 the task can be defined using a lambda expression:
ctx -> {
ctx.service(MyService.class).doSomething();
}
Delegate class
If you do not want to specify the method by name, you can use the Execute
-annotation to define which
method you want be executed for a task:
class TestDelegate implements Delegate {
@Execute
abstract public void execute(TestService testService, ExecutionContext context);
}
FlowDefinition handlerFlow = new FlowBuilder() {
@Override
public void define() {
start(event(id(START)))
.execute(task(id(TASK)).delegate(TestDelegate.class))
.end(event(id(END)));
}
}.getDefinition();
Service Call
You may use service call definition, to directly define the invocation of a method during flow node definition.
FlowDefinition serviceCallFlow = new FlowBuilder() {
@Override
public void define() {
start(event(id(START)))
.execute(task(id(TASK)).call(method(TestService.class).name("getString")))
.end(event(id(END)));
}
}.getDefinition();
Typesafe Service Call
It possible to define service calls using a proxy-based approach similar to Mockito.
interface TestService {
public String getString();
public String echo(String echo);
}
FlowDefinition serviceCallFlow = new FlowBuilder() {
@Override
public void define() {
TestService testService = service(TestService.class);
start(event(id(START)))
.execute(task(id(TASK)).call(method(testService.echo(testService.getString()))))
.end(event(id(END)));
}
}.getDefinition();
In this case, the call to the service will be made at execution using the recorded argument values.
This will be done using reflection on the instance of the service, which must be available in the Registry
.