import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
@Configuration
public class ScheduleConfig {
int shutdownTimeout = 1 * 60 * 1000;//1 minutes
private final String cronExpression = "0 0 1 * * ?";//every hour
public void start() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = threadPoolTaskScheduler();
threadPoolTaskScheduler.initialize();
threadPoolTaskScheduler.schedule((Runnable) task(), new CronTrigger(cronExpression));
}
public Runnable task() {
//return the job to launch
}
public void stop() {
ScheduledExecutorService scheduledExecutorService = threadPoolTaskScheduler().getScheduledExecutor();
try {
scheduledExecutorService.shutdownNow();
if (!scheduledExecutorService.awaitTermination(shutdownTimeout, TimeUnit.SECONDS)) {
LOG.debug("error during shutdown - did not terminate properly");
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setThreadNamePrefix("job");
return threadPoolTaskScheduler;
}
}
Tuesday, May 31, 2011
Scheduling jobs using Spring
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment