API orchestration tool

I am trying to create an API orchestration tool on camunda. That is, the first API is called, and based on the result of the API, a decision is made between calling 2 different API’s. I’m trying to implement this using the Delegation method, but am having issues understanding exactly how to get the result of the first API, define the condition and call the next API. The API’s are simple webservices that return a JSON object. Any help would be appreciated!

Task 1: Call API, Save result to a Process Variable (or use Transitive variables: https://docs.camunda.org/manual/7.9/user-guide/process-engine/variables/#transient-variables).

Task 2 or Gateway Expression: Access the variable (execution.getVariable() ), eval the value of the variable, and preform your next API call, and repeat the steps from before.

When you save your variable you can use the SPIN library (lookup Camunda SPIN) to save your variables as JSON objects.

Thank you for that information. I have a couple of questions to that though.

This is the java code I came up with to call API1.

package org.camunda.bpm.orch;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import org.camunda.bpm.client.ExternalTaskClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.net.;
import java.io.
;
import java.util.*;
import javax.inject.Named;

@Named(“api1call”)
public class orchDelegate implements JavaDelegate{

public void execute(DelegateExecution execution) throws Exception {
	try(InputStream is=new URL("https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint16").openStream()) {
	 	BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
	 	StringBuilder sb=new StringBuilder();
	 	int cp;
	 	while((cp=rd.read())!=-1) {
	 		sb.append((char)cp);
	 	}
	 	String jsonText = sb.toString();
     	JSONObject json = new JSONObject();
     	json.put("jsonText", jsonText);
     	System.out.println(json.toString());
     	Object retVal=json.get(2);
        System.out.println(retVal);
        is.close();
		}
	catch(Exception e){
		e.printStackTrace();
	}
	
}

}

I’m basically calling the API in this code, and have 2 more classes orchDelegate2 and orchDelegate3 to call API 2 and API3 in similar manners.

  1. I’m a little confused about where exactly I access the return value of API1 and where I apply the conditional logic? Can I basically do this on the camunda modeler, or do I need additional code for it in my java program?

  2. I also didn’t really get any resources for actually communicating variables between the program and the modeler. I need to basically be able to input and output stuff on the cockpit, and I’m not sure how to.

Thanks for any help once again!

Take a look at:

https://docs.camunda.org/manual/7.8/user-guide/process-engine/delegation-code/

https://docs.camunda.org/manual/7.8/user-guide/process-engine/expression-language/#conditions

and

https://docs.camunda.org/manual/7.8/user-guide/process-engine/variables/

Each task is executing some java delegate. That java delegate you are doing a few possible things:

  1. Call API
  2. with the Response from the API Save a Process Variable (execution.setVariable)
  3. Get a process variable from a previous API call (execution.getVariable).

When you are using a Gateway for your logic, you can do the same thing or use a expression to access the variable and eval the value. See the links above.

So I added a variable ret as an output of the first API and the input of the first gateway. In the first API call code, I set this variable with the response the API returns.

In the modeler, I added the conditions to the gateway that ${ret<3000} and ${ret>3000}

However, I am unsure of where to add a main function and its purpose to my java code in order to be able to run it. And is it okay that I have 3 different files that are only connected to camunda through the @Named section or is there something else I need to do?

Sorry about the questions, but its only been a week since I’ve been introduced to camunda and I dont have too many resources to look to.

Take a look at this walkthrough

It uses HTTP connector and scripting, but the logic is the same. Once you work through this example and understand the flow, you should be able to adapt your Java code to the same thing

1 Like

I followed this example, and basically nixed the java code I had and added js scripts to call the API. Now, when I deploy the model, it says it was deployed correctly, but I don’t see it in the cockpit. I tried deploying other bpmn diagrams and they show up fine.

orch1.bpmn (10.9 KB)

This is my current file, and I’m really not sure where Im going wrong.

check your camunda command prompt for any errors after the deployment was done. also check whether the executable flag is enabled.

1 Like