-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathTripBookingSaga.java
74 lines (61 loc) · 3.25 KB
/
TripBookingSaga.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package io.flowing.trip.saga.camunda.simple;
import java.io.File;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.builder.ProcessBuilder;
import io.flowing.trip.saga.camunda.adapter.BookFlightAdapter;
import io.flowing.trip.saga.camunda.adapter.BookHotelAdapter;
import io.flowing.trip.saga.camunda.adapter.CancelCarAdapter;
import io.flowing.trip.saga.camunda.adapter.CancelFlightAdapter;
import io.flowing.trip.saga.camunda.adapter.CancelHotelAdapter;
import io.flowing.trip.saga.camunda.adapter.ReserveCarAdapter;
/**
* One main class containing everything to run a Saga
* using Camunda and BPMN. The class runs an in memory engine,
* defines the Saga and run a couple of instances.
*/
public class TripBookingSaga {
public static void main(String[] args) {
// Configure and startup (in memory) engine
ProcessEngine camunda =
new StandaloneInMemProcessEngineConfiguration()
.buildProcessEngine();
BpmnModelInstance saga = createSaga();
// finish Saga and deploy it to Camunda
camunda.getRepositoryService().createDeployment() //
.addModelInstance("trip.bpmn", saga) //
.deploy();
// now we can start running instances of our saga - its state will be persisted
camunda.getRuntimeService().startProcessInstanceByKey("trip", Variables.putValue("name", "trip1"));
camunda.getRuntimeService().startProcessInstanceByKey("trip", Variables.putValue("name", "trip2"));
}
public static BpmnModelInstance createSaga() {
// define saga as BPMN process
ProcessBuilder flow = Bpmn.createExecutableProcess("trip");
// - flow of activities and compensating actions
flow.startEvent()
.serviceTask("car").name("Reserve car").camundaClass(ReserveCarAdapter.class)
.boundaryEvent().compensateEventDefinition().compensateEventDefinitionDone()
.compensationStart().serviceTask("CancelCar").camundaClass(CancelCarAdapter.class).compensationDone()
.serviceTask("hotel").name("Book hotel").camundaClass(BookHotelAdapter.class)
.boundaryEvent().compensateEventDefinition().compensateEventDefinitionDone()
.compensationStart().serviceTask("CancelHotel").camundaClass(CancelHotelAdapter.class).compensationDone()
.serviceTask("flight").name("Book flight").camundaClass(BookFlightAdapter.class)
.boundaryEvent().compensateEventDefinition().compensateEventDefinitionDone()
.compensationStart().serviceTask("CancelFlight").camundaClass(CancelFlightAdapter.class).compensationDone()
.endEvent();
// - trigger compensation in case of any exception (other triggers are possible)
flow.eventSubProcess()
.startEvent().error("java.lang.Throwable")
.intermediateThrowEvent().compensateEventDefinition().compensateEventDefinitionDone()
.endEvent();
// ready
BpmnModelInstance saga = flow.done();
// optional: Write to file to be able to open it in Camunda Modeler
//Bpmn.writeModelToFile(new File("trip.bpmn"), saga);
return saga;
}
}