仅加热 URI,不等待 Spring 中的响应 Rest
only heat a URI, not wait for responce in Spring Rest
假设我使用 POSTMAN 使用 Hello
class POST JSON 对象单击 URI localhost:8080/admin/hello
并有一个控制器,例如,
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
//here it goes to service class and doing processes...
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
我只想让 URI 命中控制器而不是等待响应(因为处理所有服务 class 过程需要 10 秒,但我想 return 立即没有响应)。
我该如何实施?
将所有处理逻辑移动到服务 class(如果您还没有)并使用 @Async
注释您的服务 class 方法。这将使方法调用在单独的线程中执行,并且控制器方法不会等待服务 class 方法完成。
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
helloHomeService.processRequest(obj);
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
public Class HomeService{
@Async
public void processRequest(Hello obj){
//processing logic
}
}
您可以使用@EnableAsync 启用异步:
@Configuration
@EnableAsync
class AsyncConfig{}
@Component //in order to be scanned
class YourService{
@Async
public void asyncMethod(args){}
}
}
in your controller:
@Autowired
private YourService service;
....
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
service.asyncMethode(params);
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
您可以通过配置启用异步处理。
@Configuration
@EnableAsync
public class YourConfig
现在您可以添加一项服务并通过使用 @Async
注释对其方法之一启用异步处理。它会在调用后立即 return。
@Async
public void asyncMethodOnAService() {}
如果您想为 return 一个值服务,您可以 return 一个 CompletableFuture
@Async
public CompletableFuture<String> asyncMethodOnAService() {}
调用异步方法的控制器可以return一个DeferredResult
,这将让客户端知道异步处理完成后结果将可用。
@RequestMapping(value = "/async", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<String>> doAsync() {
DeferredResult<ResponseEntity<String>> result = new DeferredResult<>();
this.asyncService.asyncMethodOnAService().whenComplete((serviceResult, throwable) -> result.setResult(ResponseEntity.ok(serviceResult)));
return result;
}
假设我使用 POSTMAN 使用 Hello
class POST JSON 对象单击 URI localhost:8080/admin/hello
并有一个控制器,例如,
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
//here it goes to service class and doing processes...
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
我只想让 URI 命中控制器而不是等待响应(因为处理所有服务 class 过程需要 10 秒,但我想 return 立即没有响应)。
我该如何实施?
将所有处理逻辑移动到服务 class(如果您还没有)并使用 @Async
注释您的服务 class 方法。这将使方法调用在单独的线程中执行,并且控制器方法不会等待服务 class 方法完成。
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
helloHomeService.processRequest(obj);
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
public Class HomeService{
@Async
public void processRequest(Hello obj){
//processing logic
}
}
您可以使用@EnableAsync 启用异步:
@Configuration
@EnableAsync
class AsyncConfig{}
@Component //in order to be scanned
class YourService{
@Async
public void asyncMethod(args){}
}
}
in your controller:
@Autowired
private YourService service;
....
public ResponseEntity<Hello> helloHome(@RequestBody Hello obj){
service.asyncMethode(params);
return new ResponseEntity<Hello>(obj, HttpStatus.OK);
}
您可以通过配置启用异步处理。
@Configuration
@EnableAsync
public class YourConfig
现在您可以添加一项服务并通过使用 @Async
注释对其方法之一启用异步处理。它会在调用后立即 return。
@Async
public void asyncMethodOnAService() {}
如果您想为 return 一个值服务,您可以 return 一个 CompletableFuture
@Async
public CompletableFuture<String> asyncMethodOnAService() {}
调用异步方法的控制器可以return一个DeferredResult
,这将让客户端知道异步处理完成后结果将可用。
@RequestMapping(value = "/async", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<String>> doAsync() {
DeferredResult<ResponseEntity<String>> result = new DeferredResult<>();
this.asyncService.asyncMethodOnAService().whenComplete((serviceResult, throwable) -> result.setResult(ResponseEntity.ok(serviceResult)));
return result;
}