• 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>
    

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

  • 相关阅读:
    [APUE]第十章 信号
    [APUE]第十一章 线程
    android 音乐播放器中播放模式的设计
    php socket 发送HTTP请求 POST json
    php 扫描url死链接 \033[31m ANSI Linux终端输出带颜色
    Redis学习——Redis持久化之RDB备份方式保存数据
    exit与return的区别
    MQ入门总结(一)消息队列概念和使用场景
    微软放缓.NET动态语言开发计划,徐汇区网站设计 狼人:
    构建高性能ASP.NET站点 网站优化需要考虑的方面,徐汇区网站设计 狼人:
  • 原文地址:https://www.cnblogs.com/hhhaisen/p/8087024.html
Copyright © 2020-2023  润新知