使用spring注解执行定时任务

前言:之前用过quartz框架执行定时调度问题,然后感觉比较麻烦。最近看了下人家的项目,发现都是直接在spring使用注解去执行任务了,所以想记录下来方便以后使用。

废话不多说,直接来干货

1.新建fmb-platform-schedule-server.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:jaxws="http://cxf.apache.org/jaxws"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-lazy-init="true" default-autowire="byType">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath*:/applicationContext-ds.xml"/>
    
    <import resource="classpath:xxx-platform-finance-client.xml"/>

    <bean id="fmb-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="classpath:xxx.properties"/>
    </bean>

    <context:annotation-config/>
    <context:component-scan base-package="cn.xxx"/>
    
    <task:annotation-driven/>
    
</beans>

   如上:要有spring定时执行文件:

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"

   然后要加上task任务扫描注解:

  

<task:annotation-driven/>

   我们扫描的包是:

context:component-scan base-package="cn.xxx"/>

    注:在配置的时候:

default-lazy-init="true" default-autowire="byType"

    autowire的装载方式不能是byName,不然会报装载错误的日志。然后还有这里配置的默认是懒加载,所以在定时器执行时候必须要注解为@Lazyload(false),否则会报错!

2.java文件编写:(在扫描的cn.xxx包下面)

//定时处理到期订单产品
@Component  
@Lazy(false)
public class DueOrderProductQuartz extends BaseService{
	
	@Autowired
	private OrderProductInfoService orderProductInfoService;
	
	//每天凌晨1点执行
	@Scheduled(cron="0 0 1 * * ?")
//	@Scheduled(cron="0/60 * * * * ?")//测试
	public void queryDueOrderProduct() {
		log.info(Utils.getCurrentDate() + "执行DueOrderProductQuartz-->queryDueOrderProduct");
		try {
			int pageNum = 1;
			int pageSize = 20;
			QueryResult<OrderProductInfoDto> ret = orderProductInfoService.queryDueProductRet(1,1,1);
			if(Utils.isNotNull(ret) && Utils.isNotEmpty(ret.getResults())){
				Integer total = ret.getTotal();
				int size = total / pageSize + 1;
				for (int i = 0; i < size; i++) {
					ret = orderProductInfoService.queryDueProductRet(pageNum + i,pageSize,0);
					if(Utils.isNotNull(ret) && Utils.isNotEmpty(ret.getResults())){
						orderProductInfoService.dealDueOrderProduct(ret.getResults());
						Thread.sleep(5000);//暂停5秒让接口处理
					}
				}
			}
		} catch (Exception e) {
			log.error(Utils.getCurrentDate()+":queryDueOrderProduct"+e.getMessage(),e);
		}
	}
	
}

    上面是@Scheduled是spring定时配置,

    注:@Lazyload(false),不能是懒加载,不然定时器不会执行!

   然后下面再普及下常用的cron表达式:

  

CRON表达式    含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 

  4.ok,全局的配置就是这么简单!

猜你喜欢

转载自lopez.iteye.com/blog/2273879