Scheduling Task in Spring/Java

12,817

Solution 1

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.

Solution 2

@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.

Share:
12,817
Ashu
Author by

Ashu

Updated on June 30, 2022

Comments

  • Ashu
    Ashu almost 2 years

    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 this thread to be active all the time. If there are no records in the database I want it to wait and check again after some time. I was thinking of using spring task scheduler to schedule this but not sure if that is right because I only want my task to be started once. What will be the good way of implementing this in Spring ?

    Also, i need to have a boundary check that if my thread goes down (because of any error or exception condition) it should be re-instantiated after some time.

    I can do all this in java by using thread communication methods but just trying if there is something available in Spring or Java for such scenarios.

    Any suggestions or pointer will help.