1. Controller为单例;
  2. 非线程安全;
  3. 堵塞方式;
  4. 1个request对应1个处理Thread;
@RestController
public class ProcessingController {

  @RequestMapping("/process-blocking")
  public ProcessingStatus blockingProcessing(...) {
    ...
    return new ProcessingStatus(...);
  }
}

【转】non-blocking REST services with Spring MVC

【转】non-blocking REST services with Spring MVC

非阻塞

@RestController
public class ProcessingController {

  @RequestMapping("/process-non-blocking")
  public DeferredResult<ProcessingStatus> nonBlockingProcessing(...) {

    // Initiate the processing in another thread
    DeferredResult<ProcessingStatus> deferredResult = new DeferredResult<>();
    ProcessingTask task = new ProcessingTask(deferredResult, ...);
    dispatch(task);

    // Return to let go of the precious thread we are holding on to...
    return deferredResult;
  }
}


public class ProcessingTask extends SomeCallbackInterface {

  private DeferredResult<ProcessingStatus> deferredResult;

  public ProcessingTask(DeferredResult<ProcessingStatus> deferredResult, ...) {
    this.deferredResult = deferredResult;
    ...
  }

  @Override
  public void done() {
    if (deferredResult.isSetOrExpired()) {
      LOG.warn("Processing of non-blocking request already expired");
    } else {
      boolean deferredStatus = deferredResult.setResult(new ProcessingStatus(...));
    }
  }
}

【转】non-blocking REST services with Spring MVC

【转】non-blocking REST services with Spring MVC

【转】non-blocking REST services with Spring MVC


原文链接

Developing non-blocking REST services with Spring MVC

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-01
  • 2022-01-31
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-16
  • 2022-12-23
  • 2021-05-14
  • 2022-03-08
  • 2021-10-19
  • 2021-04-16
  • 2022-01-22
相关资源
相似解决方案