总体SpringMvc.xml中的效果:
第一步:
在Springmvc的xml中加入如下:
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
效果如下图:
第二步:
在自定扫描包下中间加上:
<!-- 任务调度器 配置线程池 -->
<task:scheduler id="scheduler" pool-size="10" />
<!--开启注解调度支持 @Scheduled -->
<task:annotation-driven executor="scheduler" proxy-target-class="true"/>
效果如下:
第三步:
编写测试类:需要在类加@Component注解,但是本人在@Controller测试可行,在相应的方法上使用@Scheduled注解(import org.springframework.scheduling.annotation.Scheduled;)如:
/**
* 每天晚上23点执行查询第三方数据任务
* @throws Exception
*/
@Scheduled(cron = "0 0 23 * * ?")
public void Scheduled() throws Exception{
System.out.println("每天的23:00时间到了,开始调用第三方接口查询流水任务咯");
}
最终效果:
注:在第一步、第二步的时候,可能SpringMVC.xml会报错,这是复制文本的时候有空格导致的,将所有的换行自行删除,再重新格式化就行