首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring配备quartz定时任务

2013-08-01 
spring配置quartz定时任务beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp:/

spring配置quartz定时任务

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

">

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->

<bean id="acWarehouseSchedulerFactoryBean" lazy-init="false"

autowire="no" />

</list>

</property>

<property name="applicationContextSchedulerContextKey" value="applicationContext" />

<property name="configLocation" value="classpath:quartz.properties" />

</bean>

?

<entry key="targetMethod" value="execute" />

</map>

</property>

</bean>

?

?

<!-- every hour -->

<!-- 定义触发时间 -->

<bean id="deleteZipTrigger" />

</property>

<property name="cronExpression">

<value>59 59 * * * ?</value>

<!-- <value>0 0/3 * * * ?</value> -->

</property>

</bean>

<!-- 要调用的工作类 -->

<bean id="deleteZipJob" + targetObject + "] at once1111111111111>>>>>>"); ?

? ? ? ? ? ? Object otargetObject = ctx.getBean(targetObject); ?

? ? ? ? ? ? Method m = null; ?

??

? ? ? ? ? ? try { ?

? ? ? ? ? ? ? ? m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class}); ?

? ? ? ? ? ? ? ? m.invoke(otargetObject, new Object[] {context}); ?

? ? ? ? ? ? } catch (SecurityException e) { ?

? ? ? ? ? ? ? ? logger.error(e); ?

? ? ? ? ? ? } catch (NoSuchMethodException e) { ?

? ? ? ? ? ? ? ? logger.error(e); ?

? ? ? ? ? ? } ?

? ? ? ? } catch (Exception e) { ?

? ? ? ? ? ? throw new JobExecutionException(e); ?

? ? ? ? } ?

? ? } ?

??

? ? public void setApplicationContext(ApplicationContext applicationContext) { ?

? ? ? ? this.ctx = applicationContext; ?

? ? } ?

??

? ? public void setTargetObject(String targetObject) { ?

? ? ? ? this.targetObject = targetObject; ?

? ? } ?

??

? ? public void setTargetMethod(String targetMethod) { ?

? ? ? ? this.targetMethod = targetMethod; ?

? ? } ?

}

TaskTestBean类文件:

//@2步骤

public class TaskTestBean implements Job ,Serializable {?

private static final long serialVersionUID = 4937479059845599009L;

? ? private static final Logger logger = Logger.getLogger(TaskTestBean.class);

? ? public TaskTestBean(){

? ?

? ? }

? ? ??

? ? @Override ?

? ? public void execute(JobExecutionContext arg0) throws JobExecutionException { ?

? ? logger.info("-----------进入自动任务------------");

? ?

? ? ? ? logger.info("-----------此次自动任务结束------------");

? ?

? ? }

?}

?

方式二:

?

applicationContext.xml 配置文件:

?

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

">

?

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 内存中处理 -->

<bean id="startQuartz" lazy-init="false" autowire="no"?

?/>?

</list>?

</property>?

</bean>?

?

<!-- 定义调用对象和调用对象的方法 内存中处理 -->

<bean id="deleteZipJobDetail2"

/>

</property>

<!-- 调用类中的方法 -->

<property name="targetMethod">

<value>work</value>

</property>

</bean>

<!-- every hour -->

<!-- 定义触发时间 -->

<bean id="deleteZipTrigger" />

</property>

<property name="cronExpression">

<value>59 59 * * * ?</value>

<!-- <value>0 0/3 * * * ?</value> -->

</property>

</bean>

<!-- 要调用的工作类 ?内存处理 -->

<bean id="updateFromAcDataZip" 每天中午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期间的每1分钟触发?
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发?
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发?
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发?
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午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" 每月的第三个星期五上午10:15触发?

?

?

参考连接:http://www.open-open.com/lib/view/open1354666663304.html

热点排行