【转】non-blocking REST services with Spring MVC
2024-08-25 21:22:08
堵塞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(...));
}
}
}
原文链接
Developing non-blocking REST services with Spring MVC
最新文章
- Redis时延问题分析及应对
- jquery的show/hide/toggle详解
- 记一次Redis和NetMQ的测试
- iOS搜索附近的位置(类似微博朋友圈位置)
- Eclipse中Java项目转换为Web项目
- md5sum 生成 经md5加密后的字符串
- 《java笔记 day07》
- magiczoom 插件去版本(放大镜效果)
- python 多行字符串
- 通过live555实现H264 RTSP直播
- java mysql驱动
- 测试体验Centrifugo
- NIO学习
- Asp.Net SignalR - 准备工作
- python集合使用范例的代码
- KXO151 Programming &; Problem Solving
- 浏览器虚拟过程IP插件
- 阿里云redis映射到阿里云服务器
- 【Excel】输出固定长文本
- rhel6+apache2.4+mysql5.7+php5.6部署LAMP架构