任务调度 Spring Task 4(二 )
注解和配置文件两种
第一种:配置文件方式
第一步:编写作业类
即普通的pojo,如下:
import org.springframework.stereotype.Service;
@Service
public class TaskJob { public void job1() {
System.out.println(“任务进行中。。。”);
}
}
Java代码
第二步:在spring配置文件头中添加命名空间及描述
<</span>beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
。。。。。。 http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
>
XML代码
第三步:spring配置文件中设置具体的任务
<</span>task:scheduled-tasks>
<</span>task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
</</span>task:scheduled-tasks> <</span>context:component-scan base-package=" cn.springmvc" />
XML代码
第四步:启动测试
package cn.springmvc.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StartConfig {
public static void main(String[] args){
System.out.println("启动spring容器");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
示例
二:annotation 注解配置
第一步:配置测试类
package cn.springmvc.quartz; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component("annotationTask")
public class AnnotationTask {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void job1() {
System.out.println("spring task 注解使用。。。任务进行中");
}
}
测试代码
第二步: 配置配置文件:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
。。。 <</span>task:annotation-driven scheduler="qbScheduler" mode="proxy"/> // 理论上只需要这么一句就可以了
<</span>task:scheduler id="qbScheduler" pool-size="10"/>
配置文件
第三步:启动测试
package cn.springmvc.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class StartConfig {
public static void main(String[] args){
System.out.println("启动spring容器");
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
示例
实现TaskScheduler 接口的ThreadPoolTaskExecutor 继承关系。 
ThreadPoolTaskExecutor 是TaskExecutor 的最主要实现类之一。该类的核心继承关系如下图所示。 
ThreadPoolTaskExecutor 接口扩展了重多的接口,让其具备了更多的能力。要实现异步需要标注@Async 注解:
- AsyncTaskExecutor 增加了返回结果为Future 的submit() 方法,该方法的参数为Callable 接口。相比Runnable 接口,多了将执行结果返回的功能。
- AsyncListenableTaskExecutor 接口允许返回拥有回调功能的ListenableFuture 接口,这样在结果执行完毕是,能够直接回调处理。
任务调度 Spring Task 4(二 )的更多相关文章
- 任务调度 Spring Task 4(一)
深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...
- Spring 使用介绍(十二)—— Spring Task
一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...
- 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job
任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...
- Spring任务调度器之Task的使用
Spring Task提供两种方式进行配置,正如大家所想吧,还是一种是annotation(标注),而另外一种就是XML配置了.但其实这里我觉得比较尴尬,因为任务调度这样的需求,通常改动都是比较多的, ...
- Spring任务调度器之Task的使用(转)
文章转自 http://blog.csdn.net/l454822901/article/details/51829307 最近发现真的凹凸了,spring升级到3后原来已经自带任务调度器了,之前还一 ...
- 基于Spring Task的定时任务调度器实现
在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...
- spring-boot-route(二十)Spring Task实现简单定时任务
Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...
- java Quartz定时器任务与Spring task定时的几种实现,
java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置 请查阅 http://www.cnblogs.com/si ...
- Quartz和Spring Task定时任务的简单应用和比较
看了两个项目,一个用的是Quartz写的定时器,一个是使用spring的task写的,网上看了2篇文章,写的比较清楚,这里做一下留存 链接一.菠萝大象:http://www.blogjava.net/ ...
随机推荐
- Python(面向对象3 ——实例)
这个小东西包括了最近学习的,包括模块.包.序列化.继承.派生.组合的应用举例.整体架构如图: bin是程序入口,core包括了几个主要逻辑,main是主架构,login包括登录功能,register包 ...
- 转:C#访问修饰符
http://www.cnblogs.com/netlyf/archive/2009/12/13/1623103.html
- PL/SQL编程—包
1.PLSQL 中的包就相当于java中的package,主要好处有(1)防止命名污染,(2)功能统一,(3)允许重载,(4)可以隐藏核心代码,(5)最重要的就是断开依赖链. 2.对于一个程序需要大量 ...
- CSS 中文字体 Unicode 编码表
CSS 中文字体 Unicode 编码表 在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误. 为此,在 CSS 直接使用 Unic ...
- python进阶——进程/线程/协程
1 python线程 python中Threading模块用于提供线程相关的操作,线程是应用程序中执行的最小单元. #!/usr/bin/env python # -*- coding:utf-8 - ...
- Spring中的定时调度(Scheduling)和线程池(Thread Pooling)
使用triggers和SchedulerFactoryBean来包装任务 我们已经创建了job details,jobs.我们同时回顾了允许你调用特定对象上某一个方法的便捷的bean. 当然我们仍需要 ...
- IntelliJ IDEA 2017 创建SpringBoot项目, 及.jar没有主清单属性解决办法
1. 创建项目: File >> New >> Spring Initializr 选好 SDK, 及 依赖包(比如 Web >> Web ) . 需要使用 ...
- Spring Cloud 微服务开放平台接口
github源码地址:https://github.com/spring-cloud/spring-cloud-security 前言: 什么是开放平台接口 场景 : 总公司与子公司 对接接口 还有 ...
- CommStringLib
#include "syswatcher/CommStringLib.h" //#include "String16.h" #undef LOG_TAG #de ...
- geoserver源码maven编译相关问题
1.登陆失败跳转404错误 登陆失败后指向的路径为: http://192.168.15.97:8080/hgisserver/web/wicket/bookmarkable/org.geoserve ...