异步任务实现
小于 1 分钟
异步任务实现
在utils文件夹下创建MyAsyncTask类

代码
package com.example.springs.controller;
import com.example.springs.utils.MyAsyncTask;
import lombok.extern.slf4j.Slf4j;
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;
@RequestMapping("admin")
@RestController
@Slf4j
public class hello {
@Autowired//注入其它类
private MyAsyncTask myConfig;
@Autowired//注入其它类
private MyAsyncTask myAsyncTask;
@GetMapping("getMyConfig")
public Object getMyConfig() {
myAsyncTask.publishMsg();
log.info("这是跳过异步任务的执行");
return "执行完毕";
}
}
然后在controller文件夹下的hello类中编写接口
package com.example.springs.controller;
import com.example.springs.utils.MyAsyncTask;
import lombok.extern.slf4j.Slf4j;
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;
@RequestMapping("admin")
@RestController
@Slf4j
public class hello {
@Autowired//注入其它类
private MyAsyncTask myConfig;
@Autowired//注入其它类
private MyAsyncTask myAsyncTask;
@GetMapping("getMyConfig")
public Object getMyConfig() {
myAsyncTask.publishMsg();
log.info("这是跳过异步任务的执行");
return "执行完毕";
}
}

打印

可以看到异步任务延迟了5秒后才执行的