How do I schedule a task to happen every 'N' seconds in Spring framework How do I schedule a task to happen every 'N' seconds in Spring framework spring spring

How do I schedule a task to happen every 'N' seconds in Spring framework


You can annotate the desired routine using

public class Foo {    @Scheduled(fixedDelay=5000)    public void Bar() {       // ...    }}

But in order to for Spring for find and recognize the annotation you must declare which base package the class Foo lies in as well as configure Spring to look for scheduling tasks. Put the following in your spring XML configuration (and don't forget to import the XML namespaces contextand task).

<context:component-scan base-package="com.company.scheduling"/><task:annotation-driven />

Alternatively you can put @EnableScheduling before the class declaration and it does the XML config for you out of the box.

See also the context namespace and the task namespace.


You can use the Quartz scheduler integration as documented here:http://static.springsource.org/spring/docs/2.0.8/reference/scheduling.html

I do not understand the 2nd part of your question about refreshing a server method.


How abt using Quartz with spring. This is highly configurable by spliting Jobs, triggers and timers up. You can also use cron expressions and integrate things like JMX:

Spring and Quartz