zoukankan      html  css  js  c++  java
  • spring 任务调度quartz

    简单记录一下spring任务调度quartz的例子

    首先添加包

    quartz-2.2.3.jar

    然后写个简单的TestJob类

    package com.job;
    
    import java.util.Date;
    
    public class TestJob {
    private String name;
    
        public String getName() {
            return name;
        }
    
    
        public void setName(String name) {
            this.name = name;
        }
    
    
        public void excute(){
            
            System.out.println(name+"===定时任务:"+new Date());
        }
    
    }

    再加配置文件application-job.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" >
    
        
        <bean id="testJob" class="com.job.TestJob">
            <property name="name" value="testtest"></property>    <!-- 给testJob里的name属性赋值 -->
        </bean>
        
        <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="testJob"></property>    <!-- 调用testJob -->
            <property name="targetMethod" value="execute" />  <!-- testJob里的execute()方法 -->
            <property name="concurrent" value="false" /><!-- 作业不并发调度 -->  
        </bean>
        
        <bean id="testJobCron" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
              <property name="jobDetail" ref="testJobDetail" />     <!-- 调用 id="testJobDetail"的 bean -->
              <property name="cronExpression" value="0/5 * * * * ?" />  <!-- 每5秒执行一次 -->
        </bean>
        
          <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
          <bean id="schedulerFactoryBean" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
              <property name="triggers">
                  <list>
                       <ref bean="testJobCron" />  <!-- 调用 id="testJobCron"的 bean -->                  
                  </list>
              </property>
          </bean>
    </beans>

    最后应该加web.xml里载一下配置文件

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:application-*.xml</param-value>
      </context-param>
    

      估计大概应该也许或者就差不多可以了吧

  • 相关阅读:
    selenuim
    selenium
    selenium
    selenium
    【OSG】 报错:丢失osg100-osgDB.dll
    无法启动此程序,因为计算机中丢失 zlibd.dll【OSG】
    无法打开mfc120.lib
    jquery中的$("#id")与document.getElementById("id")的区别
    arcgis api for javascript 各个版本的SDK下载
    ajax与servlet交互(通过JSON),JAVA的arraylist传到前端的方法
  • 原文地址:https://www.cnblogs.com/hhhaisen/p/8087024.html
Copyright © 2011-2022 走看看