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;
    }

}

java Quartz定时器任务与Spring 的实现的更多相关文章

  1. java Quartz定时器任务与Spring task定时的几种实现,

    java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置    请查阅   http://www.cnblogs.com/si ...

  2. java定时器,Spring定时器和Quartz定时器

    一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...

  3. Quartz定时器+Spring + @Autowired注入 空指针异常

    在Quartz的定时方法里引用@Autowired注入Bean,会报空指针错误 解决办法: 第一种方法:(推荐,简单,亲测可行) 使用@Resource(name="指定要注入的Bean&q ...

  4. Spring的quartz定时器重复执行二次的问题解决

    Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的ha ...

  5. 基于spring和Quartz定时器

    最近做一个小项目,要每7天去调用webservice获取一次数据.所以就用定时器来完成spring是4.1.6,quartz是2.2.1. 首先配置spring的xml文件.首先定义你要被执行的类 & ...

  6. Spring的quartz定时器同一时刻重复执行二次的问题解决

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  7. spring启动quartz定时器

    在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: &l ...

  8. 实现quartz定时器及quartz定时器原理介绍(转)

    一.核心概念 Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可.1.Job表示一个工作,要执行的具体内容.此接口中只有一个方法void execute(Job ...

  9. java之定时器任务Timer用法

    在项目开发中,经常会遇到需要实现一些定时操作的任务,写过很多遍了,然而每次写的时候,总是会对一些细节有所遗忘,后来想想可能是没有总结的缘故,所以今天小编就打算总结一下可能会被遗忘的小点: 1. pub ...

随机推荐

  1. git 创建tag , 查看tag , 删除tag

    简介  用git了很久了,也喜欢这个版本控制工具,今天来分享下,怎么用命令创建tag,查看tag,删除tag和把本地tag推到远程git服务器上 C:\Users\\WandaPuHuiProject ...

  2. 欢迎访问我的最新个人技术博客http://zhangxuefei.site

    博客已经搬家,欢迎访问我的最新个人技术博客:http://zhangxuefei.site

  3. C语言 · 年龄巧合

    标题:年龄巧合 小明和他的表弟一起去看电影,有人问他们的年龄.小明说:今年是我们的幸运年啊.我出生年份的四位数字加起来刚好是我的年龄.表弟的也是如此.已知今年是2014年,并且,小明说的年龄指的是周岁 ...

  4. IntelJ idea下lombok 不生效的问题(@Builder等注解不生效的问题)解决,lombok Plugin插件安装

    插件安装方式,在设置setting 中找到plugins.在检索框中检索lom,没有的话点击红框内的search in repositories. 点击install进行安装. 记得安装好了重启ide ...

  5. ffmpeg CLI常用命令

    使用-copy参数:  CLI压缩视频时保持音频不变

  6. SpringBoot系统列 4 - 常用注解、拦截器、异常处理

    在前面代码基础上进行改造: 1.SpringBoot常用注解 @SpringBootApplication :指定SpringBoot项目启动的入口,是一个复合注解,由@Configuration.@ ...

  7. Fiddler抓包手机代理配置

    参考链接:https://i.wanz.im/2013/04/30/debugging_http_request_with_fiddler/ http://www.hangge.com/blog/ca ...

  8. iOS开发之--单个页面禁止右滑返回操作

    禁止右滑: if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) ...

  9. sql知识点记录

    order by就是排序. group by就是分组. WHERE语句在GROUP BY语句之前:SQL会在分组之前计算WHERE语句.    HAVING语句在GROUP BY语句之后:SQL会在分 ...

  10. Android查看文件大小

    查看当前路径下的各个挂载模块的大小及剩余量(例如在根目录执行) df #输出 Filesystem Size Used Free Blksize /sys/fs/cgroup .0K /mnt/ase ...