• spring-定时任务


    <?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);
        }
    }
    

      

      

  • 相关阅读:
    使用BusyBox制作根文件系统【转】
    嵌入式根文件系统的移植和制作详解【转】
    Linux TTY框架【转】
    Linux内核配置机制(make menuconfig 、Kconfig、Makefile)讲解【转】
    【转】Java如何克隆集合——深度拷贝ArrayList和HashSet
    【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
    【转】JAVA中的浅拷贝和深拷贝
    【转】JAVA字符串格式化-String.format()的使用
    【转】Android API 中文(14) —— ViewStub
    【转】Android 之 下拉框(Spinner)的使用
  • 原文地址:https://www.cnblogs.com/651434092qq/p/12930605.html
Copyright © 2020-2023  润新知