Quartz的使用和springTask的使用

两个东西都是定时任务

先说Quartz 

需要jar包quartz

下载依赖 

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>

新建一个spring-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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <!-- 添加调度的任务bean 配置对应的class-->
  <bean id="myScheduleTest" class="com.osy.task.MyScheduleTest" />这个是我们要执行任务的类
  <!--配置调度具体执行的方法-->
  <bean id="myPrintDetail"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="myScheduleTest" />执行的类名
    <property name="targetMethod" value="mySchedule" />执行的方法
    <property name="concurrent" value="false" />
  </bean>
  <!--配置调度执行的触发的时间-->
  <bean id="myPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="myPrintDetail" />
    <property name="cronExpression">
      <!-- 每天早上8点执行任务调度 -->
      <value>0/5 * * * * ? </value>
    </property>
  </bean>
  <!-- quartz的调度工厂 调度工厂只能有一个,多个调度任务在list中添加 -->
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
      <list>
         <!-- 所有的调度列表-->
        <ref local="myPrintTrigger" />
      </list>
    </property>
  </bean>
</beans>

定时任务的类

package com.osy.task;

/**
 * * @author 作者 E-mail: * @date 创建时间:2018年7月23日 下午2:12:19 * @version 1.0
 * * @parameter * @since * @return
 */
public class MyScheduleTest {
    public void mySchedule(){
        System.out.println("spring-quqrz的使用");
    }
}

当然了 此配置文件需要在web.xml里面加入

    <!-- Spring MVC servlet -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml,classpath:spring-quartz.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

然后执行 返回结果

scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

如果出现异常以上 有可能是你的quartz的版本低了 要去下载一个高一点的版本

最后的返回结果

[org.quartz.core.QuartzSchedulerThread] - batch acquisition of 0 triggers
[org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.myPrintDetail
spring-quqrz的使用
[org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
[org.quartz.core.QuartzSchedulerThread] - batch acquisition of 0 triggers
[org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.myPrintDetail
spring-quqrz的使用
[org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers
[org.quartz.core.JobRunShell] - Calling execute on job DEFAULT.myPrintDetail
spring-quqrz的使用
[org.quartz.core.QuartzSchedulerThread] - batch acquisition of 1 triggers

接下来说spring-task的使用

在springmvc文件里面加入spring-task的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
    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-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                           http://www.springframework.org/schema/task  
                        http://www.springframework.org/schema/task/spring-task-3.1.xsd "
                        >
    
    <!-- 激活controller注解标签的 -->
    <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>
    <context:annotation-config/> 
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
     <!-- <context:component-scan base-package="com.osy" use-default-filters="false">
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
    </context:component-scan>  -->
    <context:component-scan base-package="com.osy"/>
    <task:annotation-driven scheduler="scheduler" executor="executor"/> 
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" />    <!-- JSON转换器 -->
            </list>
        </property>
    </bean>
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>  
    <mvc:resources location="/frame/" mapping="/frame/**"/>
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
</beans>

 

定时任务

package com.osy.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/** * @author  作者 E-mail: * @date 创建时间:2018年7月23日 上午11:48:45 * @version 1.0 * @parameter  * @since  * @return  */
@Component
public class FlightTrainTask {
    @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
    public void taskCycle() {
        System.out.println("使用SpringMVC框架配置定时任务");
    }
}

执行返回结果

使用SpringMVC框架配置定时任务

再说一哈时间的配置

http://blog.csdn.net/supingemail/article/details/22274279 直接去这个博客看吧

最后说一哈这两个的区别

精确度和功能 

Quartz可以通过cron表达式精确到特定时间执行,而TimerTask不能。

Quartz拥有SpringTask所有的功能,而TimerTask则没有。 

任务类的数量 

Quartz每次执行都创建一个新的任务类对象。 SpringTask则每次使用同一个任务类对象。 

对异常的处理 Quartz的某次执行任务过程中抛出异常,不影响下一次任务的执行,当下一次执行时间到来时,定时器会再次执行任务。 SpringTask不同,一旦某个任务在执行过程中抛出异常,则整个定时器生命周期就结束,以后永远不会再执行定时器任务。

总结:一般情况下还是使用Quartz

 

转载于:https://www.cnblogs.com/oushiyang/p/9354703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值