springboot任务的异步处理

说明:

在添加开启异步处理业务的时候,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);
    }

}

猜你喜欢

转载自blog.csdn.net/m0_57184607/article/details/120926720