How correctly run task in RepeatStatus.CONTINUABLE while some condition?

237 Views Asked by At

I have a simple job that consists of 2 tasklet each of them making an HTTP request to get some status result.

    @Bean
public Job receiveStatusesJob(
        @Qualifier(RECEIVE_STATUSES_PARTITIONER_BEAN) Partitioner partitioner,
        SecondStatusReceivingTasklet secondStatusReceivingTasklet) {
    var messagingTemplate = new MessagingTemplate();
    messagingTemplate.setDefaultChannel(receiveStatusesPartitionRequestChannel());

    return jobBuilderFactory.get(RECEIVE_STATUSES_JOB)
            .start(managerStepBuilderFactory.get(RECEIVE_STATUS_MANAGER_STEP)
                    .partitioner(RECEIVE_STATUS_WORKER_STEP, partitioner)
                    .gridSize(100)
                    .messagingTemplate(messagingTemplate)
                    .allowStartIfComplete(true)
                    .build())
            .next(stepBuilderFactory.get(SECOND_RECEIVE_STATUS_STEP)
                    .tasklet(secondStatusReceivingTasklet)
                    .allowStartIfComplete(true)
                    .build())
            .incrementer(new RunIdIncrementer())
            .build();
}

First tasklet looks like

@Slf4j
@Component
@StepScope
public class StatusReceivingTasklet extends Tasklet {
//constructors and services

@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) {
    Object urlsToCheck = stepContribution.getStepExecution().getExecutionContext().get(URLS_TO_CHECK_PARAM);
    if (urlsToCheck instanceof List) {
        return receiveProviderPaymentCbsStatuses((List<String>) urlsToCheck );
    }
}

private RepeatStatus receiveStatuses(List<String> urlsToCheck ) {
    var repeatStatus = RepeatStatus.FINISHED;
    for (String url: urlsToCheck ) {
            repeatStatus = sendStatusRequestAndHandleResponse(repeatStatus, url);
        } 
    return repeatStatus;
}

private RepeatStatus sendStatusRequestAndHandleResponse(RepeatStatus repeatStatus,
                                                        String urlToCheck) {
  
    StatusResponse statusResponse =
            sendingService.checkStatus(urlToCheck);
    log.debug("Received CBS response: {}", cbsStatusResponse);
    if (statusResponse.getHttpStatus().equals(200)) {
        //business logis work
    } else if (!statusResponse.getHttpStatus.equals(200) && !status.getHttpStatus.equals(500)) {
        repeatStatus = RepeatStatus.CONTINUABLE;
    } else {
// business logic
    }
    return repeatStatus;
}

}

So if I have a response status different than 200 and 500 Task is on state CONTINUALBE if I have 200 or different status than 500 task state is FINISHED. When I have tas state CONTINUABLE how correctly continue receiving statuses periodically (every 5-10 mins) while task is not in state FINISHED ?

1

There are 1 best solutions below

0
On

I'm not sure I fully understand your use case, but I would not mix the scheduling logic (every 5-10 mins) with the business logic of the task. You can define a task that does a single call, and then schedule it to run every 5-10 mins with your favourite scheduling library (a method annotated with @Scheduled for instance.