怎么中断excel转pdf的转换任务,或有效的控制excel转pdf的转换时长
那word转pdf的转换任务呢
public class StopConversionOrLoadingUsingInterruptMonitor
{
static String outDir = “D:\fcs\test\pdf\209aab3c-6f1b-3c97-81cf-8eecffae490e.pdf”;
//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
{
InputStream in = new FileInputStream("C:\\Users\\LYCIT\\Desktop\\需求\\转换超时的文件\\191\\209aab3c-6f1b-3c97-81cf-8eecffae490e.xlsx");
//Create a workbook object
Workbook wb = new Workbook(in);
//Assign it InterruptMonitor object
wb.setInterruptMonitor(im);
try
{
//Save the workbook to Pdf format
wb.save(outDir);
//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.");
}
}
用这个文档试了一下,没有发生中断
链接: https://pan.baidu.com/s/1FVygAa2F4HOoWgh8AU4SWQ?pwd=5ggi
提取码: 5ggi 复制这段内容后打开百度网盘手机App,操作更方便哦
代码测试里面的excel我放到百度云盘了,可下载测试
@Lycheng
一般情况下,我们需要慎重考虑检查interruption的频率以平衡响应速度和程序性能。 如果我们对interruption的检查过于频繁,则monitor本身会给用户程序带来很大的性能影响。而检查的粒度过大时则会出现响应不及时的情况。
Chart to image 是一个颗粒,我们不会在 chart2image中间去检查 monitor。
那怎么办,还有其它中断转换的方案吗
还有word转pdf的任务怎么中断呢
哦,那word转pdf的任务怎么中断呢
How do we track this issue CELLSJAVA-45363
用wb.calculateFormula();提前计算处理一下能避免这个死循环问题吗
赞!谢谢大家能快速响应!