Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:55
Show Gist options
  • Save fmbenhassine/11300e92b4bd621d6fe6 to your computer and use it in GitHub Desktop.
Save fmbenhassine/11300e92b4bd621d6fe6 to your computer and use it in GitHub Desktop.
Easy (Batch|Rules) inception #EasyBatch #EasyRules
import org.easybatch.core.api.Engine;
import org.easyrules.core.BasicRule;
public class JobRule extends BasicRule {
private ReportHolder reportHolder;
private ReportPredicate reportPredicate;
private Engine engine;
public JobRule(Engine engine, ReportHolder reportHolder, ReportPredicate reportPredicate) {
super(engine.getName());
this.engine = engine;
this.reportHolder = reportHolder;
this.reportPredicate = reportPredicate;
}
@Override
public boolean evaluate() {
return reportPredicate.apply(reportHolder.getReport());
}
@Override
public void execute() throws Exception {
reportHolder.setReport(engine.call());
}
}
import org.easybatch.core.api.Report;
public class ReportHolder {
private Report report;
public Report getReport() {
return report;
}
public void setReport(Report report) {
this.report = report;
}
}
import org.easybatch.core.api.Report;
public interface ReportPredicate {
boolean apply(Report report);
}
INFO: Rule priority threshold: 2,147,483,647
INFO: Skip on first applied rule: false
INFO: Skip on first failed rule: false
INFO: Rule 'engine1ThatFinishesWith100percentSuccess' triggered.
INFO: Initializing the engine
INFO: Engine name: engine1ThatFinishesWith100percentSuccess
INFO: Execution id: bf3267cb-9cad-42cb-8f74-bf62244771d2
INFO: Strict mode: false
INFO: Data source: In-Memory String
INFO: The engine is running
record = [header=[number=1, source="In-Memory String", creationDate="Thu Jun 18 14:16:38 CEST 2015"], payload="foo"]
record = [header=[number=2, source="In-Memory String", creationDate="Thu Jun 18 14:16:38 CEST 2015"], payload="bar"]
INFO: Shutting down the engine
INFO: Rule 'engine1ThatFinishesWith100percentSuccess' performed successfully.
INFO: Rule 'engine2ThatAbortsExecutionOnError' triggered.
INFO: Initializing the engine
INFO: Engine name: engine2ThatAbortsExecutionOnError
INFO: Execution id: 20fa57fa-d11f-43d1-9d30-c6145e894cbc
INFO: Strict mode: true
INFO: Data source: In-Memory String
INFO: The engine is running
SEVERE: An exception occurred while attempting to process record [header=[number=1, source="In-Memory String", creationDate="Thu Jun 18 14:16:38 CEST 2015"], payload="foo"]
org.easybatch.core.api.RecordProcessingException: fatal error!
at test.Test$2.processRecord(Test.java:32)
at test.Test$2.processRecord(Test.java:30)
at org.easybatch.core.impl.ProcessingPipeline.process(ProcessingPipeline.java:66)
at org.easybatch.core.impl.EngineImpl.call(EngineImpl.java:224)
at org.easybatch.core.impl.EngineImpl.call(EngineImpl.java:48)
at test.JobRule.execute(JobRule.java:28)
at org.easyrules.core.DefaultRulesEngine.applyRules(DefaultRulesEngine.java:138)
at org.easyrules.core.DefaultRulesEngine.fireRules(DefaultRulesEngine.java:112)
at test.Test.main(Test.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
INFO: Strict mode enabled: aborting execution
INFO: Shutting down the engine
INFO: Rule 'engine2ThatAbortsExecutionOnError' performed successfully.
INFO: Rule 'engine3ThatIsUsedOnlyForTestAndThatWillNotBeCalled' has been evaluated to false, it has not been executed.
import org.easybatch.core.api.Report;
public class ShouldAlwaysBeExecuted implements ReportPredicate {
public boolean apply(Report report) {
return true;
}
}
import org.easybatch.core.api.Report;
import org.easybatch.core.api.Status;
public class ShouldBeExecutedOnlyIfPreviousJobHasFinishedWith100PercentSuccess implements ReportPredicate {
public boolean apply(Report report) {
return Status.FINISHED.equals(report.getStatus()) && report.getSuccessRecordsCount() == report.getTotalRecords();
}
}
import org.easybatch.core.api.Report;
import org.easybatch.core.api.Status;
public class ShouldNotBeExecutedIfPreviousHasBeenAborted implements ReportPredicate {
public boolean apply(Report report) {
return !Status.ABORTED.equals(report.getStatus());
}
}
import org.easybatch.core.api.Engine;
import org.easybatch.core.api.RecordProcessingException;
import org.easybatch.core.api.RecordProcessor;
import org.easybatch.core.impl.EngineBuilder;
import org.easybatch.core.reader.StringRecordReader;
import org.easybatch.core.record.StringRecord;
import org.easyrules.api.RulesEngine;
import org.easyrules.core.RulesEngineBuilder;
public class Test {
public static void main(String[] args) {
Engine engine1 = EngineBuilder.aNewEngine()
.named("engine1ThatFinishesWith100percentSuccess")
.reader(new StringRecordReader("foo\nbar"))
.processor(new RecordProcessor<StringRecord, StringRecord>() {
public StringRecord processRecord(StringRecord record) throws RecordProcessingException {
System.out.println("record = " + record);
return record;
}
})
.build();
Engine engine2 = EngineBuilder.aNewEngine()
.named("engine2ThatAbortsExecutionOnError")
.reader(new StringRecordReader("foo\nbar"))
.processor(new RecordProcessor<StringRecord, StringRecord>() {
public StringRecord processRecord(StringRecord record) throws RecordProcessingException {
throw new RecordProcessingException("fatal error!");
}
})
.strictMode(true)
.build();
Engine engine3 = EngineBuilder.aNewEngine()
.named("engine3ThatIsUsedOnlyForTestAndThatWillNotBeCalled")
.reader(new StringRecordReader("foo"))
.processor(new RecordProcessor<StringRecord, StringRecord>() {
public StringRecord processRecord(StringRecord record) throws RecordProcessingException {
System.out.println("engine3 has been called");
return record;
}
})
.build();
ReportHolder reportHolder = new ReportHolder();
RulesEngine rulesEngine = RulesEngineBuilder.aNewRulesEngine().build();
JobRule firstJobRule = new JobRule(engine1, reportHolder, new ShouldAlwaysBeExecuted());
JobRule secondJobRule = new JobRule(engine2, reportHolder, new ShouldBeExecutedOnlyIfPreviousJobHasFinishedWith100PercentSuccess());
JobRule thirdJobRule = new JobRule(engine3, reportHolder, new ShouldNotBeExecutedIfPreviousHasBeenAborted());
rulesEngine.registerRule(firstJobRule);
rulesEngine.registerRule(secondJobRule);
rulesEngine.registerRule(thirdJobRule);
rulesEngine.fireRules();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment