Calling service layer instead of Camunda Rest API?

If we embed the Camunda engine in a spring-boot service, then is it possible/suggested to directly call the Java methods instead of coming through REST API?

Consider the case if we want to wrap some of the methods and do not want a separate service and come through REST API, instead, we can directly call the Camunda java method.

If that is possible, is there any documentation/tutorial?

Hello @Himanshu_Singh ,

if you are working with an embedded engine using spring-boot (with the camunda spring boot starter), you can use @Autowired to inject the ProcessEngine interface in your bean.

There, you will have access to some services as described here:

https://docs.camunda.org/manual/7.15/user-guide/process-engine/process-engine-api/#services-api

Hope this helps

Jonathan

1 Like

@Himanshu_Singh You can find examples here:

@Service
public class CamundaApiService {

	@Autowired
	private IdentityService identityService;

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private TaskService taskService;

	@Autowired
	private FilterService filterService;

	@Autowired
	private ManagementService managementService;

	@Autowired
	private RuntimeService runtimeService;

	public void getFailedJobs(DelegateExecution execution) {

		// Only select jobs that failed due to an exception at an activity with the
		// given id.
		long jobCount = execution.getProcessEngineServices().getManagementService().createJobQuery()
				.failedActivityId("failedActivityId").count();

		// Only select incidents which were created due to a failure at an activity with
		// the given id.
		long incidentCount = execution.getProcessEngineServices().getRuntimeService().createIncidentQuery()
				.failedActivityId("failedActivityId").count();
		
		DbEntityManager dbEntityManager = Context.getCommandContext().getDbEntityManager();
		dbEntityManager.createJobQuery().processDefinitionKey("processDefinitionKey").active().count();
	}

	public void createUnassignedTaskFilter(String processDefinitionKey) {
		TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskUnassigned();
		Filter newFilter = new FilterEntity(ResourceTypes.RUNTIME.getName());
		newFilter = newFilter.setName("unassigneTaskFilter").setQuery(taskQuery);
		Filter savedFilter = filterService.saveFilter(newFilter);
		log.info("Unassigned task filter created: {}", savedFilter.getName());
	}

	public void completeTask(String taskId, Map<String, Object> processVariables) {
		taskService.complete(taskId, processVariables);

	}

	public void deleteProcessDefinitionById(String processDefinitionId) {
		repositoryService.deleteProcessDefinition(processDefinitionId, true, true, true);
	}

	public void deleteProcessDefinitionByKey(String processDefinitionKey, Optional<Integer> version) {
		ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
				.processDefinitionKey(processDefinitionKey);
		processDefinitionQuery = version.isPresent() ? processDefinitionQuery.processDefinitionVersion(version.get())
				: processDefinitionQuery.latestVersion();
		ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
		repositoryService.deleteProcessDefinition(processDefinition.getId(), true, true, true);
	}

	public void correlateMessage(String businessKey, Object kafkaMessagePayload) {
		VariableMap processVariables = Variables.createVariables().putValue("orderId", 271381983).putValue("name",
				"notebook");
		runtimeService.correlateMessage("PROCESS_KAFKA_MESSAGE",
				businessKey, processVariables);
	}

	// parse bpmn process model and extract taskDefinitionKeys to query active tasks
	// for a swimlane
	public void getTaskItemsForLanes(String processDefinitionKey, String[] taskDefinitionKeys) {
		List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey)
				.taskDefinitionKeyIn(taskDefinitionKeys).active().list();
		taskList.forEach(task -> task.setAssignee("adminUser"));
	}

	public String getLoggedInUserId() {
		return identityService.getCurrentAuthentication().getUserId();
	}

	public UserDto getUserDetails() {
		User user = identityService.createUserQuery().userId(getLoggedInUserId()).singleResult();
		log.info("Logged in userid: {}, first name: {}", user.getId(), user.getFirstName());
		return UserDto.fromUser(user, false);
	}

}

2 Likes