Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:39
Show Gist options
  • Save fmbenhassine/4d55e8b18f2ba05ab94a14eda98bd6e9 to your computer and use it in GitHub Desktop.
Save fmbenhassine/4d55e8b18f2ba05ab94a14eda98bd6e9 to your computer and use it in GitHub Desktop.
Example of how to create an Easy Flows workflow with execution data passed between steps #EasyFlows
package org.jeasy.flows.work;
public abstract class ContextAwareWork implements Work {
private String name;
protected ExecutionContext executionContext;
public ContextAwareWork(String name, ExecutionContext executionContext) {
this.name = name;
this.executionContext = executionContext;
}
@Override
public String getName() {
return name;
}
@Override
public abstract WorkReport call();
}
package org.jeasy.flows.work;
import java.util.HashMap;
import java.util.Map;
public class ExecutionContext {
private Map<String, Object> context = new HashMap<>();
public void put(String key, Object value) {
context.put(key, value);
}
public Object get(String key) {
return context.get(key);
}
}
package org.jeasy.flows.work;
public class Step1 extends ContextAwareWork {
public Step1(String name, ExecutionContext executionContext) {
super(name, executionContext);
}
@Override
public WorkReport call() {
// just an example: count files in home directory
System.out.println(getName() + ": counting files");
executionContext.put("totalFiles", 500);
return new DefaultWorkReport(WorkStatus.COMPLETED);
}
}
package org.jeasy.flows.work;
public class Step2 extends ContextAwareWork {
public Step2(String name, ExecutionContext executionContext) {
super(name, executionContext);
}
@Override
public WorkReport call() {
int totalFiles = (int) executionContext.get("totalFiles");
System.out.println(getName() + ": totalFiles = " + totalFiles);
return new DefaultWorkReport(WorkStatus.COMPLETED);
}
}
package org.jeasy.flows.work;
import org.jeasy.flows.engine.WorkFlowEngine;
import org.jeasy.flows.engine.WorkFlowEngineBuilder;
import org.jeasy.flows.workflow.SequentialFlow;
import org.jeasy.flows.workflow.WorkFlow;
public class TestContextAwareWorkflow {
public static void main(String[] args) {
ExecutionContext executionContext = new ExecutionContext();
Work step1 = new Step1("step1", executionContext);
Work step2 = new Step2("step2", executionContext);
WorkFlow workFlow = SequentialFlow.Builder.aNewSequentialFlow()
.execute(step1)
.then(step2)
.build();
WorkFlowEngine workFlowEngine = WorkFlowEngineBuilder.aNewWorkFlowEngine().build();
workFlowEngine.run(workFlow);
// prints
// step1: counting files
// step2: totalFiles = 500
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment