Start BPMN process with java

Hi

I am trying to start my process “MyProcess” with the following code. But, I want to add a uniqie BusinesKey to the call. Does anyone hva examples on how to start a process and add both business key and other variables relevant for the process?

@KafkaListener(topics = “${message.topic.name}”)
public void listen(String message) {
logger.info(“LOGGER MESSAGE”);
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService=processEngine.getRuntimeService();
ProcessInstance instance = runtimeService.createProcessInstanceByKey(“MyProcess”).setVariable(“testvariable”,message).execute();
}

I think you’re on the wrong foot with the createProcessInstanceByKey method. You can do what you need using a different method on the RuntimeService:

runtimeService.startProcessInstanceByKey("MyProcess", "myBusinessKey", variables);

That worked well. :slight_smile:
But…in my first task, i want to send the variables in “variables” out to with a REST client. But I am not able to get the variables to send them.

Here is my code in the RestProducer class that implements JavaDelegate and is wired in the BPMN task. I am able to get the businesskey, etc, but not the values i recieve in the first question:

@Override
public void execute(DelegateExecution execution) throws Exception {
String businessKey = execution.getProcessBusinessKey();
String eventNameIdString = execution.getCurrentActivityId();
String fnr = (String) execution.getVariables().toString();
//String fnr = (String) execution.getVariable(“testvariable”);
logger.info(“Businesskey and eventnameid and fnr: “+businessKey+”----”+ eventNameIdString +"----"+fnr);

    //Map<String, Object> variables = execution.getVariables();
    //String fnr = Integer.parseInt(variables.get("orderID").toString());
    //sendRestMessage(fnr);
}

I add the variable like this:

Map<String, Object> variables = new HashMap<String, Object>();

    //variables.put("traceId", traceId);
    variables.put("testvariable", message);


    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService=processEngine.getRuntimeService();
    ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, businessKey, variables);
1 Like

That should get you the variable with name “testvariable” from the DelegateExecution, but is is commented at the moment.

So, i dont have to add anything in the task in the BPMN diagram?

Variables you pass in as part of starting the process, are available when executing a service or send task within the process, so there’s no extra action needed to have them available there.

1 Like

Many thanks! Very grateful for your answer. Will test out here now. :slight_smile:

HTH :slight_smile:

This worked well. The error I got, was because I had made variables with the same name in the BPMN file. When I erased them, I got the values. Nice!

Do you know where I can fin info about reliable transactions in camunda? In the last rest call, I want Camunda to, in case of exception runtime/business, caunda to be able to resend the request later on. Either automatic og by a sort of “resume” command.

I’d say this is the primary resource for information on that: https://docs.camunda.org/manual/7.10/user-guide/process-engine/transactions-in-processes/

As a quick indication: any RuntimeExceptions that bubble up to the process from the delegate will result in a rollback and a retry by Camunda, according to the retry policy, which is also configurable. That will be attempted until the retries for the job (assuming the task is asynchronous, which is good practice) are exhausted.

If you want functional handling of exceptions, model them explicitely in your process diagram as e.g. error boundary events and throw a BpmnError from the delegate with the matching error code to trigger an alternative path instead of retrying.

I think that runtime error (technical errors) can ble rerun as they are after some time. When the network error is fixed og the server we are trying to run is up and running again. Is there a way that one can just trigger a retry after ex 24 hours?

Business errors will have to go to manual task flow.

Yes if you configure a custom retry policy you could set it to 24 hours if you need to PT24H would be the expression for that.

Is there a way I can resubmit errors which now is marked red in cockpit? I just turned of a REST app so all calls fail towards that server (via javadelegate).

Does the camunda REST API cover this?

I think you’re looking for resolving an incident, which is what the “red” items in Cockpit are. The API docs for that are here:

https://docs.camunda.org/manual/7.10/reference/rest/incident/resolve-incident/

Hi

I will first thank you for taking your time to answer my questions.

I am not sure on how to solve the issue I mentioned above, where a task is gone into incident status. In my case, this is because the task is implemented with a JavaDelegate class which tries to connect to a REST server. The server is down and the task marks its step as an incident and the process stops there.

I am trying to test how I can “rerun” the task and make the whole process continue. Now it hangs in the task that failed.

What are the preferred way to do this?

Br

Frank

If you resolve the incident, that will signal Camunda to retry from the point it was stuck, being before the call to the REST service, so it will re-attempt that.

To prevent the need for a lot of manual incident resolutions, you can use the retry policy to instruct Camunda to automatically retry the task at the intervals that make sense for your environment or even for the specific task if the service you are calling has abnormal uptime characteristics you need to account for.