We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Stop PDF conversion after X seconds

Hi,

I would like to achieve an interruption of a conversion process from cells -> pdf if it takes longer than X seconds. I have found the following article, but it’s in C#. Some commands are not available in Java, so my code doesn’t work.

For example, there are no ThreadStart.start() or ThreadStart.join() methods in Java.

Can you please guide me how to achieve the interruption in Java?

Thanks!

@enigma969,

See the Java document on how to stop conversion or loading using InterruptMonitor when it is taking too long for your reference, similar document with exmaple code is already written in Java.

Hi,

thanks very much. I’m using the code of the example now, but now every conversion takes X seconds, even if the document is empty.

Is there a way to abort the waiting if the conversion was successful already?

Greets

@enigma969,

The example code in the document is just for reference. You got to write/update your code for your needs accordingly. If you still could not evaluate, kindly share your sample code (runnable) that you are using, we will check and help you through.

@enigma969

You can stop the monitor thread if conversion is ended before monitor time limit: MonitorThread.interrupt();

Code:

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 and add some text inside it.
            // Cell cell = ws.getCells().get("AB1000000");
            Cell cell = ws.getCells().get("A1");
            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 thread 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