時間がかかりすぎる場合は、InterruptMonitor を使用して変換またはロードを停止します

考えられる使用シナリオ

Aspose.Cells を使用すると、PDF、HTML などのさまざまな形式へのワークブックの変換を停止できます。割り込みモニター時間がかかりすぎる場合はオブジェクト。多くの場合、変換プロセスは CPU とメモリの両方を集中的に使用するため、リソースが限られている場合に停止すると便利な場合がよくあります。使用できます割り込みモニター変換を停止するためと、巨大なワークブックのロードを停止するための両方です。使ってくださいWorkbook.InterruptMonitor変換を停止するプロパティとLoadOptions.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.");

ご覧のとおりAB1000000XLSX ファイルのかなり遠いセルです。しかし*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