Need Help to call a Service Task from Java Class

Hello All,
I am new to Camunda. I have a case study to implement as below -

  1. One JSON string to receive from kafka cluster through kafka consumer API.
  2. Then once a new message is consumed, if the object has JSON value, we have to call a service task of the deployed BPMN process from java service class.

I have springboot code working to consume the message from kafka cluster and the Camunda process is also deployed.
Now I need your help on the steps so that a particular BPMN service task can be called from Java class, so that the service task can be executed instead of initialing/executing the task manually in modeler.

@sudi24pta could you elaborate more on your camunda platform setup?

Is your kafka consumer api and process engine are configured and packed as into one process application or two different services?

@aravindhrs – Kafka and process engine related code are in same services. I am looking for the steps or java code snippet that can be called from consumer service class after the consume of the message and that code should initiate/start one service task of deployed BPMN camunda process.

@sudi24pta do you want to initiate the new process instance itself or you want to invoke some activity like (service task, receive task) for already initiated process?

@aravindhrs - I would like to invoke a service task of a deployed BPMN process from my service class .

Great.

Approach 1:

You can model a process with a Receive task which can be correlated with a message name and then the attached Execution Listener on the receive task can execute your custom logic to process the request.

Execution Listener code:

import org.camunda.bpm.engine.delegate.*;
import org.springframework.stereotype.*;

import lombok.extern.slf4j.*;

@Slf4j
@Component
public class KafkaMessageProcessor implements ExecutionListener {

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		log.info("Processing kafka message for businessKey: {}", execution.getProcessBusinessKey());
		// your implementation
	}
}

Approach 2:

Model a process with intermediate catch event followed by service task. So that you can correlate message as like above from service class and configure a Java Delegate in Service Task to process the request.

Code for java Delegate:

@Slf4j
@Component
public class KafkaMessageProcessorDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) throws Exception {
		log.info("Processing kafka message for businessKey: {}", execution.getProcessBusinessKey());
		// your implementation
	}
}



Invocation from service class will be same for both Intermediate event and Receive Task.

To invoke the Receive task/Intermediate Catch Event from your service class:

@Slf4j
@Service
public class MyServiceImpl implements MyService {

	@Autowired
	private RuntimeService runtimeService;

	public void correlateMessage(String businessKey, String orderId, String name) {

		VariableMap processVariables = Variables.createVariables()
             .putValue("orderId", orderId).putValue("orderName", name);

		runtimeService.correlateMessage("PROCESS_KAFKA_MESSAGE",
				businessKey, processVariables);
	}
}

KafkaMessageProcessor.bpmn (8.5 KB)

1 Like