Hi Simone,
> The real question is: why you call startAsync() in your filter and then chain to the ProxyServlet?
Using AsyncListener, I want to wait for the "completion" of the requests whose URLs are mapped to my filter, so that I can perform some specific operations in the AsyncListener's onComplete() method. For example, if the request to create
a search object in Kibana is completed, I want to create a new object in my application and also generate an audit event.
Anyway, I fixed the IllegalStateException as follows:
1.
The listener related code segment was located BEFORE the filter’s “chain.doFilter()” call. I moved this code segment to a position AFTER the filter’s "chain.doFilter()" call, so that I can retrieve the AsyncContext that is "already created"
in the Jetty ProxyServlet's service() method. I can then add an AsyncListener to the retrieved AsyncContext.
2.
I changed the “request.startAsync()” call in my filter to “request.getAsyncContext()” so that I am not starting a new AsyncContext which leads to the IllegalStateException, but only retrieving the AsyncContext that is already created
in Jetty’s ProxyServlet.
So the updated code segment looks like this:
chain.doFilter(myRequestWrapper, response);
AsyncContext asyncContext = myRequestWrapper.getAsyncContext();
asyncContext.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent event) throws IOException
{
logger.log(Level.WARNING, "Async timeout...");
}
@Override
public void onStartAsync(AsyncEvent event) throws IOException
{
logger.log(Level.INFO, "Async start...");
}
@Override
public void onError(AsyncEvent event) throws IOException
{
logger.log(Level.SEVERE, "Async error...");
}
@Override
public void onComplete(AsyncEvent event) throws IOException
{
HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
//HttpServletResponse httpResponse = (HttpServletResponse) event.getAsyncContext().getResponse();
if (httpResponse.getStatus() == HttpServletResponse.SC_OK || httpResponse.getStatus() == HttpServletResponse.SC_CREATED ) {
//some business logic
}
}
}, myRequestWrapper, httpServletResponse);
Thanks a lot for your response…
Regards,
Sanjay
----------------------------------------------------------------------
Date: Tue, 21 Feb 2017 23:14:37 +0100
From: Simone Bordet <sbordet@xxxxxxxxxxx>
To: "Jetty @ Eclipse developer discussion list"
<jetty-dev@xxxxxxxxxxx>
Subject: Re: [jetty-dev] Calling startAsync() on a servlet request
throws java.lang.IllegalStateException: s=DISPATCHED i=true a=STARTED
Message-ID:
<CAFWmRJ0OrqcF7tQOi2OsyfZxwUfY67QQ-HAP0qC0FXf5_K+RJA@xxxxxxxxxxxxxx>
Content-Type: text/plain; charset=UTF-8
Hi,
On Tue, Feb 21, 2017 at 6:47 PM, Sanjay Bhat <Sanjay.Bhat@xxxxxxxxxxxxxx> wrote:
> Is the "IllegalStateException" being thrown because the startAsync()
> method is being called twice on the same request - for the first time
> in my filter and for the second time in Jetty ProxyServlet's service() method?
Yes.
> If yes, is there no way to use an "AsyncListener" in a filter chain
> that is terminated by Jetty's ProxyServlet?
Multiple startAsync() calls are supported by the Servlet Spec (and of course in Jetty), but only in consecutive cycles.
The real question is: why you call startAsync() in your filter and then chain to the ProxyServlet ?
--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support from the Jetty & CometD experts.
------------------------------