JSF Project Image Servlet "Locking" the Application Server

0

I have the following code that gives my portal 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 for a single page, the application server (Glassfish) simply "hangs" and after a time looses the error, the images are usually loaded a bit (some 10%, another 50%) but simply the server "hangs" out of nowhere. Headers of Errors in log:

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
    
asked by anonymous 21.03.2014 / 13:09

1 answer

1

I've had to do this once. What I'm finding quite strange is the Glassfish "crashing" in some photos and running on others. That's right? Anyway, follow the code I used to do almost the same thing as you. But in my servlet there is integration with Spring that you can ignore.

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

The function of this servlet is basically to search for a photo in the database (oracle) where the photo column is a blob . I'm using JPA , so the entity for the photo is ColaboradorFoto .

The call of this 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;
        }
    }
}
    
22.03.2014 / 01:13