zoukankan      html  css  js  c++  java
  • java中的定时器的实现样例

    1.在pom.xml文件中添加定时器需要的jar包

    <!-- quartz start -->
    <dependency>
          <groupId>org.quartz-scheduler</groupId>
           <artifactId>quartz</artifactId>
           <version>2.2.1</version>
    </dependency>
    <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
    </dependency>

    2.创建applicationContext-quartz.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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <!-- 每隔5秒执行一次:*/5 * * * * ?
    
                     每隔1分钟执行一次:0 */1 * * * ?
    
                     每天23点执行一次:0 0 23 * * ?
    
                     每天凌晨1点执行一次:0 0 1 * * ?
    
                     每月1号凌晨1点执行一次:0 0 1 1 * ?
    
                     每月最后一天23点执行一次:0 0 23 L * ?
    
                     每周星期天凌晨1点实行一次:0 0 1 ? * L
    
                     在26分、29分、33分执行一次:0 26,29,33 * * * ?
    
                     每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
     -->
    
    
        <!-- 要调用的工作类 -->
            <bean id="ylOuterDataJob" class="com.tk.framework.outerdata.YLOuterDataJob"></bean>
            <!-- 定义调用对象和调用对象的方法 -->
            <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                <!-- 调用的类 -->
                <property name="targetObject">
                    <ref bean="ylOuterDataJob"/>
                </property>
                <!-- 调用类中的方法 -->
                <property name="targetMethod">
                    <value>work</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 22 17 * * ?</value> 
                  <!--  每5分钟执行一次 <value>0 */5 * * * ?</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>

     3.在applicationContext.xml文件中引入:

    <!-- 引入定时器相关配置 -->
    <import resource="applicationContext-quartz.xml"/>

    4.编写需要定时调用的java类代码

  • 相关阅读:
    第一轮冲刺团队评分
    意见汇总
    各组对我组的评价
    对各项目评价建议
    【每日Scrum】第十天冲刺
    【每日Scrum】第九天冲刺
    SQL-插入的方法
    Random
    基本测试理论
    web项目工作流程
  • 原文地址:https://www.cnblogs.com/Leonar-do/p/5704513.html
Copyright © 2011-2022 走看看