首页 > 编程语言 > 详细

Java定时任务Quartz

时间:2019-01-28 13:06:18      阅读:215      评论:0      收藏:0      [点我收藏+]

第一步:创建xml文件,名称为:spring-scheduler 路径如下图:

技术分享图片

第二步:spring-scheduler配置详情

<!--创建任务-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="updateTimeOutUrgentJobDetail" />
                <ref bean="earlyWarnJobDetail" />
                <!--<ref bean="msgPushJobDetail" />-->
            </list>
        </property>
<!--引入触发器-->
        <property name="triggers">
            <list>
                <ref bean="updateTimeOutUrgentTrigger" />
                <ref bean="earlyWarnJobTrigger" />
                <!--<ref bean="msgPushJobTrigger" />-->
            </list>
        </property>
    </bean>

    <!--任务 引入具体的service-->
    <bean id="updateTimeOutUrgentJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="cn.com.klec.bepcs.web.quartz.TimeOutUrgentJob" />
        <property name="jobDataMap">
            <map>
                <entry key="timeOutUrgentJobService" value-ref="timeOutUrgentJobService" />
            </map>
        </property>

        <property name="durability" value="true" />
    </bean>
<!--创建触发器-->
     <bean id="updateTimeOutUrgentTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--引入任务-->
        <property name="jobDetail" ref="updateTimeOutUrgentJobDetail" />
<!--指定 Cron 表达式-->
        <property name="cronExpression" value="0 0 * * * ?"/>
    </bean>

第三步:实现定时任务,具体的业务操作

第四步:创建任务 TimeOutUrgentJob

public class TimeOutUrgentJob extends QuartzJobBean {
    private static final Logger LOG = LoggerFactory.getLogger(TimeOutUrgentJob.class);
    private TimeOutUrgentJobService timeOutUrgentJobService;
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LOG.info("**********************里程碑催办任务启动*******************************");
        try {
            timeOutUrgentJobService = (TimeOutUrgentJobService) context.getMergedJobDataMap().get("timeOutUrgentJobService");
             timeOutUrgentJobService.insertInfo();
            
            
        } catch (Exception e) {
            LOG.error("里程碑催办任务异常: ", e);
        } finally {
            LOG.info("**************************里程碑催办任务结束 **************************");
        }
    }
        
}

注释:timeOutUrgentJobService.insertInfo();就是项目中需要具体实现的业务

Java定时任务Quartz

原文:https://www.cnblogs.com/dslnn/p/10329467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!