Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

hibernate - I get a stackOverflowError when trying to generate a very large PDF file in Java

When the data to generate the PDF is small the PDF is generated without problems, but when the data is large I get stackOverflowError.

16:40:37,510 WARN  [org.hibernate.engine.loading.internal.LoadContexts] (default task-54) HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@5df3d7ab<rs=oracle.jdbc.driver.OracleResultSetImpl@357156df>
16:40:37,510 WARN  [org.hibernate.engine.loading.internal.CollectionLoadContext] (default task-54) HHH000160: On CollectionLoadContext#cleanup, localLoadingCollectionKeys contained [1] entries
16:40:37,528 ERROR [br.gov.ans.commons.rest.exception.handler.ExceptionHandler] (default task-54) org.modelmapper.MappingException: ModelMapper mapping errors:

1) Error mapping br.gov.ans.rps.rede.services.entities.Movimentacao to br.gov.ans.rps.rede.services.dtos.MovimentacaoDTO

1 error: org.modelmapper.MappingException: ModelMapper mapping errors:

1) Error mapping br.gov.ans.rps.rede.services.entities.Movimentacao to br.gov.ans.rps.rede.services.dtos.MovimentacaoDTO

1 error
    at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:374)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:69)
    at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:497)
    at org.modelmapper.ModelMapper.map(ModelMapper.java:340)
    at br.gov.ans.rps.rede.services.business.SolicitacaoNegocio.converterMovimentacaoVO(SolicitacaoNegocio.java:394)

io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:264) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:175) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:207) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:802) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.StackOverflowError at java.security.AccessController.doPrivileged(Native Method) at org.jboss.logmanager.formatters.Formatters$14.renderRaw(Formatters.java:638)

solicitacaoBean

public StreamedContent imprimirSolicitacao(SolicitacaoVO solicitacaoVO){
        
        StreamedContent download = new DefaultStreamedContent();

        try {
            SolicitacaoVO solicitacaoCompleta = solicitacaoClient.buscarSolicitacao(solicitacaoVO.getId().toString(), true);
                        
            ModelMapper modelMapper = new ModelMapper();
            
            SolicitacaoVO solicitacaoImprimir = modelMapper.map(solicitacaoCompleta, SolicitacaoVO.class);
            
            for(MovimentacaoVO mov:solicitacaoImprimir.getListaMovimentacao()){
                
                List<PlanoVO> planosSemDuplicados = new ArrayList<PlanoVO>();
                mov.setListaPrestadoresExclusao(new ArrayList<PrestadorVO>());
                for (PlanoVO planoVO : mov.getListaPlanos()) {
                    if(!planosSemDuplicados.contains(planoVO)){
                        planosSemDuplicados.add(planoVO);
                    }
                    if(!mov.getListaPrestadoresExclusao().contains(planoVO.getPrestador())){
                        mov.getListaPrestadoresExclusao().add(planoVO.getPrestador());                              
                    }
                }
                mov.setListaPlanos(planosSemDuplicados);
                
                if ( mov.getReconsideracao()!=null && !mov.getReconsideracao().getArquivos().isEmpty() ){
                    
                    int sizeList = mov.getReconsideracao().getArquivos().size();
                    int i = 1;
                    StringBuffer aux = new StringBuffer();
                    for ( ArquivoReconsideracaoVO arq : mov.getReconsideracao().getArquivos() ){
                        aux.append(arq.getNomeArquivo());
                        if ( i < sizeList ){
                            aux.append(";  ");
                        }else {
                            aux.append(".");
                        }
                        i++;
                    }
                    mov.getReconsideracao().setNomeArquivosConcatenados(aux.toString());
                }
                
                Collections.sort(mov.getListaPrestadoresExclusao());
                Collections.sort(mov.getListaPrestadores());
            }
            
            InputStream pdfGerado = gerarRelatorioSolicitacaoOuMovimentacao(solicitacaoImprimir);
            download = prepararDownload(pdfGerado, "Solicita??o", TipoArquivo.PDF.getContentType());
        } catch (Exception e) {
            exibirMensagemErro("erro.imprimir.pdf");
            logger.error(e,e);
            return null;
        }
        return download;
    }

solicitacaoClient

@JsonIgnoreProperties(ignoreUnknown = true)
public class SolicitacaoClient implements Serializable{
    private static final long serialVersionUID = 1L;

    @Inject
    @PropertiesInfo(file="services.properties", key="rps.rede.services.rest.uri")
    @Server
    private String uri;
    
    @Inject
    @Autenticado(value = "rps.rede", type = AuthType.BEARER)
    private Client cliente;
        
    @Inject
    Logger logger;
    
    public List<OperadoraVO> consultarOperadoras(String filtroCodigo, String filtroCNPJ, String filtroNome) throws Exception{
        Response response = cliente.target(uri + "operadoras")
                .queryParam("codigo", filtroCodigo)
                .queryParam("cnpj", filtroCNPJ)
                .queryParam("nome", filtroNome)
                .request().get();
        try {
                if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                        throw new Exception(response.readEntity(ErrorMessage.class).getError());
                    } else{
                        String json = (String) response.readEntity(String.class);
                        ObjectMapper mapper = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
                        List<OperadoraVO> operadoras = mapper.readValue(json, new TypeReference<List<OperadoraVO>>() { });
                        
                        return operadoras;
                    }
        } finally {
            response.close();
        }

    }
    
    public SolicitacaoVO buscarSolicitacao(String id, boolean lgCompleto) throws Exception{
        WebTarget target = cliente.target(uri + "/solicitacoes/" + id);
        
        target = target.queryParam("lg-completa", lgCompleto ? 1 : 0);
        
        Response response = target.request().get();
        
        try {
            if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                throw new Exception(response.readEntity(ErrorMessage.class).getError());
            } else{
                String json = (String) response.readEntity(String.class);
                ObjectMapper mapper = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
                return mapper.readValue(json, SolicitacaoVO.class);
            }
        } finally {
            response.close();
        }
    }
    
    public List<SolicitacaoVO> buscarSolicitacoes(String coOperadora, FiltroSolicitacao filtro) throws Exception{
        
        Response response = cliente.target(uri + "operadoras/" + coOperadora + "/solicitacoes")
            .queryParam("cdSituacao", filtro.getStatus())
            .queryParam("dataEnvioInicio", Utils.dateToString(filtro.getDataInicio(), "dd/MM/yyyy"))
            .queryParam("dataEnvioFim", Utils.dateToString(filtro.getDataFim(), "dd/MM/yyyy HH:mm"))
            .queryParam("protocolo", Utils.getSomenteNumeros(filtro.getProtocolo()))
            .queryParam("gru", filtro.getGru())
            .request().get();

        try {
            if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                throw new Exception(response.readEntity(ErrorMessage.class).getError());
            } else{
                String json = (String) response.readEntity(String.class);
                ObjectMapper mapper = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
                return mapper.readValue(json, new TypeReference<List<SolicitacaoVO>>() { });
            }
        } finally {
            response.close();
        }
    }
    
    public ComprovanteRelatorio montarComprovanteRelatorio(Integer idSolicitacao) throws Exception{
        
        Response response = cliente.target(uri + "/solicitacoes/" + idSolicitacao + "/montarComprovanteRelatorio").request().get();
        
        try {
            if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                throw new Exception(response.readEntity(ErrorMessage.class).getError());
            } else{
                String json = (String) response.readEntity(String.class);
                ObjectMapper mapper = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
                return mapper.readValue(json, ComprovanteRelatorio.class);
            }           
        } finally {
            response.close();
        }
    }

    public List<MovimentacaoRelatorioVO> montarRelatorioMovimentacoes(Integer idSolicitacao) throws Exception {
        
        Response response = cliente.target(uri + "/solicitacoes/" + idSolicitacao + "/relatorioMovimentacoes").request().get();

        gerarLogResponse(uri + "/solicitacoes/" + idSolicitacao + "/relatorioMovimentacoes", response);

        try {
            if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                throw new Exception(response.readEntity(ErrorMessage.class).getError());
            } else{
                String json = (String) response.readEntity(String.class);
                ObjectMapper mapper = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
                return mapper.readValue(json, new TypeReference<List<MovimentacaoRelatorioVO>>() { });
            }
        } finally {
            response.close();
        }
    }

    public List<MovimentacaoRelatorioVO> montarRelatorioConsultaMovimentacoesInterno(List<MovimentacaoVO> listaMovimentacoes) throws Exception {
        
        ConsultaMovimentacaoRelatorioVO consultaMovimentacaoRelatorioVO = new ConsultaMovimentacaoRelatorioVO();
        consultaMovimentacaoRelatorioVO.setListaMovimentacao(listaMovimentacoes);
        
        Response response = cliente.target(uri + "/solicitacoes/movimentacoes/relatorio/interno")
                .request().post(Entity.entity(consultaMovimentacaoRelatorioVO, MediaType.APPLICATION_JSON));
        
        try {
            if(response.getStatusInfo().getFamily() != Family.SUCCESSFUL){
                throw new Exception(response.readEntity(ErrorMessage.class).getError());
            } else{
                String json = (String) response.readEntity(String.cla

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know your data model, but maybe you have a cycle in the object model i.e. obj1 refers to obj2 that refers to obj1 again? A simple model mapping would cause infinite recursion which leads to a stack overflow. Maybe the object mapper for obj2 should use a mapper that only maps a subset of obj1 instead to avoid the recursion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...