External Task fetchAndLock() getting zero task

Hi All,
i’m trying to fetch the task using fetchAndLock() but getting zero task.

I have two services which are spring boot camunda application(process-service) and one more is ExternalTask client spring boot application(notification-service).
The process-service is having running tasks and i’m trying to fetch those task with ExternalTask using fetchAndLock() but getting as zero task.
attached code snippet please suggest there is any way to fetch and lock the tasks.

 @Inject
   private ExternalTaskService externalTaskService;

@ExternalTaskSubscription("notificationTopic")
@Bean
public void sendEmailNotification() throws Exception {
     List<LockedExternalTask> tasks = externalTaskService.fetchAndLock(5, "worker")
	  .topic("externalTaskTopic", 1000L).execute();

          for (LockedExternalTask task : tasks) {
  
  // Business logic for send mail

              externalTaskService.complete(task.getId, worker);

Hey @Purushotham & welcome to the Community!
I am happy to investigate this problem together with you. :slight_smile:

To make sure we are on the same page - here are a few questions:

  1. Which dependency and what version do you use in your external task worker?
  2. You have started one/multiple process instances in Camunda Platform 7 already?

Besides, I do have a very old snippet of Java code for a spring boot worker lying around on my GitHub repository. You can find it here: camunda-javadev-training/Application.java at main · Hafflgav/camunda-javadev-training · GitHub
(It is for not the most up to date version which you used - but maybe you can identify some difference already)

Let me know the answer to my questions and I will try things out myself. :slight_smile:
Best,
Thomas

1 Like

Hi @Hafflgav,
Thanks for your reply! :slight_smile:

Here is the answers for the your questions.

  1. I’m using 7.16 version for external task,

     <dependency>
         <groupId>org.camunda.bpm.springboot</groupId>
         <artifactId>camunda-bpm-spring-boot-starter-external-task-client</artifactId>
         <version>7.16.0</version>
     </dependency>
    
  2. Yes, I’ve started multiple process instances in Camunda Platform 7

1 Like

Hi @Hafflgav ,

I’m already done with your GitHub code flow and i’m able to proceed, but i want to fetch the tasks using fetchAndLock(),
why i’m getting zero task even having multiple process instances running in Camunda Platform 7 already.

Could you please help on this below flow, i need to use the fetchAndLock() method only to get the tasks.

public void sendEmailNotification() throws Exception {
     List<LockedExternalTask> tasks = externalTaskService.fetchAndLock(5, "worker")
	  .topic("externalTaskTopic", 1000L).execute();

          for (LockedExternalTask task : tasks) {

Thanks,

I see that lockDuration is set to 1 second which is extremely small. Could you try a bigger value 20000L

lockDuration - the duration in milliseconds for which tasks should be locked; begins at the time of fetching

1 Like

Thanks @hassang!
I was just about to try it out. Haven’t used C7 in quite some time…

@Purushotham - let us know if this changes anything! :slight_smile:

1 Like

Hi @hassang ,
Still i’m getting zero tasks only,
attached screen could you please check and help on this.

image

Hi @Purushotham,

if you use the annotation @ExternalTaskSubscription in the spring-boot-application, you will get the task and the service in a lambda function:

  @Bean
  @ExternalTaskSubscription("creditScoreChecker")
  public ExternalTaskHandler creditScoreCheckerHandler() {
    return (externalTask, externalTaskService) -> {
      // add your business logic here
      externalTaskService.complete(externalTask);
    };
  }

See External Task Client Spring Boot Starter | docs.camunda.org.

Your additional fetchAndLock call didn’t receive any tasks as they are already locked by the framework.

Hope this helps, Ingo

2 Likes

Hello Camunda team,

I’m from the Purushotham’s team, what is the difference between both the approaches ? `Approach-1: @ExternalTaskSubscription(“collateral4EyeCheckTopic”)
@Bean
public ExternalTaskHandler updateCollateral4EyeCheckResult() {

return (externalTask, externalTaskService) -> {
  String status=externalTask.getVariable("4eyeCheckStatus");


  LOG.info("<=========Collateral 4EyeCheck completed with status {} for task{}============>",status,externalTask.getId());
  externalTaskService.complete(externalTask);
};

}andApproach-2 @ExternalTaskSubscription(“collateral4EyeCheckTopic”)
@Bean
public void updateCollateral4EyeCheckResult1() {
List tasks = externalTaskService.fetchAndLock(10, “collateral-4eyeCheck-result-processor”)
.topic(“collateral4EyeCheckTopic”, 20000L)
.execute();
for (LockedExternalTask task : tasks) {
LOG.info("<=========task{} started============>",task.getId());
String approvalStatus = (String) task.getVariables().get(“4eyeCheckStatus”);
externalTaskService.complete(task.getId(),approvalStatus);
}
}`
Here, Approach-1 is working fine but while using Approach-2 it returns 0 task as explained by Purushotham in above thread.

1 Like

This is the exact same issue that Ingo explained above, and is clear in the link that was supplied.
When the @ExternalTaskSubscription is used, it will AUTOMATICALLY fetch tasks.

Since the task is already collected, it cannot be collected again.