• spring MVC 设置 定时任务


    在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"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-3.2.xsd" >
    
    <!--     bean definition    HeartBeatTask 启动定时任务的类 要做的什么事
    --> 
    <bean id="heartBeat" class="devp.machine.business.controller.HeartBeatTask" /> <!-- task definition-->
      <task:scheduler id="myScheduler" pool-size="10" />
        <task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="heartBeat" method="run" initial-delay="10000" fixed-delay="5000" />
      </task:scheduled-tasks>
    </beans>

    <task:scheduled> 的参数说明如下:
    initial-delay :  表示第一次运行前需要延迟的时间,单位是毫秒
    fixed-delay  :  表示从上一个任务完成到下一个任务开始的间隔, 单位是毫秒
    fixed-rate    :  表示从上一个任务开始到下一个任务开始的间隔, 单位是毫秒(如果某次任务开始时上次任务还没有结束,那么在上次任务执行完成时,当前任务会立即执行)
    cron           :  cron 表达式,由6个字段组成,依次为 second, minute, hour, day, month, weekday  例如:
                     "0 0 * * * *"                     = the top of every hour of every day.
                     "*/10 * * * * *"                 = every ten seconds.(在 0, 10, 20, 30, 40, 50 秒时运行)
                     "0 0 8-10 * * *"               = 8, 9 and 10 o'clock of every day.
                     "0 0/30 8-10 * * *"          = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 o'clock every day.
                     "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
                     "0 0 0 25 12 ?"              = every Christmas Day at midnight

    要启动的类:

    package devp.machine.business.controller;
    
    import devp.common.utils.IdCreator;
    import devp.machine.api.service.ICenterService;
    import devp.machine.impl.dao.ResourceCountDao;
    import devp.machine.impl.service.CenterService;
    import org.apache.commons.collections4.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    import java.util.Map;
    
    
    public class HeartBeatTask {
    
        /**
         * 定时完成  统计数据资源中心的数据
         */
    
        @Autowired
        private ICenterService centerService;
        @Autowired
        private CenterService centerServiceImpl;
    
        @Autowired
        private ResourceCountDao resourceCountDao;
    

      // run 方法就是启动的类 与配置文件中的方法名一致。
    public void run(){
        
    try{ // 获取查出来的数据 放在新的表中 Map<String, List<Map<String, Object>>> data = centerService.dataCount(); if(!data.isEmpty()){ // 清空表 String deleteSql = "delete from resource_count"; int delete = resourceCountDao.executeSQL(deleteSql); List<Map<String, Object>> entity = data.get("entity"); List<Map<String, Object>> relation = data.get("relation"); List<Map<String, Object>> text = data.get("text"); // 循环 存入数据库表 resource_count 中 Integer entityCount = centerServiceImpl.insertEntityList(entity); Integer relationCount = centerServiceImpl.insertRelationList(relation); Integer textCount =centerServiceImpl.insertTextList(text); } }catch (Exception e){ e.printStackTrace(); } } }
  • 相关阅读:
    MYSQL数据丢失讨论【转】
    MySQL 5.6 新功能之 Index Condition Pushdown (ICP)
    MySQL Binlog 【ROW】和【STATEMENT】选择
    MySQL open_files_limit相关设置
    Python颜色输出和random的学习
    Python 之 【二进制、十进制、八进制、十六进制 】之间的转换【转】
    初识 MySQL 5.5 新功能、参数
    Oracle Inventory关键词解释
    plsql 输出当月的所有日期
    ORA29279: SMTP permanent error: 550 XXX@XX.com... No such user
  • 原文地址:https://www.cnblogs.com/yishuo/p/13030189.html
Copyright © 2020-2023  润新知