springboot任务的异步处理

本文介绍了在SpringBoot中如何实现任务的异步处理。通过在Service层的方法上添加@Async注解,并在启动类中使用@EnableAsync开启异步功能,解决Controller层调用返回null的问题。详细内容包括HelloService、HelloController的设置以及正常和异步启动类的对比。

说明:

在添加开启异步处理业务的时候,controller层调用service层返回数据为null;

springboot处理异步任务:

  • 在异步方法上面添加一个@Async注解
  • 在启动类上面天剑一个@EnableAsync开启异步功能

正常处理异步任务:

HelloService

package cn.itcast.springbootasyncmeathod.service;

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

/**
 * @ClassName: Hello
 * @Auther: GZ
 * @Date: 2021/10/23 21:12
 * @Description:
 */
@Service
public class HelloService {
    public String getHello() {
        try {
            Thread.sleep(3000);//线程沉睡三秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "HELLO";//向前端返回字符串
    }

}

HelloController

package cn.itcast.springbootasyncmeathod.controller;

import cn.itcast.springbootasyncmeathod.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: Hello
 * @Auther: GZ
 * @Date: 2021/10/23 21:14
 * @Description:
 */
@RestController
public class HelloController {
    @Autowired
    private HelloService hello;
    @GetMapping("/hello")
    public String getHello(){
        System.out.println(this.hello.getHello());//打印字符串
        return "ok";浏览器返回ok会延迟三秒
    }
}

正常的启动类

package cn.itcast.springbootasyncmeathod;

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

@SpringBootApplication
public class SpringbootAsyncMeathodApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAsyncMeathodApplication.class, args);
    }

}

springboot任务的异步处理

package cn.itcast.springbootasyncmeathod.service;

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

/**
 * @ClassName: Hello
 * @Auther: GZ
 * @Date: 2021/10/23 21:12
 * @Description:
 */
@Service
public class HelloService {
    @Async//第一步添加异步注解
    public String getHello() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "HELLO";
    }
}

HelloController,前端不变

package cn.itcast.springbootasyncmeathod.controller;

import cn.itcast.springbootasyncmeathod.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: Hello
 * @Auther: GZ
 * @Date: 2021/10/23 21:14
 * @Description:
 */
@RestController
public class HelloController {
    @Autowired
    private HelloService hello;
    @GetMapping("/hello")
    public String getHello(){
        System.out.println(this.hello.getHello());
        return "ok";
    }
}


@EnableAsync启动类

package cn.itcast.springbootasyncmeathod;

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

@EnableAsync//添加开启异步任务功能
@SpringBootApplication
public class SpringbootAsyncMeathodApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootAsyncMeathodApplication.class, args);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值