`
拓子轩
  • 浏览: 204983 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring的定时任务和异步方法

    博客分类:
  • java
阅读更多

一、使用示例

1. 创建java工程,引入spring相关的jar包(略)

2. 在spring配置文件中加入如下配置:

    <task:annotation-driven/>

    <context:component-scan base-package="com.tuozixuan.task"/>

 3. 编写如下示例代码并运行

 

package com.tuozixuan.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("myTask")
public class MyTask
{
    @Scheduled(cron="0 0/5 10-12 * * ?")
    public void execute()
    {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("[" + date + "]execute task...");
    }
    
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
    }
}

 

 

二、spring定时任务详解

1. @Scheduled配置

    @Scheduled(fixedRate=1000),fixedRate表明这个方法需要每隔指定的毫秒数进行周期性地调用。

    @Scheduled(fixedDelay=2000),fixedDelay用来指定方法调用之间的间隔(一次调用完成与下一次调用开始之间的间隔)。

    @Scheduled(cron="0 0/5 10-12 * * ?") cron用来指定这个方法在什么时间调用。

 

    cron设置:

    1)秒(0-59)

    2)分钟(0-59)

    3)小时(0-23)

    4)月份中的日期(0-31)

    5)月份(1-12或JAN-DEC)

    6)星期中的日期(1-7或SUN-SAT)

    7)年份(1970-2099)

    每个元素都可以显式地指定值(如3)、范围(2-5)、列表(2,4,7)或者通配符(如*)。

    月份汇总的日期和星期中的日期是互斥的,可以通过设置一个问号(?)来表明你不想设置的那个字段

    

三、异步方法

 1. 在Bean的方法上使用@Async进行注解,就可以使该方法成为一个异步执行的方法,代码示例如下:

 

package com.tuozixuan.task;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component("myTask")
public class MyTask
{
    
    @Async
    public void executeAsync()
    {
        System.out.println("async execute task start...");
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println("async execute task end...");
    }
    
    public static void main(String[] args)
    {

        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
        
        MyTask myTask =  context.getBean("myTask", MyTask.class);
        myTask.executeAsync();
        System.out.println("main...");
    }
}

 

分享到:
评论

相关推荐

    Java多线程之定时任务 以及 SpringBoot多线程实现定时任务——异步任务

    1. SpringBoot 自定义线程池以及多线程间的异步调用(@Async、@EnableAsync) 2.Java多线程之定时任务 以及 SpringBoot多线程实现定时任务 3.@EnableScheduling 与 @Scheduled

    【java框架】SpringBoot(4)--SpringBoot实现异步、邮件、定时任务(csdn)————程序.pdf

    【java框架】SpringBoot(4)--SpringBoot实现异步、邮件、定时任务(csdn)————程序

    Java课程实验 Spring Boot 任务管理(源代码+实验报告)

    1.在Spring Boot中,你可以使用@Scheduled注解来创建定时任务。将@Scheduled注解与方法一起使用,指定任务执行的时间表达式。 2.使用Spring的TaskScheduler: Spring提供了TaskScheduler接口和相关实现,用于任务...

    Springboot 定时任务(task)

    使用springboot 构建的 spring task 定时任务,采用异步任务形式,防止任务堵塞.

    浅谈spring 线程异步执行

    主要介绍了浅谈spring 线程异步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    java 中Spring task定时任务的深入理解

    在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提供了一种抽象。这就意味着spring容许你使用其他的定时任务框架,当然spring自身也提供了...

    SpringBoot专题学习Part29:SpringBoot的Async异步任务、Scheduled定时任务、mail邮件发送任务

    一、Async异步任务 在Java应用中绝大多数情况下默认都是通过同步的方式来实现交互处理的 但在处理与第三方系统交互的时候 容易造成响应迟缓的情况 可以使用多线程来完成此类任务 但其实 在Spring 3.x之后 已经内置了...

    springboot schedule 解决定时任务不执行的问题

    主要介绍了springboot schedule 解决定时任务不执行的问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

    Spring-Reference_zh_CN(Spring中文参考手册)

    Deprecated的类和方法 2.7.1.4. Apache OJB 2.7.1.5. iBatis 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. 控制反转容器 3.1. 简介 3.2. 容器和bean的基本原理 3.2.1. 容器 3.2.1.1. 配置元数据 3.2.2. ...

    Spring 2.0 开发参考手册

    6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点(pointcut) 6.2.4. 声明通知 6.2.5. 引入(Introductions)...

    尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),...

    Job Plus项目是基于SpringBoot+Vue的轻量级定时任务管理系统+源代码+文档说明

    7. 强自定义:支持在线配置定时任务请求类型、请求路径、请求参数、Cron表达式,即时生效; 8. 动态控制:支持动态修改任务状态、启动/停止任务,以及终止运行中任务,即时生效; 9. 执行策略:支持丰富的执行策略,...

    Spring中文帮助文档

    6.1.2. Spring AOP的功能和目标 6.1.3. AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点(pointcut) 6.2.4. 声明通知 6.2.5. 引入(Introduction) 6.2.6. ...

    基于Vue+SpringCloud博客的设计与实现 有论文

    用户会员中心:SVIP与VIP,定时任务/RabbitMQ延迟队列/登录验证三种判定会员截止时间到期用邮箱去提醒 用户支付中心:我的钱包和支付宝支付以及打印我的账单,内网穿透获得异步通知作为结果判定标志,原始支付的普通...

    spring chm文档

    6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个切入点(pointcut) 6.2.4. 声明通知 6.2.5. 引入(Introductions)...

    128元尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),安全(整合SpringSecurity),分布式...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    spring boot 全面的样例代码

    - chapter4-1-1:[使用@Scheduled创建定时任务](http://blog.didispace.com/springbootscheduled/) - chapter4-1-2:[使用@Async实现异步调用](http://blog.didispace.com/springbootasync/) #### 日志管理 - ...

Global site tag (gtag.js) - Google Analytics