Spring+Quartz(定时任务)

    1.通过maven导入需要jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>grainforecast</groupId>
  <artifactId>grainforecast</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>grainforecast Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <!-- spring版本号 -->
        <spring.version>4.2.0.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis.version>3.2.4</mybatis.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.9</log4j.version>
    </properties>
    <dependencies>
        <!-- spring核心包 -->
        <!-- springframe start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>
   <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- springframe end -->

        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis/spring包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- 日志文件管理包 -->
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- log end -->
    </dependencies>
  <build>
      <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>
    </plugins>
    <finalName>grainforecast</finalName>
  </build>
</project>

2. 写一个定时任务

package com.haut.grain.junit.test;

public class QuartzJob {
public void work(){
	System.out.println("执行一个定时任务");
}
}

3.beans.xml配置

 <!-- 工作的bean -->
    <bean id="myJob" class="com.haut.grain.junit.test.QuartzJob" />

    <!-- job的配置开始 -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="myJob" />
        </property>
        <property name="targetMethod">
            <value>work</value>
        </property>
    </bean>
    <!-- job的配置结束 -->

    <!-- 调度的配置开始 -->
    <bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="myJobDetail" />
        </property>
        <property name="cronExpression">
            <value>10 0/1 * * * ?</value>  <!-- Cron表达式“10 */1 * * * ?”意为:从10秒开始,每1分钟执行一次。  -->
        </property>
    </bean>
    <!-- 调度的配置结束 -->

    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger" />

            </list>
        </property>
    </bean>
    <!-- 启动触发器的配置结束 -->
时间: 2024-10-06 13:55:44

Spring+Quartz(定时任务)的相关文章

Spring Quartz定时任务不准时执行

1. 前言 也是前段时间工作上遇到过这样的问题:quartz定时任务没有在预期的时间执行.后来研究了下quartz的机制,查明了原因,这里做个记录和分享. 2. 原因解释 先看一下spring quartz的大致机制或者说原理.quartz任务由一个主线程和线程池中的多个具体的工作线程构成. 主线程是QuartzSchedulerThread, 主要负责获取具体的定时任务和该任务执行的时间(比如可以通过cron expression 得到时间),并分发任务给线程池. 具体的任务由线程池中的工作线

Java spring quartz 定时任务

首先,需要导入quartz 的jar包 ① applicationContext.xml <!-- 轮询任务 --> <import resource="classpath:/conf/quartz/ctmanage-schedule.xml" /> ② ctmanage-schedule.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q

spring quartz 定时任务“Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder”“Checking for available updated version of Quartz”

Failed to load class "org.slf4j.impl.StaticLoggerBinder 需要slf4j-api.jar.slf4j-log4j12.jar Checking for available updated version of Quartz 添加系统属性 System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true"); spring quartz 定

Spring quartz定时任务service注入问题

今天想单元测试一下spring中的quartz定时任务,一顿折腾,到最后总是发现job类里注入的service为null.一开始还以为spring的配置问题,各种找原因,最后还是确定是没有注入. 就去网上搜搜吧.也找出来一些眉目.简单的理解这个原因是job是在quartz中实例化出来的,不受spring的管理.所以就导致注入不进去了.参考这个文章 http://www.tuicool.com/articles/Qjyamu 找着试试的态度,就按照文章里说的.new一个类 public class

spring quartz定时任务配置【基础篇】

一个定时执行的job package dk.spring.quartz; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; /** * 使用spring集成的quartz调度 */ public class QuartzTe

[Spring] - Quartz定时任务 - Annotation

Spring + Quartz可以使用annoation方式: 1.AppJob类: package com.my.quartz.testquartz1; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class AppJob { // @Scheduled(fixedDelay=1000)

一看便知spring+quartz定时任务

这是我经过网上收集然后加上自己的测试写的,以便大家使用 标配:已测 注意需要的包:(在已经配置spring 的情况下) quartz-all-1.6.jar        spring-context-support-4.0.0.jar applicationContext.xml配置文件 <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 当 IoC容器启动时,lazy-init="true"的bean,IoC容器启动时不会实例化该bean

spring quartz定时任务

配置quartz 在spring中需要三个jar包: quartz-1.8.5.jar.commons-collections-3.2.1.jar.commons-logging-1.1.jar 首先要配置我们的spring.xml xmlns 多加下面的内容. [html] view plaincopy xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容. [html]

spring + quartz定时任务,以及修改定时任务

spring4+quartz2.2.3,定时任务弄好了,修改定时任务没折腾起,没找到合适的解决方案. 最终使用库spring-context-support 3.2.17.RELEASE +  quartz 1.8.0解决,quart1.8.x版本的CronTriggerBean弄起来方便 关键代码如下: 1.maven文件 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu