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秒打印一次当前时间
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值