Accessing the retry attempt for a failedJobRetryTimeCycle config during execution

Hi,

I need to have some custom logic based on the number of retries completed for my service task.
I have setup my retries using this configuration

<bpmn:extensionElements>
      <camunda:failedJobRetryTimeCycle>R5/PT1M</camunda:failedJobRetryTimeCycle>
    </bpmn:extensionElements>

I am currently using camunda version 7.6

Now, in my service task I need to be able access which retry attempt is in progress. Can someone help me understand how to find if nth attempt is in progress?

Hello my friend! Welcome to community \o :smiley:

This script that I’m sending below can be used in an execution listner, or in a script task, or even directly in your project’s code (with a few modifications):

var retriesNumber = execution.getProcessEngineServices().getManagementService()
         .createJobQuery()
         .activityId(execution.getCurrentActivityId())
         .singleResult()
        .getRetries();

Although you can execute the necessary logic, I believe you will not be able to “print” the amount of retry in a process variable, for example, because this functionality has been buggy since 2018, I think from what I’ve heard.

In this post below, in the last comment, I talked a little about this subject.

But if you just need to run some logic like you mentioned it will work!

I hope this helps!

William Robert Alves

Thanks for the quick reply william. I ran with the above but there is a exception

org.camunda.bpm.engine.ProcessEngineException: Query return 2 results instead of max 1
	at org.camunda.bpm.engine.impl.AbstractQuery.executeSingleResult(AbstractQuery.java:211)
	at org.camunda.bpm.engine.impl.AbstractQuery.execute(AbstractQuery.java:167)
	at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:104)
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66)
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30)
	at org.camunda.bpm.engine.impl.AbstractQuery.singleResult(AbstractQuery.java:130)
	at com.linkedin.perseus.workflows.example.CallActivityOne.execute(CallActivityOne.java:34)
	at org.camunda.bpm.engine.impl.bpmn.delegate.JavaDelegateInvocation.invoke(JavaDelegateInvocation.java:36)
	at org.camunda.bpm.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:54)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocationInContext(DefaultDelegateInterceptor.java:87)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:59)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.execute(ServiceTaskJavaDelegateActivityBehavior.java:50)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.performExecution(ServiceTaskJavaDelegateActivityBehavior.java:41)
	at org.camunda.bpm.engine.impl.bpmn.behavior.TaskActivityBehavior.execute(TaskActivityBehavior.java:65)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior$1.call(ClassDelegateActivityBehavior.java:65)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior$1.call(ClassDelegateActivityBehavior.java:62)
	at org.camunda.bpm.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior.executeWithErrorPropagation(AbstractBpmnActivityBehavior.java:108)

Thanks for the reply.
I ran with this and see the following error :

org.camunda.bpm.engine.ProcessEngineException: Query return 2 results instead of max 1
	at org.camunda.bpm.engine.impl.AbstractQuery.executeSingleResult(AbstractQuery.java:211)
	at org.camunda.bpm.engine.impl.AbstractQuery.execute(AbstractQuery.java:167)
	at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:104)
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66)
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30)
	at org.camunda.bpm.engine.impl.AbstractQuery.singleResult(AbstractQuery.java:130)
	at com.linkedin.perseus.workflows.example.CallActivityOne.execute(CallActivityOne.java:34)
	at org.camunda.bpm.engine.impl.bpmn.delegate.JavaDelegateInvocation.invoke(JavaDelegateInvocation.java:36)
	at org.camunda.bpm.engine.impl.delegate.DelegateInvocation.proceed(DelegateInvocation.java:54)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocationInContext(DefaultDelegateInterceptor.java:87)
	at org.camunda.bpm.engine.impl.delegate.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:59)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.execute(ServiceTaskJavaDelegateActivityBehavior.java:50)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ServiceTaskJavaDelegateActivityBehavior.performExecution(ServiceTaskJavaDelegateActivityBehavior.java:41)
	at org.camunda.bpm.engine.impl.bpmn.behavior.TaskActivityBehavior.execute(TaskActivityBehavior.java:65)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior$1.call(ClassDelegateActivityBehavior.java:65)
	at org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior$1.call(ClassDelegateActivityBehavior.java:62)
	at org.camunda.bpm.engine.impl.bpmn.behavior.AbstractBpmnActivityBehavior.executeWithErrorPropagation(AbstractBpmnActivityBehavior.java:108)

Thanks for the reply william!
I am seeing an exception when executing the above

org.camunda.bpm.engine.ProcessEngineException: Query return 2 results instead of max 1
at org.camunda.bpm.engine.impl.AbstractQuery.executeSingleResult(AbstractQuery.java:211)
at org.camunda.bpm.engine.impl.AbstractQuery.execute(AbstractQuery.java:167)

Oooh sorry my brother!

Try something like this in below, with one more validation by processInstanceId.

var retriesNumber = execution.getProcessEngineServices().getManagementService()
        .createJobQuery()
        .activityId(execution.getCurrentActivityId())
     .processInstanceId(execution.getProcessInstanceId())
        .singleResult()
        .getRetries();

I hope this helps.
William Robert Alves

1 Like

It looks like the number of retries are evaluated after first failure. So for 5 retries, if I print the number of retries on each failure, I see the following : 3,4,3,2,1.
Is there a way to differentiate between the first 3 (an actual 5) and last 3?

Another followup I have to the above solution is :

  1. Will it work for multi-instance?
  2. What can I read to understand how it was derived?

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