Spring 的定时任务

今天已经是农历12月26了,过几天就要放假过年了~~~~~        一年又一年~~~~~~~~~~~~

情景:这周有个任务,就是要 定时迁移表的数据,所以要用到定时器,于是在网上一段乱搜。

首先,看以下 从其他各位大神博主 参考过来的文字:

从实现的技术上来分类,目前主要有三种技术(或者说有三种产品):

  1. Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少。
  2. 使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。
  3. Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。

针对作业使用的触发器,主要有以下两种:

  1. 每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean
  2. 每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

一.使用  Spring3.0 后自带的 定时器 (很轻,很简单)

1.Spring 配置文件(某博主的例子)

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/task   
        http://www.springframework.org/schema/task/spring-task-3.0.xsd”

<description>
        定时任务
    </description>
    //定时注解驱动
    <task:annotation-driven />
    //进行定时任务的类,将其定义为一个bean
    <bean id="spaceStatisticsService" class="com.pojo.system.manager.sigar.impl.SpaceStatisticsServiceImpl"></bean>
    //通过task标签,定义定时功能
    <task:scheduled-tasks>
        <task:scheduled ref="spaceStatisticsService" method="statisticSpace" cron="59 59 23 * * ?" />
    </task:scheduled-tasks>

2.实现类               

@Service
public class SpaceStatisticsServiceImpl implements SpaceStatisticsService
{
    @Override
    public void statisticSpace()
    {
        System.out.println("实现定时功能");
    }
}

简单吧

然后我照着上面的例子,自己在项目中实现了一下~

1.Spring 配置文件

2.类  (我跟例子不同的是,我需要从容器拿东西,所以实现了接口,其实是一样的)

上述代码的意思是:每天 02:10:00 都会执行 SpringSelfTask 类中的doPlan() 方法

我试了实现两个不同的接口

(1)实现 ServletContextListener,但是wac 为null  ,所以我还在web.xml中做了如下的配置

<listener>
	<listener-class>com.zhiguangyun.modules.dvs.task.SpringSelfTask</listener-class>
  </listener>
public class SpringSelfTask implements ServletContextListener{
	
	private static ApplicationContext wac = null;
  
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}
    @Override
    public void contextInitialized(ServletContextEvent event) {
    	ServletContext servletContext = event.getServletContext();
		wac= WebApplicationContextUtils.getWebApplicationContext(servletContext);
    }
	public void doPlan(){
		System.out.println("迁移时间: "+new Date());
		
	}
}

(2)实现 ApplicationContextAware 接口,wac 可以顺利拿到,无须再在 web.xml 进行如上配置

public class SpringSelfTask implements ApplicationContextAware{

	private Logger logger = Logger.getLogger(SpringSelfTask.class);
	private static ApplicationContext wac = null;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
           wac= applicationContext;
	}
	public void doPlan(){
		PlanResultDeatilService planResultDeatilService = (PlanResultDeatilService) wac.getBean("planResultDeatilService");
	}
}

CronTrigger配置格式:

格式: [秒] [分] [小时] [日] [月] [周] [年]

0 0 12 * * ? 每天12点触发
0 15 10 ? * * 每天10点15分触发
0 15 10 * * ? 每天10点15分触发
0 15 10 * * ? * 每天10点15分触发
0 15 10 * * ? 2005 2005年每天10点15分触发
0 * 14 * * ? 每天下午的 2点到2点59分每分触发
0 0/5 14 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发)
0 0/5 14,18 * * ? 每天下午的 2点到2点59分(整点开始,每隔5分触发)
每天下午的 18点到18点59分(整点开始,每隔5分触发)
0 0-5 14 * * ? 每天下午的 2点到2点05分每分触发
0 10,44 14 ? 3 WED 3月分每周三下午的 2点10分和2点44分触发
0 15 10 ? * MON-FRI 从周一到周五每天上午的10点15分触发
0 15 10 15 * ? 每月15号上午10点15分触发
0 15 10 L * ? 每月最后一天的10点15分触发
0 15 10 ? * 6L 每月最后一周的星期五的10点15分触发
0 15 10 ? * 6L 2002-2005 从2002年到2005年每月最后一周的星期五的10点15分触发
0 15 10 ? * 6#3 每月的第三周的星期五开始触发
0 0 12 1/5 * ? 每月的第一个中午开始每隔5天触发一次
0 11 11 11 11 ? 每年的11月11号 11点11分触发(光棍节)
发布了100 篇原创文章 · 获赞 96 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/qq_25221835/article/details/86715213