好久没写博客了,废话不说,直入正题,Quertz的简单使用:
创建一个类,用于调用定时任务的接口:
public class InsertTotalJob
{
private static boolean isRun = false;
/**
* 执行定时任务
*/
public void execute()
{
try
{
if (!isRun)
{
isRun = true;
String date = Tool.DATE.getDate();
//SpringHelp.getBean(InsertTotalJobService.class).executeByTimer(date);
/*项目中这里进行了封装,其实在这里就可以直接调用你要执行的任务接口了*/
isRun = false;
}
} catch (Exception e)
{
isRun = false;
Logs.jobLog.error("未统计额度定时任务异常!", e);
}
}
}
简单的定时任务在代码中注释的位置写就可以了,倘若复杂,在进行封装即可。
创建spring-quartz.xml文件,定时器指向InsertTotalJob类,代码如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<beans>
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="DifAreaJob" />
<!-- 未汇总额度入库定时任务,每天8点至18点,每隔30分钟执行一次-->
<ref bean="InsertTotalJob" />
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->
<bean id="DifAreaJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.forms.bjczfs.web.difAreaPay.job.DifAreaJob"/>
</property>
<property name="targetMethod" value="execute"/>
</bean>
</property>
<property name="cronExpression" value="0 30 11 * * ?"/>
</bean>
<bean id="InsertTotalJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.forms.dxczzf.web.plan.selPlan.job.InsertTotalJob"/>
</property>
<property name="targetMethod" value="execute"/>
</bean>
</property>
<property name="cronExpression" value="0 */30 08-18 * * ?"/>
</bean>
</beans>
</beans>
在applicationContext.xml中引入spring-quartz.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring公共配置文件,开启自动扫描、配置属性文件、国际化文件、初始化、日志等组件 -->
<import resource="spring-commons.xml"/>
<!-- Quartz配置文件 -->
<import resource="spring-quartz.xml"/>
</beans>
OK,启动项目,定时器就可以运行了!
注:测试时,可以将时间调的短一点,不如两分钟,方便测试:
<property name="cronExpression" value="0 */2 * * * ?"/>