Skip to content

Instantly share code, notes, and snippets.

View berndruecker's full-sized avatar

Bernd Ruecker berndruecker

View GitHub Profile
SagaBuilder saga = SagaBuilder.newSaga("trip")
.activity("Reserve car", ReserveCarAdapter.class)
.compensationActivity("Cancel car", CancelCarAdapter.class)
.activity("Book hotel", BookHotelAdapter.class)
.compensationActivity("Cancel hotel", CancelHotelAdapter.class)
.activity("Book flight", BookFlightAdapter.class)
.compensationActivity("Cancel flight", CancelFlightAdapter.class)
.end()
.triggerCompensationOnAnyError();
@Configuration
public class CamundaEngineHistoryConfiguration extends AbstractCamundaConfiguration implements CamundaHistoryLevelAutoHandlingConfiguration {
@Override
public void preInit(SpringProcessEngineConfiguration configuration) {
configuration.setHistory(ProcessEngineConfiguration.HISTORY_FULL);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE, //
classes = TestApplication.class, //
properties = { //
"camunda.bpm.job-execution.enabled=false", //
"camunda.bpm.auto-deployment-enabled=false"})
@Deployment(resources = {"..."})
public class SomeScenarioTest {
//...
var camunda = new CamundaEngineClient("http://localhost:8080/engine-rest/engine/default/", null, null);
// Deploy the BPMN XML file from the resources
camunda.RepositoryService.Deploy("trip-booking", new List<object> {
FileParameter.FromManifestResource(Assembly.GetExecutingAssembly(), "FlowingTripBookingSaga.Models.FlowingTripBookingSaga.bpmn")
});
// Register workers
registerWorker("reserve-car", externalTask => {
// here you can do the real thing! Like a sysout :-)
public class Payment implements EventObserver {
public void eventReceived(Event event) {
if (event.is("OrderPlaced")) {
// skip the real work
Bus.send(new Event("PaymentReceived", payload));
}
}
}
public class Inventory implements EventObserver {
public void eventReceived(Event event) {
public class Payment implements EventObserver {
public void eventReceived(Event event) {
if (event.is("OrderPlaced") && !(Boolean)event.getPayload().get("vip")) {
// skip the real work
Bus.send(new Event("PaymentReceived", payload));
}
}
}
public class Inventory implements EventObserver {
public void eventReceived(Event event) {
public class Order implements EventObserver {
public void eventReceived(Event event) {
if (event.is("OrderPlaced")) {
Bus.send(new Event("RetrievePaymentCommand", event.getPayload()));
}
if (event.is("PaymentReceived")) {
Bus.send(new Event("FetchGoodsCommand", event.getPayload()));
}
if (event.is("GoodsFetched")) {
Bus.send(new Event("ShipGoodsCommand", event.getPayload()));
public class OrderCamunda implements EventObserver {
private static ProcessEngine camunda;
public static void init() {
// Configure Camunda engine (in this case using in memory H2)
StandaloneInMemProcessEngineConfiguration conf = new StandaloneInMemProcessEngineConfiguration();
conf.setJobExecutorActivate(true);
camunda = conf.buildProcessEngine();
// Define flow
BpmnModelInstance flow = simpleFlowOfActivities();
private static BpmnModelInstance extendedFlowOfActivities() {
ProcessBuilder flow = Bpmn.createExecutableProcess("order");
flow.startEvent()
.exclusiveGateway("split").condition("normal folks", "#{not vip}") //
.serviceTask().name("Retrieve payment").camundaClass(RetrievePaymentAdapter.class) //
.boundaryEvent().compensateEventDefinition().compensateEventDefinitionDone() //
.compensationStart() //
.serviceTask().name("refund payment").camundaClass(RefundPaymentAdapter.class) //
.compensationDone() //
var Workers = require('camunda-worker-node');
var workers = Workers('http://localhost:8080/engine-rest', {
workerId: 'some-worker-id'
});
workers.registerWorker('reserve-car', [ 'someData' ], function(context, callback) {
var someNewData = context.variables.someData + " - added something";
callback(null, {
variables: {
someNewData: someNewData