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