How to fetch the process instance key in camunda 8, inside a service task in Spring java?

I tried using the “final ActivatedJob job”, but it is giving me this error

java.lang.NullPointerException: Cannot invoke “io.camunda.zeebe.gateway.protocol.GatewayOuterClass$ActivatedJob.getProcessInstanceKey()” because “job” is null

Hey @localhost! :wave:
Welcome to the Camunda Community!

You can get the process instance key in an external task simply by calling a method one the activated job. You can find a snippet blow how to achieve this:

@JobWorker(type = "sendMessage", autoComplete = true)
    public void doSomeWork(final ActivatedJob job, @Variable String businessKey) throws InterruptedException {"message is being send by workflowKey='{}', bpmnProcessId='{}', version='{}' with workflowInstanceKey='{}' and businessKey='{}",
                job.getProcessDefinitionKey(), job.getBpmnProcessId(), job.getProcessDefinitionVersion(), job.getProcessInstanceKey(), businessKey);

        messageCorrelation.correlateMessage(client, businessKey);

Does this help?

Thanks for the response, when I try this the “ActivatedJob job” variable is null. what can be issue here?

Hi @localhost , could you share the How does your worker code looks like? Might be missing some annotation or job worker is not enabled.

Your job worker class should have annotation @Component and also check this property:

    enabled: true  // *true is default value

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.camunda.zeebe.client.api.worker.JobClient;
import io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob;
import io.camunda.zeebe.spring.client.annotation.JobWorker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ButlerInteractions {

    @JobWorker(type = "sendOrderToButler")
    public void sendOrderToButler(final JobClient client, final ActivatedJob job) {"Process instace key: {}", job.getProcessInstanceKey());

@localhost Can you try changing the imports from

import io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob;


import io.camunda.zeebe.client.api.response.ActivatedJob;

It should work.

1 Like

Thank you, that worked silly mistake.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.