Java – Proper Usage of JRSwapFileVirtualizer

exportjasper-reportsjava

I am exporting a very huge jasper report (around 20000 pages). For that purpose in order to avoid out of memory error, as suggested by different websites, I am using JRSwapFileVirtualize as shown below:

virtualizer = new JRSwapFileVirtualizer(1000, new JRSwapFile(reportFilePath, 2048, 1024), true);
parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
.
.
JasperPrint print = JasperFillManager.fillReport(report, parameters, list);
virtualizer.cleanup();

My questions are:

1) I have configured maxSize as 1000. Is it ok for a report of 20000 pages?

2) I am cleaning up virtualizer immediately after method call 'fillReport'. Is it correct or I need to clean up the virtualizer after I export the report to XLS?

3) Is anyway cleaning up the virtualizer after exporting the report, helpful to me?

4) Creating JasperPrint object, logic is written on server side. Exporting report logic, is written in java client. If I am supposed to clean up virtualizer, after exporting the report, how do I handle this scenario? Because in this case I need to create static object (because JRSwapFileVirtualizer is not serializable so I can not create and pass it inbetween server and client) of this virtualizer on server side and then again after export i need to call server side method to clean up virtualizer. I was thinking creating static object is a risk because, if multiple calls are made to server side logic, then virtualizer static object will hold reference to swap file created by latest call and because of this after clean up, previous swap files may not get deleted.

I found more information on this topic:
In the sample provided by jasper for using virtualizer, they are cleaning up the virtualizer only when the call common export, which exports more than one type of files. They are not calling clean up, when they export only single type of report:

else if (TASK_CSV.equals(taskName))
{
    exportCSV(jasperPrint);
}       
else if (TASK_EXPORT.equals(taskName))
{
    exportPDF(jasperPrint);
    exportXML(jasperPrint, false);
    exportHTML(jasperPrint);
    exportCSV(jasperPrint);

    // manually cleaning up
    virtualizer.cleanup();
}

Also they have not mentioned XLS export in this example. I found another link where the person has faced the same issue, but there also no solution mentioned either:

http://www.jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=95689

Also this link says we can use JRXlsxExporter instead. But JRXlsxExporter exports only excel 2007 format not in 2003.

I am using JExcelApiExporter.exportReport() to export the reports to XLS, and for huge reports it is not working.
Please help me to sort out this issue.
Any small hint or suggestion is appreciable. Let me know, if you require more information.
Thanks.

Best Answer

1.For your first question, It really depends upon how much heap size you have given for your report generation app. Try trial-error scenarios & you will get exact size

for your second question

2.As per you first code snippet, You are filling report and immediately applying cleanup on virtualizer. So even if you have created report, after cleanup your swap file will be destroyed & since your data. -- You should apply cleanup on virtualizer, once you are confirmed about completion of report data usage. So in this case after exporting to particular format.

3.cleanup is to destroy swap file immediately. So if you are not going to reuse the report after exporting applying cleanup is best idea. Note: even if you dont cleanup it explicitly, garbage collector will destroy it on the disposal of jasperPrint.

Your 4th point is quite unclear to me, you are saying jasperPrint has been creating on server side.... and export logic is been written on client side... how export logic could be on client side without having reference to JasperPrint,which ison server side....??1!!

Related Topic