咸咸海风

https://github.com/xianxianhaifeng

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
<?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:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
	
	 <!--【定时任务-业务类】-->
	 <bean id="updateById" class = "xxxx.xxxx.xxxx.UpdateById"/>

	 <!--配置【定时任务】,指向具体【业务类】-->
     <bean id = "updateByIdJob" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="updateById" />
        </property>
        <property name="targetMethod">
            <value>execute</value>
        </property>
        <property name="concurrent" value="false" /> <!--指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始-->
     </bean>  
 
	 <!--配置【Cron表达式触发器】,指向具体【定时任务】-->
     <bean id = "updateByIdTrigger" class = "org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="startDelay" value="10000"/>
        <property name="jobDetail">
            <ref bean="updateByIdJob"/>
        </property>
        <property name="cronExpression">
            <value>0 0 1 * * ?</value> <!--凌晨1点触发-->
        </property>
    </bean>
	
	
    <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
    <bean id="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
             <list>
                  <ref bean="updateByIdTrigger"/>
            </list>
        </property>
    </bean>
</beans>

 

定时任务:

 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class UpdateByIdService implements Runnable {

    @Autowired
    private UpdateByIdDao updateByIdDao;

    private static String jobType = "UPDATE_BY_ID";

    public void execute() {
        if (updateByIdDao.checkRunning(jobType)) { // 查看日志表里有没有正在执行的job,如果没有,创建一个线程执行job
            ExecutorService exec = Executors.newCachedThreadPool();
            exec.execute(this);
        }
    }

    public void run() {
        updateByIdDao.startRunning(jobType);// 往日志表里写入一条数据,并打上标识,正在执行
        String notes = "";
        try {
            notes = updateByIdDao.XXXXXX();// 定时任务要刷哪些数据,执行完成后,将日志表里的数据,标识改成,job完成
            logger.info(notes);
        }
        catch (Exception e) {
            notes = e.getMessage();
            logger.info(notes);
            updateByIdDao.setJobException(jobType, "run方法抛异常了");
        }
    }

    @PostConstruct
    public void resetJob() {
        String notes = "刷新过程中服务器关闭";
        updateEqpRegionByRoomRegionDao.resetJob(jobType, notes);
    }
}

  

 

  

 

posted on 2020-05-21 14:32  咸咸海风  阅读(156)  评论(0编辑  收藏  举报