Image Servlet for JSF Project "Locking" the Application Server

Asked

Viewed 462 times

0

I have the following code that provides my portal with the images that are in the database

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String id = req.getParameter("id");


    if(id.equals("logo")){
        resp.setContentType(museu.getConfiguracao().getContentTypeLogo());
        resp.getOutputStream().write(museu.getConfiguracao().getLogo());

    }else if(req.getParameter("slide")!=null){
        resp.setContentType(banco.getSlide(id).getContentType());
        resp.getOutputStream().write(banco.getSlide(id).getContent());

    }
    else{
        Foto foto = museu.getFoto(Long.parseLong(id));
        resp.setContentType(foto.getContentType());
        resp.getOutputStream().write(foto.getContent());
    }
}

The problem, when I try to request 3/4 or more images to the same page, the application server (Glassfish) simply "hangs" and after a while releases the error, the images are usually loaded a little (about 10%, another 50%) but simply the server "hangs" out of nowhere. Headers of Log Errors:

2014-03-21T08:58:20.561-0300|Grave: java.io.IOException: java.util.concurrent.TimeoutException
at org.glassfish.grizzly.utils.Exceptions.makeIOException(Exceptions.java:81)

Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:201)
at org.glassfish.grizzly.http.io.OutputBuffer.blockAfterWriteIfNeeded(OutputBuffer.java:951)
... 36 more

2014-03-21T08:58:20.567-0300|Advertência: StandardWrapperValve[MultiMidiaServlet]: Servlet.service() for servlet MultiMidiaServlet threw exception
java.io.IOException: java.util.concurrent.TimeoutException

Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:201)
at org.glassfish.grizzly.http.io.OutputBuffer.blockAfterWriteIfNeeded(OutputBuffer.java:951)
... 36 more
  • these requests you are making, are for the same image? Have you considered using a cache, not to read the image stream in every request?

  • They are for different images, up to 3 images works normally, make a cache as?

  • Then I was able to "solve" the problem, it seemed to be a bug in Grizzly , a glassfish module that manages the contents of Servlet, I got a server release in the repository that has a more authenticated version of Grizzly and no longer occurs the problem :)

1 answer

1

I had to do it once. What I’m finding it rather strange is the Glassfish "lock" in some photos and work in others. Is that right? Anyway, it follows the code that I used to do almost the same thing to you. Only in my Servlet there is integration with the Spring that you can ignore.

Anyway, it’s not complicated to understand. Try to adapt your need and see if it works.

The functioning of that Servlet is basically to search a photo in the database (oracle) where the photo column is a blob. I’m using JPA, then the entity for the photo is ColaboradorFoto.

The call from that Servlet would look like this: http://www.example.com/foto?matricula=123456 for example. The photo would appear in the browser.

@Component("fotoServlet")
@WebServlet("/foto")
public class FotoServlet extends HttpServlet {

    private static final long serialVersionUID = -7451359765915659089L;
    private static final Logger logger = LogManager.getLogger(FotoServlet.class);

    public FotoServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String matricula = request.getParameter("matricula");

        if (matricula != null && !matricula.isEmpty()) {
            OutputStream o = response.getOutputStream();
            response.setContentType("image/jpg");

            WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
            ColaboradorService colaboradorService = springContext.getBean(ColaboradorService.class);

            try {
                ColaboradorFoto colabFoto = colaboradorService.getColaboradorFoto(Long.parseLong(matricula));
                if (colabFoto != null && colabFoto.getFoto() != null) {
                    byte[] imgData = colabFoto.getFoto();
                    o.write(imgData);
                }
                else {
                    o.write(this.getFotoDefault());
                }

            }
            catch (ServiceException e) {
                o.write(this.getFotoDefault());
                logger.error("Erro ao obter a foto do colaborador no banco de dados", e);
            }
            catch (NumberFormatException e) {
                o.write(this.getFotoDefault());
                logger.warn("Matricula passada nao contem somente numeros. Retornando foto default", e);
            }

            o.flush();
            o.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    private byte[] getFotoDefault() {
        InputStream input = getServletContext().getResourceAsStream("/resources/images/sem_foto.png");

        try {
            return IOUtils.toByteArray(input);
        }
        catch (IOException e) {
            logger.error("Erro ao tentar obter foto padrao para colaborador", e);
            return null;
        }
    }
}
  • I refactored all the code and still I’m having problems, with the tests I did here the problem happens in the following case, when I put 3 images of +/- 400k works normal, when I put another 400k, starts the lock, must be something related to the server memory , I will try to configure this or limit the upload size ps: I like the unavailable image part, I have already put the code here

  • Very strange that Glassfish problem starts to crash. Have you tried another server or other version of Glassfish itself to see if it’s not related to the server?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.