use of au.com.vaadinutils.jasper.ui.CleanupCallback in project VaadinUtils by rlsutton1.
the class JasperManager method run.
@Override
public void run() {
JRSwapFileVirtualizer fileVirtualizer = null;
CleanupCallback cleanupCallback = null;
boolean initialized = false;
try {
logger.warn("{} permits are available", concurrentLimit.availablePermits());
concurrentLimit.acquire();
initialized = true;
inQueue = false;
queueEntry.setStatus("Gathering report data phase 1");
reportProperties.initDBConnection();
cleanupCallback = reportProperties.getCleanupCallback();
List<ReportParameter<?>> extraParams = reportProperties.prepareData(params, reportProperties.getReportFileName(), cleanupCallback);
compileReport();
if (reportProperties.getCustomReportParameterMap() != null) {
boundParams.putAll(reportProperties.getCustomReportParameterMap());
}
if (extraParams != null) {
params.removeAll(extraParams);
params.addAll(extraParams);
}
logger.info("Running report " + reportProperties.getReportFileName());
for (ReportParameter<?> param : params) {
for (String parameterName : param.getParameterNames()) {
bindParameter(param, parameterName);
if (param.displayInreport()) {
// populate dynamically added parameters to display user
// friendly parameters on the report
boundParams.put("ParamDisplay-" + parameterName, param.getDisplayValue(parameterName));
}
logger.info(parameterName + " " + param.getValue(parameterName));
}
}
reportProperties.prepareForOutputFormat(exportMethod);
queueEntry.setStatus("Gathering report data phase 2");
// use file virtualizer to prevent out of heap
String fileName = "/tmp";
JRSwapFile file = new JRSwapFile(fileName, 100, 10);
fileVirtualizer = new JRSwapFileVirtualizer(500, file);
boundParams.put(JRParameter.REPORT_VIRTUALIZER, fileVirtualizer);
if (stop) {
return;
}
if (exportMethod == OutputFormat.CSV) {
boundParams.put(JRParameter.IS_IGNORE_PAGINATION, true);
}
JasperPrint jasper_print = fillReport(exportMethod);
queueEntry.setStatus("Waiting for browser to start streaming");
if (stop) {
return;
}
logger.warn("Output stream is ready");
progressListener.outputStreamReady();
logger.warn("Waiting for reader stream");
if (readerReady.await(10, TimeUnit.SECONDS)) {
outputStream = new PipedOutputStream(inputStream);
writerReady.countDown();
exportMethod.export(jasper_print, outputStream, getPageMonitorConfig(exportMethod.getConfig()), images);
imagesrcs = (images.size() <= 0) ? null : new DataSource[images.size()];
if (imagesrcs != null) {
int xi = 0;
for (Map.Entry<String, byte[]> entry : images.entrySet()) {
ByteArrayDataSource image = new ByteArrayDataSource(entry.getValue(), "image/gif");
image.setName(entry.getKey());
imagesrcs[xi++] = image;
}
}
} else {
logger.error("Couldn't attach to reader stream");
}
Thread.sleep(750);
queueEntry.setStatus("Cleaning up");
} catch (Exception e) {
logger.error(e, e);
} finally {
if (queueEntry != null) {
jobQueue.remove(queueEntry);
queueEntry = null;
}
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error(e, e);
}
if (cleanupCallback != null) {
try {
cleanupCallback.cleanup();
} catch (Exception e) {
logger.error(e, e);
}
}
if (fileVirtualizer != null) {
try {
fileVirtualizer.cleanup();
} catch (Exception e) {
logger.error(e, e);
}
}
if (initialized) {
concurrentLimit.release();
try {
reportProperties.closeDBConnection();
} catch (Exception e) {
logger.error(e, e);
}
}
completeBarrier.countDown();
if (progressListener != null) {
progressListener.completed();
}
}
}
Aggregations