定时任务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 void execute(){

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

iBdf2DeptServiceImpl.insertbdf2dept();

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

}

public void setiBdf2DeptServiceImpl(IBdf2DeptService iBdf2DeptServiceImpl) {

this.iBdf2DeptServiceImpl = iBdf2DeptServiceImpl;

}

}

posted @ 2021-07-08 14:06  up~up  阅读(52)  评论(0编辑  收藏  举报