Spring任务调度的几种实现

第一种


使用Spring提供的MethodInvokingJobDetailFactoryBean 代理类,Quartz通过该代理类直接调度任务类的某个函数(业务类是不必进行任何修改的);

<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
					http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
					http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
					http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
					http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	>
	<!-- default-autowire="byName"-->
	
	<!-- 在Spring配置文件中增加本业务类 -->
	<bean id="fsxzTimingJob" class="com.coship.fsxz.utils.TimingJob"></bean>
	
	<!-- 定义任务。在Spring配置文件中配置代理类-->
	<bean id="fsxzTaskJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="fsxzTimingJob" />
		<property name="targetMethod" value="run" />
		<property name="concurrent" value="false" />
	</bean>
		
	<!-- 每天0点15分同步一次      配置一个触发器。在Spring配置文件中配置触发器类CronTriggerBean  -->
	<bean id="fsxzTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="fsxzTaskJob" />
		<property name="cronExpression">
		<!-- 可以使用fsxzPropertyConfigure 配置的bean变量 -->
			<value>${cronEx.fsxz}</value>  
		</property>
	</bean>
	
	<!-- 配置调度器,并且将需要用到的触发器放入 triggers list  -->		
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="fsxzTaskTrigger" />
			</list>
		</property>
		<!-- 自动启动 -->  
        <property name="autoStartup">  
            <value>true</value>  
        </property>
	</bean>	
</beans>

业务类

package com.coship.fsxz.utils;

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

import com.coship.fsxz.mysql.service.IArticleService;
import com.coship.fsxz.mysql.service.IChannelService;

/**
 * 
 * 
 */
public class TimingJob {

	@Autowired
	private IChannelService iChannelService;
	
	@Autowired
	private IArticleService iArticleService;
	
	@Autowired
	private BusinessInfoUtil businessInfo;
	
	/**
	 * 执行定时统计任务
	 * 自行指定方法
	 */
	public void run(){
		try {
			long businessId = -1;
			try {
				businessId = businessInfo.getBusinessId();
			} catch (Exception e) {
				System.out.println("根据参数获取业务ID失败!请检查参数配置!");
				e.printStackTrace();
			}
//				long businessId = 43;
			this.syncDataStart(businessId);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	private void syncDataStart(long businessId) {
		System.out.println("佛山行政同步数据任务开始...");
		iChannelService.addChannleByDivisionCode(businessId);
		iChannelService.addChannelByShortName(businessId);
		iChannelService.addChannelByFolder(businessId);
		
		iArticleService.addArticleByServiceType("企业办事", businessId);
		iArticleService.addArticleByServiceType("个人办事", businessId);
		
		System.out.println("佛山行政同步数据任务结束");
	}
		
}

第二种

业务类继承QuartzJobBean 并且重写executeInternal函数(也可以业务类实现quartz 的job接口,实现 job接口的execute函数)
/**
 * 套餐生成订单任务
 * 
 */
@Transactional
public class PackagesCreateOrderJob extends QuartzJobBean {

	private UorderService uOrderService;

	
	public void setuOrderService(UorderService uOrderService) {
		this.uOrderService = uOrderService;
	}


	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		uOrderService.packagesCreateOrder();

	}

}

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"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- 配置一个套餐生成订单任务 -->
	<bean id="packagesCreateOrderJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass"
			value="com.coship.tvmall.ufresh.quartz.PackagesCreateOrderJob" />
		<property name="jobDataAsMap">
			<map>
				<entry key="uOrderService" value-ref="uOrderService" />
			</map>
		</property>
	</bean>


	<!--触发器 -->
	<bean id="packagesCreateOrderTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="packagesCreateOrderJob" />
		</property>
		<property name="cronExpression">
			<!-- 每个星期三,星期四下午3点30分 -->
			<!-- <value>0 30 15 ? * WED,THU</value> -->
			
			<!-- 每月1号上午1点30分触发  -->      
			<!-- <value>0 30 1 1 * ?</value> -->
			
			<!-- 每天20点触发  -->  
			<value>0 0 20 * * ?</value>
		</property>
	</bean>
	
	<!-- 配置一个套餐订单离线支付任务 -->
	<bean id="packagesOrderPamentJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass"
			value="com.coship.tvmall.ufresh.quartz.PackagesOrderPamentJob" />
		<property name="jobDataAsMap">
			<map>
				<entry key="uOrderService" value-ref="uOrderService" />
			</map>
		</property>
	</bean>


	<!--触发器 -->
	<bean id="packagesOrderPamentTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="packagesOrderPamentJob" />
		</property>
		<property name="cronExpression">
			<!-- 每天21点触发  -->  
			<value>0 0 21 * * ?</value>
		</property>
	</bean>

	<!-- spring触发工厂 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="packagesCreateOrderTrigger" />
				<ref bean="packagesOrderPamentTrigger" />
			</list>
		</property>
	</bean>
</beans>


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值