zoukankan      html  css  js  c++  java
  • Spring整合quartz定时任务

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

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

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

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

    1.新建一个类ConfigConsts

    我们用来放cron表达式:

    更多cron表达式

    package com.aowang.quartz;
     
     
     
    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类来实现我们的代码逻辑

    package com.aowang.quartz;
     
    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

    <?xml version="1.0" encoding="UTF-8"?>
    <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

    <import resource="application-quartz.xml"/>
    

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

      

      

     

  • 相关阅读:
    C#中Equals和= =(等于号)的比较)(转载)
    C# 控制台应用程序输出颜色字体
    c#获取当前运行程序所在的目录
    java环境配置
    c#随机产生颜色
    Git学习
    git删除所有提交历史记录
    git忽略项gitegnore配置
    不搭建git服务器对git仓库进行局域网内共享多人合作开发项目
    搭建Git服务器-SCM-Manager
  • 原文地址:https://www.cnblogs.com/dsn727455218/p/13588291.html
Copyright © 2011-2022 走看看