Task Scheduling in Spring / Java

I create a thread that will continue to pull a piece of records from the database and put them in the queue. This thread will start when the server boots. I want this thread to be active all the time. If there are no entries in the database, I want it to wait and check again after a while. I was thinking about the spring task scheduler to schedule this, but not sure if this is correct, because I only want my task to run once. What would be a good way to implement this in spring?

In addition, I need to have a border check that if my thread does not work (due to any errors or exception conditions), it should be re-created after some time.

I can do all of this in java using thread communication methods, but just try if there is something in spring or Java for such scenarios.

Any suggestions or pointers will help.

+6
source share
5 answers

You can use the @Scheduled annotation to run tasks. First create a class using a method that annotates with @Scheduled .

Class

 public class GitHubJob { @Scheduled(fixedDelay = 604800000) public void perform() { //do Something } } 

Then register this class in the configuration files.

spring -context.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <tx:annotation-driven/> <task:annotation-driven scheduler="myScheduler"/> <task:scheduler id="myScheduler" pool-size="10"/> <bean id="gitHubJob" class="org.tothought.spring.jobs.GitHubJob"/> </beans> 

For more on planning, visit Spring Docs .

+6
source
 @Scheduled(fixedDelay=3600000) private void refreshValues() { [...] } 

Performs a task once an hour. It must be invalid and not accept any arguments. If you are using Spring Java configuration, you also need to add the @EnableScheduling annotation to one of your @Configuration classes.

+4
source

You can try using the Quartz Scheduler. http://quartz-scheduler.org/ This will allow you to specify the length of time between tasks. You can set a flag (boolean) that says if the class has been executed before to avoid duplicating code for which you want to run only the "first" time.

0
source

Spring has optional support for task scheduling. Here is for the documents for the latest version of version 3.2.x spring, but check the documents for the version used. It looks like he uses quartz under the hood to plan tasks.

0
source

I think your requirement is just a regular senario that supports a quartz or spring framework. but you want to create a special approach to implementation. my suggestion is to keep it simple and stupid. spring scheudling will use your workflow by combining, rather than starting it all the time.

According to statistics, it is easy to check the working class journal. if you want to view statistics in the web console, you need to write a log to the log. I am sure you could do it easily in the usual way.

0
source

All Articles