天天看点

Spring整合quartz定时任务

如有需要可以加我Q群【308742428】大家一起讨论技术,提供技术支持。

后面会不定时为大家更新文章,敬请期待。

今天我们来分享一波在spring项目使用quartz做定时任务。

首先我这里的项目已经是一个可以跑起来的完整项目,web.xml里面的配置我就不贴出来了。

1.新建一个类ConfigConsts

我们用来放cron表达式:

更多cron表达式

public abstract class ConfigConsts {
 
    /** 30分钟执行一次 */
    public static final String quartzInterval = "0 0/30 * * * ?";
 
    /** 每天凌晨1:30分执行*/
    public static final String quartzCustomerInterval = "0 30 1 * * ?";
 
 
    /** 每天凌晨1:00分执行*/
    public static final String quartzMaterialInterval = "0 0 1 * * ?";
    
    /** 每天凌晨2:00分执行*/
    public static final String quartzSupplierInterval = "0 0 2 * * ?";
 
   
}           

2.新建一个QuartzHandler类来实现我们的代码逻辑

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.apache.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aowang.framework.daoComp.Jconn;
import com.aowang.framework.daoComp.jComp;
import com.aowang.utils.APIUtils;
import com.aowang.utils.Constants;
import com.aowang.utils.DateUtil;
import com.aowang.utils.Utils;
import com.aowang.utils.http.HttpClientResult;
import com.aowang.utils.http.HttpClientUtils;
 
import net.sf.json.JSONArray;
 
/**
 * 描述:定时任务调度
 * 
 * @author dsn
 * @date 2020年8月28日 下午2:57:41
 */
public class QuartzHandler {
 
    private static final Logger logger = LoggerFactory.getLogger(QuartzHandler.class);
 
    private static boolean isFirst = true;// 第一次执行定时任务
    @Autowired
    private Jconn jcon; // 数据库组件
 
    private Map<String, Object> map = new HashMap<String, Object>();
 
    private static String startDate = "20130101";
 
    /**
     * Description:定时执行拉取客户主数据处理 <BR>
     * 
     * @author dsn
     * @date 2020年8月28日 下午11:57:28
     * @version 1.0
     * @throws Exception
     */
    public void run4Customer() throws Exception {
        // 定时执行1
        System.out.println("定时任务开启----------------------------");
        //这里面就可以写代码逻辑
 
    }
 
 
}           

3.新建一个application-quartz.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 要调用的工作类 -->      
    <bean id="mainClass" class="com.aowang.quartz.QuartzHandler">
    </bean>
    
     <!-- 任务配置列表 -->  
    <bean id="task_customer"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 指定任务类 -->
        <property name="targetObject" ref="mainClass" />
        <!-- 指定任务执行的方法 -->
        <property name="targetMethod" value="run4Customer" />
    </bean>
 
    <!-- 将运行时间策略常量放入bean池 -->
    <bean id="interval_customer" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> 
        <property name="staticField" value="com.aowang.quartz.ConfigConsts.quartzInterval"/> 
    </bean> 
 
    <!-- 触发器配置  时间指定 -->  
    <bean id="trigger_customer"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="task_customer" />
        <property name="cronExpression" ref="interval_customer" />
    </bean>
 
 
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->  
    <bean lazy-init="true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 触发器列表    -->  
                <ref bean="trigger_customer" />
 
                
            </list>
        </property>
    </bean>
 
</beans>           

4.在applicationContent.xml中引入第3步新建的xml

其实就是这么的简单,完事。