栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 官方文档示例:(46)异步任务执行支持

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 官方文档示例:(46)异步任务执行支持

一、普通的异步任务执行:
spring boot 默认装配了一个ThreadPoolTaskExecutor类型的bean,用来执行异步任务,可以通过application.properties对该bean进行配置:

spring.task.execution.pool.max-size=16
spring.task.execution.pool.queue-capacity=100
spring.task.execution.pool.keep-alive=10s

示例
1.在启动类加上注解@EnableAsync,用于支持异步任务

package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);
    }
}

2.在service中使用@Async注解定义异步任务

package cn.edu.tju.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Locale;

@Service
public class BasicService  {
    @Async
    public void printInfo(){
        System.out.println("线程名称:"+Thread.currentThread().getName());
        System.out.println("当前时间:"+new Date().toLocaleString());
    }
}

3.在controller中注入service,并调用异步任务的方法

package cn.edu.tju.controller;

import cn.edu.tju.domain.UserInfo;
import cn.edu.tju.service.BasicService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController4 {
    @Autowired
    private BasicService basicService;

    @RequestMapping("/test5")
    public String getMessage5() throws Exception{
        basicService.printInfo();
        System.out.println("______________________________________");
        System.out.println("线程名称:"+Thread.currentThread().getName());

        String result="hello";

        return result;
    }
}

4.启动程序并访问/test5接口,看到控制台输出如下

可以看到controller中的逻辑早于service中异步任务的执行

二、定时任务的执行:
spring boot默认装配了ThreadPoolTaskScheduler用于支持定时任务的执行,
可以通过application.properties对该bean进行配置:

spring.task.scheduling.thread-name-prefix=schedulingspring.task.scheduling.pool.size=2

使用方法如下:
1.在启动类加@EnableScheduling注解:

package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class,args);
    }
}

2.定义service,并在某个方法上加@Scheduled注解

package cn.edu.tju.service;

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

import java.util.Date;

@Service
public class ScheduledService {
    //每5秒执行一次
    @Scheduled(fixedRate = 5000)
    private void logTime(){
        System.out.println("当前时间: "+new Date().toLocaleString());
    }
}

3.启动程序,并运行,可以看到控制台每5秒打印一次当前时间

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号