Servlet 3.0 允许“请求”线程(或“主”线程)将长时间运行的处理委托(delegate)给其他线程,以便释放自身以接收更多请求。同意。 也就是说,我们正在通过利用多线程实现(请求的)可伸缩性。
但这需要我的“Servlet 容器 JVM”能够进行此类处理。 如果我有一个多层架构,其中“Servlet 容器 JVM”只是入口点,而服务请求的逻辑位于其他 JVM 中的其他地方(在本文中称为“服务 JVM”)怎么办?
如果我想将传入的“请求”(或至少请求的相关属性)发布到 JMS 队列并让“请求”由“服务 JVM”池中的一个获取和处理怎么办?将发送“响应”(例如 JSON)的责任也委托(delegate)给此服务 JVM 不是更好吗?
我认为“AsyncContext”不能在 Servlet 容器 JVM 之外有意义地传递。那么,如何真正委托(delegate)分布式服务 (JVM) 完成请求处理和响应发送?
在代码/伪代码方面,我的问题是:
@WebServlet(urlPatterns = "/AsyncServlet", asyncSupported=true)
public class AsyncServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
AsyncContext asyncCtx = request.startAsync();
// Put the asyncCtx in a JMS queue so as to be picked by another
// service JVM that can really service this request.
// return back to receiving requests and dont worry abt sending a response
// The service JVM will take care of sending the appropriate response
// as it has the data necessary for the response.
}
}
一个选项似乎是让工作线程(在 Servlet 容器 JVM 中)等待来自服务 JVM 的响应。在服务 JVM 完成实际处理后,它可以将结果(通过消息或其他方式)传达给相应的工作线程,并让工作线程发送 GET 响应。
我想知道是否有(肯定会有!)比这更好的选择,因为这看起来太复杂了!
最佳答案
- 将上下文设置为异步
- 在单例 bean 中存储上下文
- 发送 jms 请求
- 处理jms请求
- 发送jms回复
- 从单例 bean 获取回复的上下文
- 回复客户
你可能想设置一个清理计时器,你可以用异步单向 ejb 调用替换 jms
关于java - 小服务程序 3.0 : How to off-load Async processing to a different JVM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15669424/