如何异步的处理restful服务(基础)
1、使用Runnable
2、使用DeferredResult
3、异步处理的一些配置
正常请求方式
package com.nxz.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Callable;
/**
* 异步处理的controller
*/
@RestController
@Slf4j
public class AsyncController {
//标准的同步处理逻辑
@RequestMapping("/order")
public String order() throws InterruptedException {
log.info("主线城开始");
Thread.sleep(1000);//具体的业务逻辑
log.info("主线程返回");
return "success";
}
}
1、通过callable异步方式
package com.nxz.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Callable;
/**
* 异步处理的controller
*/
@RestController
@Slf4j
public class AsyncController {
//异步处理方式
@RequestMapping("/order1")
public Callable<String> order1() throws InterruptedException {
l