任务调度 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/ ...
随机推荐
- php strtotime 函数UNIX时间戳
int strtotime ( string time [, int now]) 本函数预期接受一个包含英文日期格式的字符串并尝试将其解析为 UNIX 时间戳. 如果 time 的格式是绝对时间则 n ...
- C++异常安全、copy and swap
异常安全的代码是指,满足两个条件 1异常中立性 : 是指当你的代码(包括你调用的代码)引发异常时,这个异常 能保持原样传递到外层调用代码.(异常中立,就是指任何底层的异常都会抛出到上层,也就相当于是异 ...
- beego——模板函数
beego 支持用户定义模板函数,但是必须在 beego.Run() 调用之前,设置如下: func hello(in string)(out string){ out = in + "wo ...
- mysql忽略一些错误代码
模拟的故障,在从库中新建一个库,然后主库新建一个与从库相同名字的库,然后进入下面的show Mysql从库复制故障解决 当show slave status:报错 slave_io_running:y ...
- Android 6.0 Kotlin 蓝牙BLE扫描
package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...
- Android平台利用OpenCL框架实现并行开发初试
http://www.cnblogs.com/lifan3a/articles/4607659.html 在我们熟知的桌面平台,GPU得到了极为广泛的应用,小到各种电子游戏,大到高性能计算,多核心.高 ...
- pt-osc使用方法
pt-osc实战运用 1.安装pt-osc,解压即可用 安装包在:10.135.2.217:data/online/software/percona-toolkit-3.0.12.tar.gz tar ...
- Django 中间件简介
Django 中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. ...
- jQuery多功能日历插件 带事件记录功能
在线演示 本地下载
- [mongodb] MMAP 和wiredTiger 的比较
mongodb 现在有两款存储引擎 MMAPv1 和 WireTiger,当然了除了这两款存储引擎还有其他的存储引擎了. 如: 内存引擎:现在的mongodb 版本中已经有了,主要的cache 服务 ...