2
votes

My microservice has a Rest endpoint (getLocationForCar()) where it accepts a Car DTO (as below) as it's input , has some business logic to find the car at a location and returns the Location DTO (as below).

class Car {
   String carId;
   String carName;
   String carType;
   String carModel;
   String carMake;
}
class Location {
   String locationId;
   String locationType;
   String locationAddress;
}

I want to move the business logic to BPMN and DMN. I am new to BPMN and DMN. I went through few tutorials of Camunda and thought this is how I could get this working with Camunda:

  1. Create a hardcoded DMN table with input = carId and output = locationId.
  2. Create a BPMN diagram which has a
    • start ->
    • extract carId from the Car DTO that is passed to the Rest webservice (getLocationForCar()) ->
    • call DMN to give the carId to it ->
    • send DMN's output to the java process so it can be sent as a webservice (getLocationForCar()) response

This approach has issues (I need help getting the following questions answered):

  1. How to call a BPMN process from the java Rest Webservice code?
  2. How to pass Car Object to BPMN so that it can extract the carId, use it in DMN table and geturn the output?
  3. How the java code will get output from BPMN process or DMN table, that can be used to return the required response object

Using Camunda (third part library) seems like a overhead because Camunda runs on it's own server and bpmn, dmn are deployed on that, this would slow down my process. So I am leaning more towards JBPM (although i have no idea if i can achieve my requirement using any of these).

1
Camunda can run embedded within your existing java web app, or as you mentioned on a sperate server that would be invoked from your web app. (Where Camunda lives is its own architectural decision.) The Camunda docs are excellent, and there are a lot of examples around. Please update your question with some code that shows what you have tried but isn't working.Andrew S
Another question - Is it possible to embed jBPM or Camunda (free version) seamlessly into a microservice using OSGi, docker?Tiya
Definite yes for docker (there are examples), but I'm not familiar with OSGi.Andrew S
Can you share any link to an example that will help me learn?Tiya

1 Answers

2
votes

DMN is a good way to extract your business logic. Imho Camunda is the best lightweigt and free possibility to do that.

Here an example for you

  1. Just use this two dependencies:

org.camunda.bpm.dmn:camunda-engine-dmn
org.camunda.bpm.dmn:camunda-engine-dmn-bom

  1. Create DmnEngine:
DmnEngine dmnEngine = DmnEngineConfiguration
.createDefaultDmnEngineConfiguration()
.buildEngine();
  1. Create DMN File with Camunda Modeler (is free).
  2. Prepare variables for decision evaluation:
 VariableMap variables = Variables
      .putValue("carId", carId)
      .putValue("carName", carName);
      .putValue("carType", carType);
      .putValue("carModel", carModel);
      .putValue("carMake", carMake);
  1. Parse decision from step 3 (copy XML code from Camunda Modeler and paste into a new xml file in your project)
 InputStream inputStream = CarDecider.class.getResourceAsStream("carDecisionFile.xml");
  1. Evaluate decision
try {
      DmnDecision decision = dmnEngine.parseDecision("decision", inputStream);

      // evaluate decision
      DmnDecisionTableResult result = dmnEngine.evaluateDecisionTable(decision, variables);

      // print result
      String desiredLocation = result.getSingleResult().getSingleEntry();
      System.out.println("Decision: " + desiredLocation);

    }
    finally {
      try {
        inputStream.close();
      }
      catch (IOException e) {
        System.err.println("Could not close stream: "+e.getMessage());
      }
    }
  }

If you want to use more complex DMN diagrams, you can use Decision Requirements Graph (see: https://docs.camunda.org/manual/7.6/reference/dmn11/drg/). With Camunda you can have multiple output values, like you need it in your example. Moreover, you can use Extensions for Camunda, like Feel Scala, with that you can use functions in your DMN files. Furthermore you can write your own custom functions for DMN. With this approach you don't need to use Camunda Platform. Just with 2 dependencies you can move your logic to those DMN files. With Camunda Modeler you can upload dmn files: for example you can create a microservice that receives those files and saves them in a database. Your DmnEngine micro service will load that files and evaluate decision.