Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the recommended way to run processes in the background with Spring?

I'm working on a web app with Spring (Boot, MVC) and there will be some things that will need to run in the background so I don't block the request.

Currently I was thinking in having a Spring Batch job that runs every 5mins and checks in a table (jobs_queue) in the DB if there are things to do and then run the tasks (based on what each row in the table requires).

Obviously this is not a good scalable solution (I think) as I will only have one worker (if there are too many jobs in the queue this single worker might not be able to handle the load) and it will also make at least one DB query every 5 mins (too see if there are jobs in queue).

What is a better way of doing this?

like image 389
daniels Avatar asked Jan 18 '16 20:01

daniels


3 Answers

Adding to @kotabek's answer, Spring Batch has the ability to launch jobs via messages. So you can have the JobLaunchingMessagHandler listen for requests to launch a job and run it accordingly. This allows you to handle this use case both locally (with direct channels) or remotely (via some messaging middleware like Rabbit). You can read more about it in the documentation here: http://docs.spring.io/spring-batch/reference/htmlsingle/#launching-batch-jobs-through-messages

like image 200
Michael Minella Avatar answered Nov 19 '22 22:11

Michael Minella


If you are using Spring 4, Have a look at @Scheduled annotation.

Example:

@Component
public class ScheduledTasks {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("The time is now " + dateFormat.format(new Date()));
    }
}

Code reference - https://spring.io/guides/gs/scheduling-tasks/

A small note: Don't forget to add @EnableScheduling in your config.

like image 39
shiladitya Avatar answered Nov 20 '22 00:11

shiladitya


I recommend you use RabbitMQ or ActiveMQ. So you can write you tasks in query and when somebody or something add task your job will work. I think you can integrate it with db and listen insert events.

like image 2
Otabek Kasimov Avatar answered Nov 19 '22 22:11

Otabek Kasimov