zoukankan      html  css  js  c++  java
  • spring定时器,定时器一次执行两次的问题

    Spring 定时器

    方法一:注解形式

    配置文件头加上如下:

    xmlns:task="http://www.springframework.org/schema/task"
    
    http://www.springframework.org/schema/task 
    http://www.springframework.org/schema/task/spring-task.xsd
    需要 quartz 包
    <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.2</version>
    </dependency>
    
    
     <!-- 扫描包 -->
    <context:component-scan base-package="com.hehe.content.job" />
        <!-- 启动定时任务  -->
    <task:annotation-driven/>
    @Component
    public class MyTask {
        @Scheduled(cron="0 0 2 * * ?") // 每天凌晨2点执行,该方法不能有返回值
        public void taskCycle(){  
            System.out.println("======================");  
        }     
    }

    方法二: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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.1.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd">
    
        
        <!-- 启动定时任务  -->
         <!-- <task:annotation-driven/> 
         <context:component-scan base-package="com.hehe.content.job" />  -->
         
        <!-- 要调用的工作类 -->  
            <bean id="quartzJob" class="com.hehe.content.job.MyTask"></bean>  
            <!-- 定义调用对象和调用对象的方法 -->  
            <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
                <!-- 调用的类 -->  
                <property name="targetObject">  
                    <ref bean="quartzJob" />  
                </property>  
                <!-- 调用类中的方法 -->  
                <property name="targetMethod">  
                    <value>taskCycle</value>  
                </property>  
            </bean>  
            <!-- 定义触发时间 -->  
            <bean id="doTime"  
                class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
                <property name="jobDetail">  
                    <ref bean="jobtask" />  
                </property>  
                <!-- cron表达式 -->  
                <property name="cronExpression">  
                    <value>0 0 2 * * ?</value>     
                </property>  
            </bean>  
            
            <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->  
            <bean id="startQuertz" lazy-init="false" autowire="no"  
                class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
                <property name="triggers">  
                    <list>  
                        <ref bean="doTime" />  
                    </list>  
                </property>  
            </bean>  
            
        
        
    </beans>

    问题: 每次任务到点都执行两次!!!!!!

    网上查了好多资料 ,都不是我的情况,后来发现是我的项目在启动的时候每次都会加载两次,原来是eclipse 中tomcat配置的问题

    图中若选择的是第二个,项目会启动两次,这就导致了后面的定时器执行了两次。最后改为了第一选项就好了。

    我在腾讯微视玩短视频 搜索用户  “lei9527” ,可以相互关注下哈

  • 相关阅读:
    Mac ssh 免密码登录 Mac 或者 Linux
    windows系统下将nginx作为系统服务启动
    eclipse的Git忽略某些不需要提交的文件
    spring boot 日志介绍 以及 logback配置示例
    nginx for Windows
    Eclipse 中Git的使用及如何解决冲突
    jquery是如何封装的
    SpringMVC接收json对象
    JSP中使用JSTL,并且解决XSS安全问题
    timestamp Invalid default value
  • 原文地址:https://www.cnblogs.com/c9999/p/6171062.html
Copyright © 2011-2022 走看看