Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Task Async and Delayed

I need to do onething that I don't know wich is the best practice to this.

After I send one request to an especific service, this one returns OK and queue my request. I have a callback service that is used to notify when it ends.

The problem is that the whole process can take a long time and over without notify anything and after that I need to consider a timeout.

The application is SpringBoot APP and I was considering to use the annotations @EnableAsync and @Async on a service method with a Sleep time.

@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("TIMCLL-");
        executor.initialize();
        return executor;

    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        // TODO Auto-generated method stub
        return null;
    }

}

. . .

@Async
public void verifyStatusTimPayment() throws InterruptedException {

    Thread.sleep(5000);
    logger.info( "Executed after 5s " +  new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date())); 

}

The verification needs to be done 15 minutes after the request and have to occur just one time per request.

How can I do this without make a Thread.sleep ?????

like image 933
danilongc Avatar asked Dec 01 '16 17:12

danilongc


3 Answers

You could use the ScheduledExecutorService to schedule the Task

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
...
scheduler.schedule(() -> {yourtaskhere}, 15, TimeUnit.MINUTES);

But this is not what you want. What if the server dies between the scheduling of the task and its execution? you will lose your task. It would be better if you persist the message in a queue, and retrieve it later, or use any scheduler that uses persistence (a la Quartz)

like image 197
Ruben Avatar answered Sep 28 '22 15:09

Ruben


you can add @EnableScheduling annotation to your configuration:

@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfiguration implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(500);
        executor.setThreadNamePrefix("TIMCLL-");
        executor.initialize();
        return executor;

    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        // TODO Auto-generated method stub
        return null;
    }

}

if you want to do schedule once and delayed you can call the taskScheduler :

@Autowired 
private TaskScheduler taskScheduler;

and execute the task:

taskScheduler.schedule(
    () -> {//your task}, 
    //Your Delay task
);
like image 24
user2582794 Avatar answered Sep 28 '22 14:09

user2582794


i think we can use @Scheduled in latest spring. it will run every 15 minutes like method is annotated as below

@Scheduled(cron = "0 0/15 * * * *")
public void verifyStatusTimPayment() throws InterruptedException {
    logger.info( "Executed after 5s " +  new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date())); 

}

i know i am late but might help someone who is going through thread

like image 40
Musab Qamri Avatar answered Sep 28 '22 16:09

Musab Qamri