zoukankan      html  css  js  c++  java
  • java Quartz定时器任务与Spring 的实现

    1.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:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        
            <property name="quartzProperties">
                <props>
                    <prop key="org.quartz.scheduler.instanceName">CRMscheduler</prop>
                    <prop key="org.quartz.scheduler.instanceId">AUTO</prop>
                    <!-- 线程池配置 -->
                    <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
                    <prop key="org.quartz.threadPool.threadCount">3</prop>
                    <prop key="org.quartz.threadPool.threadPriority">5</prop>
                    <!-- JobStore 配置 -->
                    <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
                    <!-- 集群配置 -->
                    <prop key="org.quartz.jobStore.isClustered">true</prop>
                    <prop key="org.quartz.jobStore.clusterCheckinInterval">15000</prop>
                    <prop key="org.quartz.jobStore.maxMisfiresToHandleAtATime">1</prop>
                    <!-- 数据源配置 使用DBCP连接池 数据源与dataSource一致 -->
                    <prop key="org.quartz.jobStore.dataSource">myDS</prop>
                    <prop key="org.quartz.dataSource.myDS.driver">${db.driverClassName}</prop>
                    <prop key="org.quartz.dataSource.myDS.URL">${db.quartz_park.url}</prop>
                    <prop key="org.quartz.dataSource.myDS.user">${db.username}</prop>
                    <prop key="org.quartz.dataSource.myDS.password">${db.password}</prop>
                    <prop key="org.quartz.dataSource.myDS.maxConnections">10</prop>
                    <prop key="org.quartz.jobStore.misfireThreshold">120000</prop>
                </props>
            </property>

            <property name="schedulerName" value="CRMscheduler" />
            <property name="startupDelay" value="30" />
            <!-- 自动启动 -->
            <property name="autoStartup">
                <value>true</value>
            </property>
            <property name="overwriteExistingJobs">
                <value>true</value>
            </property>
            
            
            <property name="triggers">
                <list>
                    <ref local="parkCouponsIssueTrigger"/><!-- 更新优惠券管理的购买表的失效时间字段 -->        
                    <ref local="parkCardOverTimeTrigger"/><!-- 更新超时的卡的信息-->        
                    <ref local="createOverTimeTicketTrigger"/><!-- 创建超时券-->    
                    <ref local="parkCouponsIssueBackupsTrigger"/><!-- 备份优惠券管理的购买表的半年数据-->        
                </list>
            </property>
            <property name="applicationContextSchedulerContextKey" value="applicationContext" />
        </bean>    
        
        <!--定时同步parkCouponsIssue表的数据 start yxz -->
        <bean id="parkCouponsIssueTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="parkCouponsIssueJobDetail"/>
            </property>
            <property name="cronExpression">
                <value>0 * * * * ?</value>
            </property>
        </bean>

        <bean id="parkCouponsIssueJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
            <property name="jobDataAsMap">
                <map>
                    <entry key="targetObject" value="parkCouponsIssueService" />
                    <entry key="targetMethod" value="updateInvalidTimeData" />
                </map>
            </property>
            <property name="concurrent" value="false" />
        </bean>
        <!--定时同步communitySynchData表的数据 end -->
        
        <!--定时备份TicketDetails(核销记录)表的数据 start 2017/11/06 yxz-->
        <bean id="parkCouponsIssueBackupsTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="parkCouponsIssueBackupsJobDetail"/>
            </property>
            <property name="cronExpression">
                <value>0 0/1 * * * ?</value>
            </property>
        </bean>

        <bean id="parkCouponsIssueBackupsJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
            <property name="jobDataAsMap">
                <map>
                    <entry key="targetObject" value="ticketDetailsBackupsService" />
                    <entry key="targetMethod" value="updateInvalidTimeData" />
                </map>
            </property>
            <property name="concurrent" value="false" />
        </bean>
        
        <!--定时同步communitySynchData表的数据 end 2017/11/06 yxz-->
        
        
        <!--定时同步parkCouponsIssue表的数据 start -->
        <bean id="parkCardOverTimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="parkCardOverTimeJobDetail"/>
            </property>
            <property name="cronExpression">
                <value>0 * * * * ?</value>
            </property>
        </bean>

        <bean id="parkCardOverTimeJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
            <property name="jobDataAsMap">
                <map>
                    <entry key="targetObject" value="parkMonthCardInfoAction" />
                    <entry key="targetMethod" value="updateOverTimeCard" />
                </map>
            </property>
            <property name="concurrent" value="false" />
        </bean>
        
        <!--定时同步parkCouponsIssue表的数据 start -->
        <bean id="createOverTimeTicketTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="createOverTimeTicketJobDetail"/>
            </property>
            <property name="cronExpression">
                <value>0 * * * * ?</value>
            </property>
        </bean>

        <bean id="createOverTimeTicketJobDetail" class="com.ivchat.common.bean.quartz.CommJobDetailBean">
            <property name="jobDataAsMap">
                <map>
                    <entry key="targetObject" value="parkDetailsAction" />
                    <entry key="targetMethod" value="createOverTimeTicket" />
                </map>
            </property>
            <property name="concurrent" value="false" />
        </bean>
        
        <!--定时同步communitySynchData表的数据 end -->
    </beans>

    2.作业类1

    package com.ivchat.common.bean.quartz;

    import org.springframework.scheduling.quartz.JobDetailBean;

    /**
     * JOB明细对象
     * @author 居里智能
     *
     */
    public class CommJobDetailBean extends JobDetailBean {
        private boolean concurrent = false;

        @Override
        public void afterPropertiesSet() {
            try{
                if (concurrent){
                    this.setJobClass(CommDetailQuartzJobBean.class);
                }else{
                    this.setJobClass(StatefulMethodInvokingJob.class);
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }
            
            // TODO Auto-generated method stub
            super.afterPropertiesSet();
        }

        public boolean isConcurrent() {
            return concurrent;
        }

        public void setConcurrent(boolean concurrent) {
            this.concurrent = concurrent;
        }
        
        public boolean getConcurrent() {
            return concurrent;
        }
    }
    3.作业类2

    package com.ivchat.common.bean.quartz;

    import java.lang.reflect.Method;

    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.scheduling.quartz.QuartzJobBean;

    import com.ivchat.common.util.SpringUtil;

    /**
     * JOB类,到时间即时执行,有可能多个JOB并发执行
     * @author 居里智能
     *
     */
    public class CommDetailQuartzJobBean extends QuartzJobBean implements ApplicationContextAware{

        private String targetObject;
        private String targetMethod;
        private ApplicationContext applicationContext;
        @Override
        public void executeInternal(JobExecutionContext context)
                throws JobExecutionException {
            Object otargetObject = null;
            Method m = null;
            
            try {
                otargetObject = applicationContext.getBean(targetObject);
                
                m = otargetObject.getClass().getMethod(targetMethod,
                        new Class[] {});
                m.invoke(otargetObject, new Object[] {});
            } catch (Exception e) {
                throw new JobExecutionException(e);
            }
        }

        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }

        public ApplicationContext getApplicationContext() {
            return applicationContext;
        }

        public String getTargetObject() {
            return targetObject;
        }

        public void setTargetObject(String targetObject) {
            this.targetObject = targetObject;
        }

        public String getTargetMethod() {
            return targetMethod;
        }

        public void setTargetMethod(String targetMethod) {
            this.targetMethod = targetMethod;
        }

    }

  • 相关阅读:
    mask_rcnn调用训练好的模型
    class list
    dataset prepare
    ZT笔记本配置mask_rcnn环境
    mask_rcnn参数
    TorchVisionV0.9中引入的ML模型
    犀牛 水杯练习
    模型的部分ifc
    Error log
    yolov5训练日志
  • 原文地址:https://www.cnblogs.com/yy00/p/7800128.html
Copyright © 2011-2022 走看看