NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

AsyncContext and IllegalStateException

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
cp7781



Joined: 12 Jul 2016
Posts: 143

PostPosted: Wed Aug 03, 2016 1:03 pm    Post subject: AsyncContext and IllegalStateException Reply with quote

Alright, following situation: There is a javax.servlet.http.HttpServlet, which starts a javax.servlet.AsyncContext. Inside that context several requests to a database are made, a java.awt.image.BufferedImage is painted and send back as a .png image response. It all works very well in the browser, even if the F5 key keeps being pressed.

It even works good, when a basic workload application is being utilized, which simulates 64 users reloading the diagram constantly.

But here comes the exception: If the workload application is being canceled manually via the NetBeans IDE, it causes major trouble on the server side. The Tomcat 8.0.36 log shows messages like:


Code:
org.apache.catalina.connector.ClientAbortException


Code:
03-Aug-2016 14:43:51.566 INFO [http-nio-80-exec-36] org.apache.coyote.AbstractProcessor.setErrorState An error occurred in processing while on a non-container thread. The connection will be closed immediately


Code:
Exception in thread "http-nio-80-exec-36" java.lang.IllegalStateException: The request associated with the AsyncContext has already completed processing.


And after such an accident, the web application becomes unusable. It responds with HTTP status code 500 and needs to be restarted.

So, I'm trying to figure out, what to do to prevent that.

Here is the affected method:

Code:
    /**
     * Writes a .png image as the servlet response.
     *
     * The specified image should not be null.
     *
     * @param bufferedImage the image
     */
    private void writePNGImage(BufferedImage bufferedImage) {
        if (bufferedImage == null) {
            String message = "The specified image should not be null.";
            throw new IllegalArgumentException(message);
        }
        String formatName = "png";
        response.setContentType("image/" + formatName);
        try (ServletOutputStream servletOutputStream = response.getOutputStream()) {
            if (servletOutputStream != null) {
                ImageIO.write(bufferedImage, formatName, servletOutputStream);
            }
        } catch (IOException ex) {
            String message = "Can not write to the servlet output stream.";
            Logger.getLogger(Processor.class.getName()).log(Level.SEVERE, message, ex);
        } finally {
            context.complete();
        }
    }


Any ideas? Share them.

Edit:

Found it.

Code:
        if (response.isCommitted()) {
            return;
        }


Last edited by cp7781 on Wed Aug 03, 2016 8:49 pm; edited 1 time in total
Back to top
cp7781



Joined: 12 Jul 2016
Posts: 143

PostPosted: Wed Aug 03, 2016 7:44 pm    Post subject: Reply with quote

Sadly, the problem still persists. It was shadowed by the fact, that the database connection pool size was temporary increased for testing purposes. If the connection pool is limited from 1 up to 4 connections, the web application starts to time out some asynchronous requests. They will be closed, while their worker threads still exist. So, the workers will work on response objects, that are already gone. And at the moment the server hits its first NullPointerException, the container goes with them.

So, yes, it would be very nice to have a more robust server container. One that knows, how to handle its garbage. Isn't easy, I know.
Back to top
cp7781



Joined: 12 Jul 2016
Posts: 143

PostPosted: Wed Aug 03, 2016 9:30 pm    Post subject: Reply with quote

The garbage now flies. I made a naive assumption when registering an javax.servlet.AsyncListener earlier in the application. The java.lang.IllegalStateException was thrown there.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo