I am spawning a thread which will keep pulling the chunk of records from database and putting them into the queue. This thread will be started on the server load. I want thi
You can try using Quartz scheduler. http://quartz-scheduler.org/ That will allow you to specify the duration of time between task executions. You can set a flag (boolean) that says if the class has run before to avoid duplicating code that you only want to run the 'first' time.
You can use the @Scheduled
annotation to run jobs. First create a class with a method that is annotated with @Scheduled
.
Class
public class GitHubJob {
@Scheduled(fixedDelay = 604800000)
public void perform() {
//do Something
}
}
Then register this class in your 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 about scheduling visit the Spring Docs.
I think your requirement is just regular senario which quartz or spring scheduling framework supports very well. but you want to create a spececial approach to impl it. my suggestion is to keep it simple and stupid. spring scheudling will take advantage your worker thread by pooling instead of runing it all the time.
As of statistics, it 's easy to check worker class's log. if you want see the statistics in web console, you need to record worker's log in database. I am sure you could make it easily in normal way.
@Scheduled(fixedDelay=3600000)
private void refreshValues() {
[...]
}
That runs a task once every hour. It needs to be void and accept no arguments. If you use Spring's Java configuration you'll also need to add the annotation @EnableScheduling
to one of your @Configuration
classes.
Spring has out-ot-the-box support for scheduling tasks. Here are for the docs for the latest 3.2.x version of spring but check the docs for the version you are using. Looks like it uses Quartz under the hood for scheduling tasks.