爱编程的欧巴

让我们成长吧~
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

SpringQtz 时间任务调度

Posted on 2016-08-03 20:08  爱编程的欧巴  阅读(364)  评论(0编辑  收藏  举报

1、配置所需要maven jar包

<!-- 任务调度需要的jar包-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- 任务调度需要的jar包-->

 

2、配置文件spring-timer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置调度程序quartz ,其中配置JobDetail有两种方式-->
<!--方式一:使用JobDetailBean,任务类必须实现Job接口 -->
<!-- <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean"> -->
<!-- <property name="name" value="exampleJob"></property> -->
<!-- <property name="jobClass" value="com.zbb.cn.filter.SpringQtz"></property> -->
<!-- <property name="jobDataAsMap"> -->
<!-- <map> -->
<!-- <entry key="service"> -->
<!-- <value>simple is the beat</value> -->
<!-- </entry> -->
<!-- </map> -->
<!-- </property> -->
<!-- </bean> -->
<!--运行时请将方式一注释掉! -->
<!--
方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法
-->
<!-- 定义目标bean和bean中的方法 -->
<bean id="SpringQtzJob" class="com.cc.hkjc.controller.backstage.interceptor.SpringQtz">

</bean>

<bean id="SpringQtzJobMethod"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="SpringQtzJob" />
</property>
<property name="targetMethod"> <!-- 要执行的方法名称 -->
<value>execute</value>
</property>
</bean>

<!-- ======================== 调度触发器 ======================== -->
<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SpringQtzJobMethod"></property>
<!-- <property name="cronExpression" value="0/5 * * * * ?"></property> -->
<property name="cronExpression" value="0 0 10 * * ?"></property> <!-- 每天晚上十点触发 -->
</bean>

<!-- ======================== 调度工厂 ======================== -->
<bean id="SpringJobSchedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CronTriggerBean" />
</list>
</property>
</bean>

</beans>

 

 

package com.cc.hkjc.controller.backstage.interceptor;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.cxf.common.util.StringUtils;

import com.cc.hkjc.domain.HcNum;
import com.cc.hkjc.domain.Zstj;
import com.cc.hkjc.service.PhoneService;
import com.cc.hkjc.service.ZstjService;

public class SpringQtz {
private static int count=0;
@Resource
private ZstjService zstjService;

@Resource
private PhoneService phoneService;

public void execute() {
  
}
}

 

4、在web.xml配置spring-timer.xml