在转换或加载花费太长时间时使用InterruptMonitor停止转换或加载
可能的使用场景
Aspose.Cells允许您在使用转换Workbook为PDF、HTML等各种格式时使用InterruptMonitor对象当转换时间过长时停止转换。转换过程通常既具有CPU又具有内存密集型,当资源有限时停止转换通常是有用的。您可以使用InterruptMonitor既用于停止转换又用于停止加载巨大的工作簿。请使用Workbook.InterruptMonitor属性停止转换,使用LoadOptions.InterruptMonitor属性加载巨大的工作簿。
在转换或加载花费太长时间时使用InterruptMonitor停止转换或加载
以下示例代码说明了 InterruptMonitor 对象的用法。该代码将一个非常大的 Excel 文件转换为 PDF。由于这些代码行,它需要数秒(即 超过 30 秒)来完成转换。
//Access cell AB1000000 and add some text inside it.
Cell cell = ws.getCells().get("AB1000000");
cell.putValue("This is text.");
如您所见,AB1000000 在 XLSX 文件中是相对较远的单元格。然而,WaitForWhileAndThenInterrupt() 方法在 10 秒后中断转换,程序结束/终止。请使用以下代码来执行示例代码。
new StopConversionOrLoadingUsingInterruptMonitor().testRun();
示例代码
// 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 |