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?
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With