How to create workflow instance in spring for zeebe

How can i create an instance of workflow in springboot project for the workflow being deployed in zeebe cluster.

@Tauqeer you can use spring Zeebe for that… like for example here: https://github.com/salaboy/fmtok8s-c4p/blob/master/src/main/java/com/salaboy/conferences/c4p/C4PController.java#L55

This is my code

package com.spring.task;

import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.spring.task.utilities.Utilities;

import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.response.WorkflowInstanceEvent;
import io.zeebe.client.api.worker.JobClient;
import io.zeebe.spring.client.EnableZeebeClient;
import io.zeebe.spring.client.ZeebeClientLifecycle;
import io.zeebe.spring.client.annotation.ZeebeDeployment;
import io.zeebe.spring.client.annotation.ZeebeWorker;
import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableZeebeClient
@ZeebeDeployment(classPathResource = “ZeebeCheckBalance2.bpmn”)
@Slf4j
@EnableScheduling
public class SpringZeebeApplication {

final static Logger logger = LoggerFactory.getLogger(SpringZeebeApplication.class);

@Autowired
private static ZeebeClientLifecycle client;

@Autowired
Utilities utilities;

Random random = new Random();

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

public static void main(String[] args) {
	SpringApplication.run(SpringZeebeApplication.class, args);
}

private static void logJob(final ActivatedJob job) {

	logger.info(
			"complete job\n>>> [type: {}, key: {}, element: {}, workflow instance: {}]\n{deadline; {}]\n[headers: {}]\n[variables: {}]",
			job.getType(), job.getKey(), job.getElementId(), job.getWorkflowInstanceKey(),
			Instant.ofEpochMilli(job.getDeadline()), job.getCustomHeaders(), job.getVariables());
}

@ZeebeWorker(type = "foo")
public void foo(final JobClient client, final ActivatedJob job) {
	logJob(job);
	client.newCompleteCommand(job.getKey()).variables("{\"fooResult\": 1}").send().join();
}

@ZeebeWorker(type = "bar")
public void bar(final JobClient client, final ActivatedJob job) {
	logJob(job);
	client.newCompleteCommand(job.getKey()).send().join();
}


@Scheduled(fixedRate = 5000L)
public static void startProcesses() {
	if (!client.isRunning()) {
		return;
	}

	final WorkflowInstanceEvent event = client.newCreateInstanceCommand().bpmnProcessId("orderManagement")
			.latestVersion().variables("{\"a\": \"" + UUID.randomUUID().toString() + "\"}").send().join();

	logger.info(
			"started instance for workflowKey='{}', bpmnProcessId='{}', version='{}' with workflowInstanceKey='{}'",
			event.getWorkflowKey(), event.getBpmnProcessId(), event.getVersion(), event.getWorkflowInstanceKey());
}

@ZeebeWorker(type = "taskA")
public void taskA(final JobClient client, final ActivatedJob job) {
	//final Map<String, Object> variables =job.getVariablesAsMap();
	logJob(job);
	logger.info("task A has been completed ");
	final Map<String, Object> result = new HashMap<>();
	result.put("condition", random.nextBoolean());
	client.newCompleteCommand(job.getKey()).send().join();
}

}
and when i run this im getting null pointer exception
2020-05-22 12:53:15.227 ERROR 21172 — [ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task

java.lang.NullPointerException: null
at com.spring.task.SpringZeebeApplication.startProcesses(SpringZeebeApplication.java:172)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

What is it in line 172? -> SpringZeebeApplication.java:172 -> java.lang.NullPointerException: null
Debug the source code and see what is null. It sounds like something is not correctly initialized

@Autowired
private static ZeebeClientLifecycle client;

this is causing the problem, its not getting initialized.

@Tauqeer can you share your project in github? to reproduce that issue?
Where your Zeebe Cluster running? are you correctly configuring the client?

Hi @salaboy,
Thank you for your help and i have solved the issue.

@Autowired
private static ZeebeClientLifecycle client;

removed static key word and it worked fine.
But i have one more query, For message based events how to send message to zeebe engine.

@Tauqeer the answer to that is definitely in the docs. But you can see an example here: https://github.com/salaboy/fmtok8s-c4p/blob/master/src/main/java/com/salaboy/conferences/c4p/C4PController.java#L100

2 Likes

@salaboy Thank you so much for the help and guidance.

1 Like

@Tauqeer can you mark this question as solved?

The POM for io.Zeebe:zeebe-client-java:jar:0.18.0-SNAPSHOT is missing, no dependency statistics available" – I Actually did the same, but getting this error. I used vs code in ubuntu as ide

Any reason why you are using 0.18.0? The latest version is 0.23.0.

If you need to use 0.18.0 and it is not available, please check the issues here (including closed issues to see if there is a workaround in there), and open one if there isn’t one for it already.

1 Like

Hi @salaboy,
Hope you are doing well, @salaboy how can i get the details of all the activities and task details in a workflow and even the timers details when i’m creating an instance of workflow.

@Tauqeer you can’t from the APIs exposed by Zeebe, you need to use an exporter to get that information.
Operate uses the ElasticSearch Exporter to get the data out and then show the state of the workflow instances. You can check ZeeQS and Simple Monitor to get that information from those data sources.


@salaboy, Thank you so much for your help, I have one question related to publish message command .
PublishMessageResponse publishMessageResponse = client.newPublishMessageCommand().messageName(messageName)
.correlationKey(correlationKey).send().join();

what does publishMessageResponse hold ? and how will i get to know that it was correlated.
I need to know wheter it was correlated or not because i need to send the status[failure or success]

@Tauqeer you don’t get that information from publishing a message. There is no failure from publishing a message, as I can or cannot be correlated based on the correlation key.
What you can do is to add in your process a notification mechanism to report back. For example, the first Service Task in your process can push data to a component that is in charge of keeping track which workflows were started.

Hope this helps.