-
Notifications
You must be signed in to change notification settings - Fork 216
tutorial
Mahmoud Ben Hassine edited this page Mar 12, 2020
·
5 revisions
This a simple tutorial about Easy Flows key APIs. First let's write some work:
class PrintMessageWork implements Work {
private String message;
public PrintMessageWork(String message) {
this.message = message;
}
public String getName() {
return "print message work";
}
public WorkReport call(WorkContext workContext) {
System.out.println(message);
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
}
This unit of work prints a given message to the standard output. Now let's suppose we want to create the following workflow:
- print "foo" three times
- then print "hello" and "world" in parallel
- then if both "hello" and "world" have been successfully printed to the console, print "ok", otherwise print "nok"
This workflow can be illustrated as follows:
-
flow1
is aRepeatFlow
ofwork1
which is printing "foo" three times -
flow2
is aParallelFlow
ofwork2
andwork3
which respectively print "hello" and "world" in parallel -
flow3
is aConditionalFlow
. It first executesflow2
(a workflow is a also a work), then ifflow2
is completed, it executeswork4
(print "ok"), otherwise executeswork5
(print "nok") -
flow4
is aSequentialFlow
. It executesflow1
thenflow3
in sequence.
With Easy Flows, this workflow can be implemented with the following snippet:
PrintMessageWork work1 = new PrintMessageWork("foo");
PrintMessageWork work2 = new PrintMessageWork("hello");
PrintMessageWork work3 = new PrintMessageWork("world");
PrintMessageWork work4 = new PrintMessageWork("ok");
PrintMessageWork work5 = new PrintMessageWork("nok");
ExecutorService executorService = Executors.newFixedThreadPool(2);
WorkFlow workflow = aNewSequentialFlow() // flow 4
.execute(aNewRepeatFlow() // flow 1
.named("print foo 3 times")
.repeat(work1)
.times(3)
.build())
.then(aNewConditionalFlow() // flow 3
.execute(aNewParallelFlow(executorService) // flow 2
.named("print 'hello' and 'world' in parallel")
.execute(work2, work3)
.build())
.when(WorkReportPredicate.COMPLETED)
.then(work4)
.otherwise(work5)
.build())
.build();
WorkFlowEngine workFlowEngine = aNewWorkFlowEngine().build();
WorkContext workContext = new WorkContext();
WorkReport workReport = workFlowEngine.run(workflow, workContext);
executorService.shutdown();
To run this tutorial, please use the following commands:
$ git clone https://github.com/j-easy/easy-flows.git
$ cd easy-flows
$ mvn install
$ mvn -Dtest=org.jeasy.flows.engine.WorkFlowEngineImplTest#defineWorkFlowInlineAndExecuteIt test
You should see the following output:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.jeasy.flows.engine.WorkFlowEngineImplTest
2017-06-30 22:13:11.325 INFO [org.jeasy.flows.engine.WorkFlowEngineImpl run] - Running workflow '024163b7-fd57-44ae-8d20-c533ed448819'
foo
foo
foo
hello
world
done
workflow report = DefaultWorkReport {status=COMPLETED, context={}, error=''}
This is not a very useful workflow, but just to give you an idea about how to write workflows with Easy Flows.
-
Introduction
-
User guide
-
Get involved