大數據

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

其實就是這麼的簡單,完事。

Leave a Reply

Your email address will not be published. Required fields are marked *