堵塞Controller
- Controller为单例;
- 非线程安全;
- 堵塞方式;
- 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(...));
}
}
}
原文链接
转载请标明出处:http://www.cnblogs.com/ssslinppp/