-
Notifications
You must be signed in to change notification settings - Fork 0
Home
darshan92kumar edited this page Oct 1, 2018
·
8 revisions
- use mvn install to build project or you can directly download the source as zip and import as project in eclipse.
- I used maven for sorting dependencies.
- mvn install will also run all the test cases.
public interface Work extends Callable<WorkReport> {
String getName();
WorkReport call();
}
public interface WorkFlow extends Work {
}
I have created 3 implementations of workflow :
- Sequential
- Conditional
- Parallel
To create a parallel flow we can use parallel flow builder
ParallelFlow parallelFlow = ParallelFlow .Builder.aNewParallelFlow()
.named("execute 'work1', 'work2' and 'work3' in parallel")
.execute(work1, work2, work3)
.build();
The parallel flow builder can be combined with sequential flow so that we achieve a hierarchy of workflows.
SequentialFlow sequentialFlow = SequentialFlow .Builder.aNewSequentialFlow()
..named("execute 'work1', 'work2' and 'work3' in sequence")
.execute(work1)
.then(work2)
.then(work3)
.build();
Conditional Flow will help us with some sort of condition based / rule based workflow execution.
ConditionalFlow conditionFlow = ConditionalFlow.Builder.aNewConditionalFlow()
.named("my conditional flow")
.execute(work1)
.when(WorkReportPredicate.COMPLETED)
.then(work2)
.otherwise(work3)
.build();
Below example shows us how to chain different flows together using sequential, parallel and conditional.
@Test
public void defineWorkFlowHedgeFundAndExecuteIt() throws Exception {
HedgeFundWork work1 = new HedgeFundWork("Trader Raise Fund Request");
HedgeFundWork work2 = new HedgeFundWork("Research Analyst Approval");
HedgeFundWork work3 = new HedgeFundWork("Fund Manager Approval");
HedgeFundWork work4 = new HedgeFundWork("Division Head Approval");
HedgeFundWork work5 = new HedgeFundWork("Operations Approval");
WorkFlow workflow = aNewSequentialFlow()
.execute(aNewRepeatFlow() // Start workflow with the request
.named(work1.getName())
.build())
.then(aNewConditionalFlow()
.execute(aNewParallelFlow() // Execute approvals parallely
.named(work2.getName()+" and "+work3.getName())
.execute(work2, work3)
.build())
.when(COMPLETED) // when above completes execute next in hierarchy
.then(work4)
.when(COMPLETED) // when above completes execute next in hierarchy
.then(work5)
.build())
.build();
WorkFlowEngine workFlowEngine = aNewWorkFlowEngine().build();
WorkReport workReport = workFlowEngine.run(workflow);
System.out.println("workflow report = " + workReport);
}
Custom flows can be implemented using the WorkFlow
interface. The WorkFlowEngine
works against interfaces, so your implementation should be work without any issues
I am using future task in executor framework to write a very basic reporting code.
public interface WorkReport {
WorkStatus getStatus();
Throwable getError();
}
REFERENCES : Programmer I & II Study Guide - Kathy Sierra , Stackoverflow, jeasy