Peter Penzov
Peter Penzov

Reputation: 1754

Implement background process in Spring

I need to implement Spring process which checks database table for new rows and makes calculations. I'm thinking to implement infinite loop which is triggered every 10 minutes.

Is there someway to implement this with Spring Boot? I can always use Java Thread but it's for sure it's better to let Spring to manage this.

Upvotes: 1

Views: 3655

Answers (3)

Vineet Mishra
Vineet Mishra

Reputation: 109

It looks like this questions is old, but I would like to answer. See, you can make an object of ThreadPoolTaskExecutor and assign the background process to it. Here is my detailed code if anyone wants to see.

https://github.com/xbox2204/SpringBoot-JPA-Swagger-Angular

First thing to do would be, add these two annotation in your application starter class.

@EnableAsync
@EnableScheduling

Now, create you TaskExceutor in the same class and assign it to bean with a name, so that background tasks can be created anywhere in your application and attached with this bean.

    @Bean(name="thPlExectr")
    public Executor getExecutor(){
        return new ThreadPoolTaskExecutor();
    }

Now create a component class somewhere in the project, where you you will create the background task. Here, you will mention the frequency with which you want your task to be exceuted. I wanted to print a statement every 5 second, you can choose your own frequency and give your own method definiton. Also, make it async and attach it with the TaskExecutor bean mentioned above, so that it doesn't interrupt the normal flow of your application.

@Component
public class BackgroundTasks {
    private static final Logger log= LoggerFactory.getLogger(BackgroundTasks.class);

    @Async("thPlExectr")
    @Scheduled(fixedRate = 5000)
    public void backTask(){
        log.info("Hi Vineet! I am a background task");
    }
}

Image of the final result

Upvotes: 3

darshakat
darshakat

Reputation: 689

find the below sample code

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class AppScheduler {

    @Scheduled(fixedRate = 10000)
    public void myScheduler() {
        System.out.println("Test print");
    }

}

Upvotes: 2

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

You can try scheduling with Spring Schedule

Here is a official example

Technically, you enable scheduling with @EnableScheduling and annotated your task with @Scheduled(fixedRate=600000).

Another config you can used to tune your scheduler:

  • fixedRate: Execute the annotated method with a fixed period in milliseconds between invocations.

  • fixedDelay: Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.

  • cron: A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.

Upvotes: 6

Related Questions