Skip to content

tutorial

Mahmoud Ben Hassine edited this page Oct 8, 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 execute(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:

  1. print "foo" three times
  2. then print "hello" and "world" in parallel
  3. 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 a RepeatFlow of work1 which is printing "foo" three times
  • flow2 is a ParallelFlow of work2 and work3 which respectively print "hello" and "world" in parallel
  • flow3 is a ConditionalFlow. It first executes flow2 (a workflow is a also a work), then if flow2 is completed, it executes work4 (print "ok"), otherwise executes work5 (print "nok")
  • flow4 is a SequentialFlow. It executes flow1 then flow3 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() // flow 2
                            .named("print 'hello' and 'world' in parallel")
                            .execute(work2, work3)
                            .with(executorService)
                            .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.