spring quartz 定时任务 demo

时间:2022-11-04 07:49:47

spring 的定时任务还是比较好用的,在项目中我使用了这个定时任务。
首先是XML配置

<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>


<!-- 定时任务 -->

<!-- 自动开标定时任务-->
<bean id="automaticPriceOpen" class="com.tydic.portal.utils.AutoPriceOpen" >
<property name="askSheetService" ref=" askSheetService"></property >
<property name="answerSheetService" ref=" answerSheetService"></property >
</bean>

<!-- 统计在线时长任务调度拦截 -->
<bean id="recordLoginTimeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
<property name="targetObject" >
<ref bean="automaticPriceOpen" /><!-- 指定具体拦截哪个定时任务 -->
</property>
<property name="targetMethod" >
<value> autoPriceOpen</value ><!-- 指定要执行的类里面的哪个方法 -->
</property>
</bean>

<!-- 用cron表达式定时任务执行时间 -->
<bean id="automaticPriceOpenCron" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail" >
<ref bean="recordLoginTimeJob" /><!-- 指明要调用哪个任务 -->
</property>
<!-- cron 表达式 -->
<property name="cronExpression" >
<!-- 每天半夜0点0分0秒执行一次-->
<value> 0 * * * * ?</value >
</property>
</bean>

<!-- quartz的调度工厂 调度工厂只能有一个,多个调度任务在list中添加 --><!-- 让spring来自动管理quartz -->
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="triggers" >
<list>
<!-- 所有的调度列表 -->
<ref bean="automaticPriceOpenCron" />
</list>
</property>
</bean >
</beans>

接下来就是我的类

package com.tydic.portal.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tydic.base.vo.JSONResult;
import com.tydic.order.bo.AnswerSheetBO;
import com.tydic.order.bo.AnswerSheetListBO;
import com.tydic.order.bo.AskSheetBO;
import com.tydic.order.service.AnswerSheetService;
import com.tydic.order.service.AskSheetService;


public class AutoPriceOpen {
private Logger logger = LoggerFactory.getLogger(AutoPriceOpen.class);
private AskSheetService askSheetService;
private AnswerSheetService answerSheetService;


public AskSheetService getAskSheetService() {
return askSheetService;
}

public void setAskSheetService(AskSheetService askSheetService) {
this.askSheetService = askSheetService;
}

public AnswerSheetService getAnswerSheetService() {
return answerSheetService;
}
public void setAnswerSheetService(AnswerSheetService answerSheetService) {
this.answerSheetService = answerSheetService;
}

public void autoPriceOpen(){
logger.info("自动开标任务开始 : "+new Date());
//自动开标需要满足的条件
//询价单的状态是o,报价中.
//1 询价单报价的单位超过两家
try {
//1 查询所有未询价中的询价单
JSONResult<List<AskSheetBO>> jsonResult=new JSONResult<List<AskSheetBO>>();
jsonResult=askSheetService.queryAskSheetListByquoting();
List<AskSheetBO> askSheetBOs=jsonResult.getData();

//已到期的询价单
List<AskSheetBO> askSheetOver=new ArrayList<AskSheetBO>();
//2过滤其已过期的
for (AskSheetBO askSheetBO : askSheetBOs) {
Date now=new Date();
Date endtime=askSheetBO.getAnswerEndTime();
if(now.after(endtime)){
askSheetOver.add(askSheetBO);
}
}


//2遍历所有询价单.
for (AskSheetBO askSheet : askSheetOver) {
AnswerSheetListBO answerSheetListBO =answerSheetService.queryAnswerSheetsByaskSheetId(askSheet.getAskSheetId());
List<AnswerSheetBO> answerSheetBOs=answerSheetListBO.getAnswerSheetBOs();


int count = 0;
for (AnswerSheetBO answerSheet : answerSheetBOs) {
if (answerSheet.getAnswerStatus().equals(new Integer(1)) ){
count++;
}
}
AskSheetBO askTemp=new AskSheetBO();
askTemp.setAskSheetId(askSheet.getAskSheetId());
if(count>=2){
askTemp.setAskSheetStatus(1);
}
if(count<2){
askTemp.setAskSheetStatus(4);
}
askSheetService.updateAskSheet(askTemp);

}

} catch (Exception e) {
e.printStackTrace();
logger.error("异常!", e);
}
}

}