Konvertierung oder Laden mit InterruptMonitor stoppen, wenn es zu lange dauert
Mögliche Verwendungsszenarien
Aspose.Cells ermöglicht es Ihnen, die Konvertierung von Arbeitsmappen in verschiedene Formate wie PDF, HTML usw. mit dem InterruptMonitor-Objekt zu stoppen, wenn es zu lange dauert. Der Konvertierungsprozess ist oft sowohl CPU- als auch speicherintensiv und es ist oft nützlich, ihn anzuhalten, wenn die Ressourcen begrenzt sind. Sie können InterruptMonitor sowohl zum Stoppen der Konvertierung als auch zum Stoppen des Ladens riesiger Arbeitsmappen verwenden. Verwenden Sie Workbook.InterruptMonitor Eigenschaft, um die Konvertierung zu stoppen, und LoadOptions.InterruptMonitor Eigenschaft, um riesige Arbeitsmappen zu laden.
** Konvertierung oder Laden mit InterruptMonitor stoppen, wenn es zu lange dauert**
Der folgende Beispielcode erläutert die Verwendung des InterruptMonitor-Objekts. Der Code konvertiert eine ziemlich große Excel-Datei in PDF. Es dauert mehrere Sekunden (d. h. länger als 30 Sekunden), um sie zu konvertieren, weil diese Zeilen Code vorhanden sind.
//Access cell AB1000000 and add some text inside it.
Cell cell = ws.getCells().get("AB1000000");
cell.putValue("This is text.");
Wie Sie sehen, befindet sich AB1000000 ziemlich weit entfernt in der XLSX-Datei. Die WaitForWhileAndThenInterrupt()-Methode unterbricht jedoch die Konvertierung nach 10 Sekunden, und das Programm endet/terminiert. Bitte verwenden Sie den folgenden Code, um den Beispielcode auszuführen.
new StopConversionOrLoadingUsingInterruptMonitor().testRun();
Beispielcode
// 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 |