quartZ定时任务

MyBatis与Spring整合及定时任务实践

mybatis配置sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
	<!-- 全局setting配置,根据需要添加 -->
	
	<!-- 配置别名 -->
	<typeAliases>
		<!-- 批量扫描别名 -->
		<package name="com.snt.crm.schedule.intf.entity"/>
	</typeAliases>

</configuration>

Spring相关配置

applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 配置数据源 ,dbcp -->

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="${jdbc.maxactive}" />
		<property name="maxIdle" value="${jdbc.maxidle}" />
		<property name="defaultAutoCommit" value="${jdbc.defaultautocommit}" />  
        <property name="defaultReadOnly" value="${jdbc.defaultreadonly}" />    
        <property name="testOnBorrow" value="${jdbc.testonborrow}"/>  
        <property name="validationQuery" value="${jdbc.validationquery}"/>  
	</bean>
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
	</bean>
	<!-- mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
		<property name="basePackage" value="com.snt.crm.schedule.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

</beans>

dubbo-consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://code.alibabatech.com/schema/dubbo  
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="crm_schedule" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry protocol="zookeeper" address="${zookeeper.address}" />
	
	<!-- 监控中心配置,protocol="registry",表示从注册中心发现监控中心地址 -->
	<dubbo:monitor protocol="registry"/>
	
	<!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
	<dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="${schedule_dubbo_port}" />
	
	<!-- 工单接口 -->
	 <dubbo:reference interface="com.snt.crm.provision.intf.ProvisionOrderIntf" id="provisionOrderIntf" check="false"/>
	<!--月度扣费接口  --> 
	 <dubbo:reference interface="com.snt.crm.business.intf.AccountInfoIntf" id="accountInfoIntf" check="false"/>
	<!-- 历史账单接口 -->
	<dubbo:reference interface="com.snt.crm.business.intf.HistoryBillsIntf"  id="historyBillsIntf" check="false"/>
	<!-- 未生效订购信息 -->
	<dubbo:reference interface="com.snt.crm.business.intf.SubscriptionOrderIntf"  id="subscriptionOrderIntf" check="false"/>
	 
	</beans>  

quartz-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

 	<!-- 调度器SchedulerFactoryBean --> 
    <bean name="startQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
            <list> 
                <ref bean="jobProvisionOrderTrigger"/> 
                <ref bean="jobMonthlyFeeTrigger"/>
                <ref bean="jobMonthlyFeeFailTrigger"/>
                <ref bean="jobHistoryBillTrigger"/>
            </list> 
        </property> 
    </bean> 
     
    <!-- 触发器(CronTriggerFactoryBean) -->   
    <bean id="jobProvisionOrderTrigger"   
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">   
        <property name="jobDetail" ref="quartzProvisionOrderJobMethod"/>   
        <property name="cronExpression">  
            <value>0/10 * * * * ?</value><!-- 每隔10秒执行一次--> 
        </property>   
    </bean>   
 
     <!-- 任务调度方法 -->   
    <bean id="quartzProvisionOrderJobMethod"   
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
        <property name="targetObject" ref="quartzProvisionOrderJob"/>   
        <property name="targetMethod" value="work"/> 
        <!-- 禁止并发 -->   
        <property name="concurrent" value="false"/> 
    </bean> 
    
     <!--触发器月度扣费   -->
     <bean id="jobMonthlyFeeTrigger"   
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">   
        <property name="jobDetail" ref="quartzMonthlyFeeJobMethod"/>   
        <property name="cronExpression">  
            <value>0 0 0 1 * ?</value><!-- 每月1号00:00:00执行--> 
        </property>   
    </bean>  
    
    <!--触发器月度扣费方法  -->
    <bean id="quartzMonthlyFeeJobMethod"   
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
        <property name="targetObject" ref="quartzMonthlyFeeJob"/>   
        <property name="targetMethod" value="work"/> 
        <!-- 禁止并发 -->   
        <property name="concurrent" value="false"/> 
    </bean>
    
     <!--触发历史账单  -->
     <bean id="jobHistoryBillTrigger"   
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">   
        <property name="jobDetail" ref="quartzHistoryBillJobMethod"/>   
        <property name="cronExpression">  
            <value>0 0 2 2 * ?  </value><!-- 每月2号凌晨2点执行--> 
        </property>   
    </bean>  
    
    <!--触发历史账单 -->
    <bean id="quartzHistoryBillJobMethod"   
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
        <property name="targetObject" ref="quartzHistoryBillJob"/>   
        <property name="targetMethod" value="work"/> 
        <!-- 禁止并发 -->   
        <property name="concurrent" value="false"/> 
    </bean>

   <!--月度扣费轮询未成功的  -->
    <bean id="jobMonthlyFeeFailTrigger"   
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">   
        <property name="jobDetail" ref="quartzMonthlyFeeFailJobMethod"/>   
        <property name="cronExpression">  
            <value>0 5 */1 * * ?</value><!-- 每隔一小时执行一次--> 
        </property>   
    </bean>  
    
    <!--触发器月度扣费轮询未成功方法  -->
    <bean id="quartzMonthlyFeeFailJobMethod"   
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
        <property name="targetObject" ref="quartzMonthlyFeeFailJob"/>   
        <property name="targetMethod" value="work"/> 
        <!-- 禁止并发 -->   
        <property name="concurrent" value="false"/> 
        
    </bean>    <!-- 自定义任务(Job) -->   
    <bean id="quartzHistoryBillJob" class="com.snt.crm.schedule.historyBill.QuartzHistoryBillJob" /> 
    <bean id="quartzProvisionOrderJob" class="com.snt.crm.schedule.provision.QuartzProvisionOrderJob" /> 
    <bean id="quartzMonthlyFeeJob" class="com.snt.crm.schedule.monthlyfee.QuartzMonthlyFeeJob"/>
    <bean id="quartzMonthlyFeeFailJob" class="com.snt.crm.schedule.monthlyfeefail.QuartzMonthlyFeeFailJob"/>
    </beans>

springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 配置要扫描的包 -->
	<context:component-scan base-package="com.snt.crm">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	</context:component-scan>
	
	<!-- 采用注释的方式配置bean -->
	<mvc:annotation-driven />
	
	<!-- 读入配置属性文件 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
           <list>  
              <!-- 这里支持多种寻址方式:classpath和file -->  
              <!-- 推荐使用file的方式引入,这样可以将配置和代码分离,${resources_path}是获取VM arguments里的-D的值 -->  
              <value>file:${resources_path}/jdbc.properties</value>
              <value>file:${resources_path}/service.properties</value>
              <value>file:${resources_path}/activemq.properties</value>
            </list>
        </property>  
    </bean>
    
	<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />	
     
	<import resource="classpath:spring/applicationContext-dao.xml" />
	<import resource="classpath:spring/dubbo-consumer.xml" />
	<import resource="classpath:spring/quartz-config.xml" />

</beans>

举两个例子,实现定时任务

package com.snt.crm.schedule.historyBill;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.snt.crm.business.intf.HistoryBillsIntf;

/**
 * @描述:月度出账 定时任务定时任务
 */
public class QuartzHistoryBillJob {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
@Autowired
private HistoryBillsIntf historyBillsIntf; 
public void work(){
	log.info("----------月度出账定时任务启动-----------");	
	historyBillsIntf.saveHistoryBill();//直接调用业务(如下)
}
}



@Service("historyBillsIntf")
public class HistoryBillsIntfImpl implements HistoryBillsIntf {

	@Autowired
	private HistoryBillsService historyBillsService;
   
    private final Logger log = LoggerFactory.getLogger(HistoryBillsIntfImpl.class);

    @Override
	public void saveHistoryBill() {
		log.info("统计上月的话单数据");
		historyBillsService.saveStatisticsBill();
	}
}

例子2

package com.snt.crm.schedule.monthlyfee;

import org.springframework.beans.factory.annotation.Autowired;

import com.snt.crm.business.intf.AccountInfoIntf;

/**
 * @描述:月度扣费定时任务定时任务
 * @作者:snt1172 chenjianlin
 * @版本:v1.1
 * @创建时间:2016年11月23日 下午7:06:55
 */
public class QuartzMonthlyFeeJob {
	@Autowired
	private AccountInfoIntf accountInfoIntf;

	public void work() {
		System.out.println("----------月度扣费定时任务启动-----------");
		accountInfoIntf.monthlyChargeProFee(); //直接调用相应要处理的业务
	}
}

 

转载于:https://my.oschina.net/erainm/blog/1186791

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值