Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.xyxy.platform.examples.showcase.demos.schedule; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.apache.commons.lang3.Validate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import com.xyxy.platform.modules.core.utils.Threads; /** * SpringThreadPoolTaskSchedulerCron?. * SpringTask NameSpace??, ????. * * */ public class SpringCronJob implements Runnable { private String cronExpression; private int shutdownTimeout = Integer.MAX_VALUE; private ThreadPoolTaskScheduler threadPoolTaskScheduler; @Autowired private UserCountScanner userCountScanner; @PostConstruct public void start() { Validate.notBlank(cronExpression); threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); threadPoolTaskScheduler.setThreadNamePrefix("SpringCronJob"); threadPoolTaskScheduler.initialize(); threadPoolTaskScheduler.schedule(this, new CronTrigger(cronExpression)); } @PreDestroy public void stop() { ScheduledExecutorService scheduledExecutorService = threadPoolTaskScheduler.getScheduledExecutor(); Threads.normalShutdown(scheduledExecutorService, shutdownTimeout, TimeUnit.SECONDS); } /** * ??. */ @Override public void run() { userCountScanner.executeBySpringCronByJava(); } public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } /** * normalShutdown,??. */ public void setShutdownTimeout(int shutdownTimeout) { this.shutdownTimeout = shutdownTimeout; } }