Thank Joakim,

I understand how to avoid the exception now. Do you have some more 
documentation on the suspend/resume? I still don’t understand why I would be 
called again. So in the else part below, I just always return and do nothing. 
And I’m not sure that this is the correct thing to do. Or should I take this as 
a notification from the web server that the request processing for this 
particular request is taking too long? In that case I should e.g. send a 
timeout reply to the caller and abort my own processing that I’m doing 
asynchronously. Does this makes sense?

Bert

From: [email protected] [mailto:[email protected]] 
On Behalf Of Joakim Erdfelt
Sent: maandag 21 december 2015 18:56
To: JETTY user mailing list
Subject: Re: [jetty-users] handling the same request multiple times?

You startAsync fine, but then don't test to see if it was started before.
That handler is reentrant for a single request processing.
The behavior depends on how the suspend / resume works out.

You might want to ensure that you don't attempt to start the AsyncContext twice.
There are many different ways to accomplish this.

One possible approach.


AsyncContext context = (AsyncContext) 
request.getAttribute(AsyncContext.class.getName());
if(context == null) {
    context = request.startAsync();
    request.setAttribute(AsyncContext.class.getName(), context);
    ServletInputStream inputStream = request.getInputStream();
    inputStream.setReadListener(new ReadListener() { ... });
} else {
    System.out.println("Re-entrant AsyncContext");
    // do what you want/need here.
    // this could even be an error handling routine.
}

Also of note, since you are using a naked Handler, it will always be called.
Even if you have something after that handler, or a RequestDispatcher call, or 
an error handling, etc ...


Joakim Erdfelt / [email protected]<mailto:[email protected]>

On Mon, Dec 21, 2015 at 5:39 AM, Robben, Bert 
<[email protected]<mailto:[email protected]>> wrote:
When I put my Jetty server under high load, I sometimes get 
IllegalStateException being thrown mentioning “ReadListener already set”. These 
exceptions are thrown when a ReadListener is set more than once to the 
inputstream of an incoming request. However, I never do that. I can only 
explain this if the same request is sent to my handler multiple times, which I 
don’t really understand. Can somebody explain how this can happen so that I can 
avoid these exceptions? Note that I don’t get these exception under light load.

Here’s some more background:

(1)          I’m using jetty 9.3.6.v20151106
(2)          I’m running an embedded Jetty
(3)          I only have a single request handler. It extends AbstractHandler 
and does the following:

       public void handle(String target, Request baseRequest, 
HttpServletRequest request, HttpServletResponse response) throws IOException, 
ServletException {
             String id = request.getRequestURI().substring(1);
             baseRequest.setHandled(true);
             long count = requests.decrementAndGet();
             if (count < 0) {
                    requests.incrementAndGet();
                    response.setStatus(HttpStatus.SERVICE_UNAVAILABLE_503);
                    return;
             }
             AsyncContext context = request.startAsync();
            ServletInputStream inputStream = request.getInputStream();
             inputStream.setReadListener(new ReadListener() { ... });
}


So as you can see, all requests are handled asynchronously unless my server is 
overloaded (which is not the case when the exceptions occur).


Thanks,

Bert

PS: Here’s the complete stacktrace

java.lang.IllegalStateException: ReadListener already set
        at 
org.eclipse.jetty.server.HttpInput.setReadListener(HttpInput.java:528) 
~[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at 
com.clear2pay.opf.realtime.rest.HttpRequestHandler.handle(HttpRequestHandler.java:56)
 ~[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at 
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119) 
~[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at org.eclipse.jetty.server.Server.handleAsync(Server.java:567) 
~[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:393) 
[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:262) 
[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at 
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
 [opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at 
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572) 
[opf-0.0.1-SNAPSHOT-allinone.jar:na]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]
12:27:09.743 [qtp2027944996-257] WARN  org.eclipse.jetty.server.HttpChannel - 
ERROR_DISPATCH loop detected on Request[POST 
//be-nft4-app-04:8080/590715314303067]@5928fccb 
java.lang.IllegalStateException: ReadListener already set
12:27:09.743 [qtp2027944996-309] WARN  org.eclipse.jetty.server.HttpChannel - 
ERROR_DISPATCH loop detected on Request[POST 
//be-nft4-app-04:8080/590715314304262]@7d407fdb 
java.lang.IllegalStateException: ReadListener already set

_____________
The information contained in this message is proprietary and/or confidential. 
If you are not the intended recipient, please: (i) delete the message and all 
copies; (ii) do not disclose, distribute or use the message in any manner; and 
(iii) notify the sender immediately. In addition, please be aware that any 
message addressed to our domain is subject to archiving and review by persons 
other than the intended recipient. Thank you.

_______________________________________________
jetty-users mailing list
[email protected]<mailto:[email protected]>
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users<https://urldefense.proofpoint.com/v2/url?u=https-3A__dev.eclipse.org_mailman_listinfo_jetty-2Dusers&d=BQMFaQ&c=3BfiSO86x5iKjpl2b39jud9R1NrKYqPq2js90dwBswk&r=fgyjFJSrg1bzpUxv9yDgCBlA45pLfd4ZSJGHV4QrcMk&m=D4WQOtreLYkpIKUdxQsxsxGZsjkeYBY_u4Kr5EBnGnI&s=k-RNyynF4pcrN2XNMTjdMXfM0MtWA5F5v9NaGNDaxVk&e=>


_____________
The information contained in this message is proprietary and/or confidential. 
If you are not the intended recipient, please: (i) delete the message and all 
copies; (ii) do not disclose, distribute or use the message in any manner; and 
(iii) notify the sender immediately. In addition, please be aware that any 
message addressed to our domain is subject to archiving and review by persons 
other than the intended recipient. Thank you.
_______________________________________________
jetty-users mailing list
[email protected]
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users

Reply via email to