Skip to content

tutorial

Mahmoud Ben Hassine edited this page Jun 30, 2017 · 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() {
        System.out.println(message);
        return new DefaultWorkReport(WorkStatus.COMPLETED);
    }
}

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, otherwise work5 which respectively print "ok" and "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");

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)
                            .build())
                .when(WorkReportPredicate.COMPLETED)
                .then(work4)
                .otherwise(work5)
                .build())
        .build();

WorkFlowEngine workFlowEngine = aNewWorkFlowEngine().build();
WorkReport workReport = workFlowEngine.run(workflow);

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, error=''}

This is not a very useful workflow, but just to give you an idea about how to write workflows with Easy Flows.

Clone this wiki locally