Java 如何处理Jetty异常-一个长时间运行的HTTP请求超时,但它调用的进程从未终止,Jetty不满意
我有一个Jetty服务器来处理长时间运行的HTTP请求——响应由不同的进程X生成,并以收集器哈希结束,Jetty请求会定期检查 有3种情况:Java 如何处理Jetty异常-一个长时间运行的HTTP请求超时,但它调用的进程从未终止,Jetty不满意,java,asynchronous,jetty,continuations,Java,Asynchronous,Jetty,Continuations,我有一个Jetty服务器来处理长时间运行的HTTP请求——响应由不同的进程X生成,并以收集器哈希结束,Jetty请求会定期检查 有3种情况: 进程X在HTTP请求超时之前完成- 没问题 进程X在请求超时后完成-否 问题 进程X从未完成-发生以下异常 如何检测这种情况(3)并防止异常,同时允许其他两种情况正常工作 例外情况: 2012-06-18 00:13:31.055:WARN:oejut.QueuedThreadPool: java.lang.IllegalStateException: I
2012-06-18 00:13:31.055:WARN:oejut.QueuedThreadPool:
java.lang.IllegalStateException: IDLE,initial
at org.eclipse.jetty.server.AsyncContinuation.complete(AsyncContinuation.java:569)
at server.AsyncHTTPRequestProcessor.run(AsyncHTTPRequestProcessor.java:72)
at org.eclipse.jetty.server.handler.ContextHandler.handle(ContextHandler.java:1119)
at org.eclipse.jetty.server.AsyncContinuation$1.run(AsyncContinuation.java:875)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
at java.lang.Thread.run(Thread.java:679)
HTTP请求的Jetty延续:
public class AsyncHTTPRequestProcessor implements Runnable {
private ConcurrentHashMap<String, String> collector;
private Logger logger;
private AsyncContext ctx;
//Defined this here because of strange behaviour when running junit
//tests and the response json string being empty...
private String responseStr = null;
public AsyncHTTPRequestProcessor(AsyncContext _ctx,
ConcurrentHashMap<String, String> _collector, Logger _logger) {
ctx = _ctx;
collector = _collector;
logger = _logger;
}
@Override
public void run() {
logger.info("AsyncContinuation start");
//if(!((AsyncContinuation)ctx).isInitial()){
String rid = (String) ctx.getRequest().getAttribute("rid");
int elapsed = 0;
if(rid !=null)
{
logger.info("AsyncContinuation rid="+rid);
while(elapsed<ctx.getTimeout())
{
if(collector.containsKey(rid)){
responseStr = collector.get(rid);
collector.remove(rid);
logger.info("--->API http request in collector:"+responseStr);
ctx.getRequest().setAttribute("status",200);
ctx.getRequest().setAttribute("response", responseStr);
ctx.getRequest().setAttribute("endTime",System.currentTimeMillis());
//ctx.complete();
break;
}
try {
Thread.sleep(10);
elapsed+=10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//}
logger.info("Collector in async stuff:");
for(String key:collector.keySet()){
logger.info(key+"->"+collector.get(key));
}
for(Entry<String, String> x:collector.entrySet()){
logger.info(x.getKey()+"->"+x.getValue());
}
ctx.complete(); <---- this line 72
}
}
}
公共类AsyncHTTPRequestProcessor实现可运行{
私有ConcurrentHashMap收集器;
私人记录器;
专用异步上下文ctx;
//由于运行junit时的奇怪行为,在这里定义了这个
//测试和响应json字符串为空。。。
私有字符串responsest=null;
公共异步HttpRequestProcessor(AsyncContext\u ctx,
ConcurrentHashMap(收集器、记录器){
ctx=_ctx;
收集器=_收集器;
记录器=_记录器;
}
@凌驾
公开募捐{
info(“异步继续启动”);
//如果(!((异步继续)ctx.isInitial()){
String rid=(String)ctx.getRequest().getAttribute(“rid”);
int=0;
if(rid!=null)
{
logger.info(“async=”+rid);
在这种情况下,使用try-catch块可能会有所帮助
try{
ctx.complete()
} catch (IllegalStateException e){
//Handle it the way you prefer.
}
这里的问题不是调用AsyncContext#complete(),而是代码的总体设计
Continuations(Servlet async也是如此)设计为异步。利用内部continuations timeout的while循环不能在这里。您正在通过这样做将异步设计转换为同步设计。正确的做法是使用Continuation#addContinuationListener()注册侦听器并实现onTimeout()方法来适当地处理超时情况
一旦超时逻辑超时,我建议将process X逻辑移动到类AsyncHTTPRequestProcessor,并从使用收集器的需要中移出。在处理过程中,您应该假设当前线程永远不会超时。通过这样做,您可以调用complete()有意义的话,您将不会受到收集器并发问题的影响。我也有同样的问题。我正在尝试解决它。我会尽快给出答案。如果进程从未完成,那么异常怎么会发生?