1. 程式人生 > 其它 >定時任務quartz

定時任務quartz


pom引入

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>1.8.6</version>

</dependency>

配置檔案xml中引入

<!--配置schedule -->

<import resource="classpath:context/schedule/schedule-center.xml" />

<!--例子 -->

<import resource="classpath:context/schedule/schedule-syncTeamName-config.xml"/>

<!--配置properties檔案 -->

<import resource="classpath:context/envPropertyLoader.xml" />

schedule-center.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:jee="http://www.springframework.org/schema/jee"

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

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

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/tx

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

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd"

default-lazy-init="false">

<!-- 總管理類 如果將lazy-init='false'那麼容器啟動就會執行排程程式 -->

<bean id="schedulerFactory" lazy-init="false" autowire="no"

class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="tp-schedule.syncTeamNameTrigger" />

</list>

</property>

</bean>

</beans>

envPropertyLoader.xml

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:/properties/tp-schedule.properties</value>

</list>

</property>

</bean>

</beans>

schedule-syncTeamName-config.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:jee="http://www.springframework.org/schema/jee"

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

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/tx

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

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd"

default-lazy-init="false">

<!-- 定義觸發時間 -->

<bean id="tp-schedule.syncTeamNameTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail">

<ref bean="syncTeamNameJob" />

</property>

<!-- cron表示式 -->

<property name="cronExpression">

<value>${tp-schedule.SYNCTEAMNAMEJOB}</value>

<!--properties檔案 tp-schedule.SYNCTEAMNAMEJOB= 0 0 15 ? * FRI -->

</property>

</bean>

<!-- 定義呼叫物件和呼叫物件的方法 -->

<bean id="syncTeamNameJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >

<!-- 呼叫的類 -->

<property name="targetObject">

<ref bean="syncTeamNameJobExecutor" />

</property>

<!-- 呼叫類中的方法 -->

<property name="targetMethod">

<value>execute</value>

</property>

</bean>

<!-- 入口程式 init-method="execute" 容器啟動自動執行一次-->

<bean id="syncTeamNameJobExecutor" class="com.bill99.qamp.schedule.syncTeamNameJobExecutor" init-method="execute">

<property name="iBdf2DeptServiceImpl" ref="iBdf2DeptServiceImpl" />

</bean>

</beans>

syncTeamNameJobExecutor

public class syncTeamNameJobExecutor {

private Logger logger = Logger.getLogger(this.getClass());

private IBdf2DeptService iBdf2DeptServiceImpl;

public voidexecute(){

logger.info("**-----開始同步TeamName");

iBdf2DeptServiceImpl.insertbdf2dept();

logger.info("**-----同步TeamName完成");

}

public void setiBdf2DeptServiceImpl(IBdf2DeptService iBdf2DeptServiceImpl) {

this.iBdf2DeptServiceImpl = iBdf2DeptServiceImpl;

}

}

本文來自部落格園,作者:up~up,轉載請註明原文連結:https://www.cnblogs.com/soft-engineer/p/14985794.html