Stop conversion or loading using InterruptMonitor when it is taking too long
Possible Usage Scenarios
Aspose.Cells allows you to stop conversion of Workbook to various formats like PDF, HTML, etc. using the InterruptMonitor object when it is taking too long. The conversion process is often both CPU and Memory intensive and it is often useful to halt it when resources are limited. You can use InterruptMonitor both for stopping conversion as well as to stop loading huge workbook. Please use Workbook.InterruptMonitor property for stopping conversion and LoadOptions.InterruptMonitor property for loading huge workbook.
Stop conversion or loading using InterruptMonitor when it is taking too long
The following sample code explains the usage of InterruptMonitor object. The code converts quite a large Excel file to PDF. It will take several seconds (i.e. more than 30 seconds) to get it converted because of these lines of code.
//Access cell AB1000000 and add some text inside it.
Cell cell = ws.getCells().get("AB1000000");
cell.putValue("This is text.");
As you see AB1000000 is quite a farther cell in XLSX file. However, the WaitForWhileAndThenInterrupt() method interrupts the conversion after 10 seconds and program ends/terminates. Please use the following code to execute the sample code.
new StopConversionOrLoadingUsingInterruptMonitor().testRun();
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public class StopConversionOrLoadingUsingInterruptMonitor | |
{ | |
static String outDir = Utils.Get_OutputDirectory(); | |
//Create InterruptMonitor object | |
InterruptMonitor im = new InterruptMonitor(); | |
public class ConversionThread extends Thread | |
{ | |
private Thread monitorThread; | |
public ConversionThread(Thread monitorThread) | |
{ | |
this.monitorThread = monitorThread; | |
} | |
//This function will create workbook and convert it to Pdf format | |
void createWorkbookAndConvertItToPdfFormat() throws Exception | |
{ | |
//Create a workbook object | |
Workbook wb = new Workbook(); | |
//Assign it InterruptMonitor object | |
wb.setInterruptMonitor(im); | |
//Access first worksheet | |
Worksheet ws = wb.getWorksheets().get(0); | |
//Access cell AB1000000 and add some text inside it. | |
Cell cell = ws.getCells().get("AB1000000"); | |
cell.putValue("This is text."); | |
try | |
{ | |
//Save the workbook to Pdf format | |
wb.save(outDir + "output_InterruptMonitor.pdf"); | |
//Show successful message | |
System.out.println("Excel to PDF - Successful Conversion"); | |
//stop monitor thread | |
monitorThread.interrupt(); | |
} | |
catch (CellsException ex) | |
{ | |
if(ex.getCode() == ExceptionType.INTERRUPTED) | |
{ | |
System.out.println("Conversion process is interrupted - Message: " + ex.getMessage()); | |
} | |
else | |
{ | |
throw ex; | |
} | |
} | |
} | |
public void run() | |
{ | |
try | |
{ | |
createWorkbookAndConvertItToPdfFormat(); | |
} | |
catch(Exception ex) | |
{ | |
System.out.println("Conversion thread error - Message: " + ex.getMessage()); | |
} | |
} | |
}//ConversionThread | |
public class MonitorThread extends Thread | |
{ | |
//This function will interrupt the conversion process after 10s | |
void waitForWhileAndThenInterrupt() throws Exception | |
{ | |
Thread.sleep(1000 * 10); | |
im.interrupt(); | |
} | |
public void run() | |
{ | |
try | |
{ | |
waitForWhileAndThenInterrupt(); | |
} | |
catch (InterruptedException ex) | |
{ | |
System.out.println("Monitor thread is interrupted - Message: " + ex.getMessage()); | |
} | |
catch (Exception ex) | |
{ | |
System.out.println("Monitor thread error - Message: " + ex.getMessage()); | |
} | |
} | |
}//MonitorThread | |
public void testRun() throws Exception | |
{ | |
MonitorThread monitorThread = new MonitorThread(); | |
ConversionThread conversionThread = new ConversionThread(monitorThread); | |
monitorThread.start(); | |
conversionThread.start(); | |
monitorThread.join(); | |
conversionThread.join(); | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
new StopConversionOrLoadingUsingInterruptMonitor().testRun(); | |
// Print the message | |
System.out.println("StopConversionOrLoadingUsingInterruptMonitor executed successfully."); | |
} | |
}//StopConversionOrLoadingUsingInterruptMonitor |