zoukankan      html  css  js  c++  java
  • 【转】non-blocking REST services with Spring MVC

    堵塞Controller

    1. Controller为单例;
    2. 非线程安全;
    3. 堵塞方式;
    4. 1个request对应1个处理Thread;
    @RestController
    public class ProcessingController {
    
      @RequestMapping("/process-blocking")
      public ProcessingStatus blockingProcessing(...) {
        ...
        return new ProcessingStatus(...);
      }
    }
    

    时序图

    测试结果

    非阻塞

    @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(...));
        }
      }
    }
    

    THE EXTERNAL RESOURCE API IS ALSO NON-BLOCKING

    THE EXTERNAL RESOURCE API IS BLOCKING.

    测试结果


    原文链接

    Developing non-blocking REST services with Spring MVC

  • 相关阅读:
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    免费的论文查重网站
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/8465142.html
Copyright © 2011-2022 走看看