zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    

      

      

  • 相关阅读:
    死锁及预防
    Java中的接口和抽象类
    Jmeter执行java脚本结束时提示:The JVM should have exited but did not.
    dubbo服务的group和version
    Dubbo-admin无法显示Group分组信息
    Python中的变量、引用、拷贝和作用域
    记一次调试python内存泄露的问题
    使用gdb调试python程序
    dstat用法;利用awk求dstat所有列每列的和;linux系统监控
    flask到底能登录多少用户?
  • 原文地址:https://www.cnblogs.com/651434092qq/p/12930605.html
Copyright © 2011-2022 走看看