java Spring定时任务详解

来源:互联网 发布:冠能狗粮网络代理 编辑:程序博客网 时间:2024/05/15 23:21

1.创建一个调用业务层的类并要继承QuartzJobBean

例:Object obj = SpringContextUtil.getBean("tOrderService");   //获取spring实例bean

public class ConfirmOrderStateJob extends QuartzJobBean {

@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
Object obj = SpringContextUtil.getBean("tOrderService");
if (null != obj) {// 非空再进行业务处理
TOrderService tOrderService = (TOrderService) obj;
tOrderService.doConfirmOrderStateJob();
}
}
}


2.创建一个properties文件

例如:quartz.properties

在xml文件中会被引用到

内容:如下例子

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class =org.quartz.simpl.RAMJobStore


3.创建xml文件  

例如:frame-quartz-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!--**************************************单机配置,每台节点均运行********************************-->

<!-- 统计定时任务-->
<bean id="newAcess" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.yxd.pris.common.job.ConfirmOrderStateJob</value>
</property>
</bean>
<!-- 统计访问sesion,每2分钟运行一次-->
<bean id="cronnewNewAcess" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="newAcess" />
<property name="cronExpression" value="0 */2 * * * ?" />
</bean>
<bean name="quartzSchedulerAccess"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:cfg/quartz_access.properties" />
<property name="triggers">
<list>
<ref bean="cronnewNewAcess" />
</list>
</property>
<property name="autoStartup" value="true"></property>
</bean>
</beans>


4.实现业务逻辑

原创粉丝点击