Interruption is not supported when save a word or cell


#1

it costs a long time when converts XLS to XLSX, I want to interrupt the convert process after a period. But the convert process cannot be terminated.

ExecutorService es = Executors.newSingleThreadExecutor();
Future future = null;
try {
Callable callable = new Callable() {
public Integer call() {
int i = 0;
try {
Workbook workbook = new Workbook(srcFilePath);
workbook.save(dstFilePath, FileFormatType.XLSX);
} catch (Exception e) {
System.out.println(“interrupt, then quit 1”);
return new Integer(-1);
}
return new Integer(i);
}
};
future = es.submit(callable);
iRet = future.get(500, TimeUnit.MILLISECONDS);
}
catch (Exception e) {
System.out.println(“interrupt, then quit 2”);
iRet = new Integer(-1);
}
finally {
future.cancel(true);
es.shutdownNow();
}


#2

@dropin,

Thanks for your query.

Aspose.Cells does support to interrupt loading/saving process while working with large MS Excel files, see the document for your reference:
https://docs.aspose.com/display/cellsjava/Aspose.Cells+for+Java+-+Interruptible+Library

Hope, this helps a bit.


#3

Thanks.
And how to interrupt loading/saving process Word files ?


#4

@dropin,

You can use InterruptionToken class instance and bind it to the current thread executing Aspose.Words code. Then calling Interrupt on the token instance should break the execution. Sample inherited class code is as follows:

using System.Threading;
using Aspose.Words;
 
namespace MyCompany
{
    /// <summary>
    /// My Aspose document.
    /// </summary>
    public class MyDocument : Aspose.Words.Document
    {
        public MyDocument(string fileName) : base(fileName) {}
 
        public bool TryToSave(string fileName, int timeout)
        {
            InterruptionToken token = new InterruptionToken();
 
            bool finished = SaveWithTimeout(token,
                () =>
                {
                    token.BindToCurrentThread();
                    Save(fileName);
                }, timeout);
 
            return finished;
        }
 
        private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)
        {
            Thread workerThread = new Thread(threadStart);
            workerThread.Start();
 
            bool finished = workerThread.Join(timeout);
            if (!finished)
            {
                token.Interrupt();
            }
            return finished;
        }
    }
}

Sample usage is as follows:

MyDocument myDoc = new MyDocument(@"C:\BigDoc.docx");
bool done = myDoc.TryToSave(@"C:\BigDoc.pdf", 60000);
Console.WriteLine(done ? "Converted" : "Interrupted by timeout.");

#5

I cannot find InterruptionToken class in JAVA API . so how to do it in JAVA?


#6

Fail to interrupt saving process, and the dstFile has been saved success!

    long startTime = 0;
    long endTime = 0;  
try {
            final InterruptMonitor interruptMonitor = new InterruptMonitor();
            final Workbook workbook = new Workbook(srcFilePath);
            workbook.setInterruptMonitor(interruptMonitor);
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                    // Interrupt the process
                    interruptMonitor.interrupt();
                    System.out.println("Interrupting the save thread at "
                            + System.currentTimeMillis());
                }
            }).start();
            startTime = System.currentTimeMillis();
            workbook.save(dstFilePath, FileFormatType.XLSX);
            System.out.println("finish  workbook.save!");
        } catch (com.aspose.cells.CellsException e ){
            if (e.getCode() == ExceptionType.INTERRUPTED) {
                System.out.println(e.getMessage());
                System.out.println("The save thread finishes at "
                        + System.currentTimeMillis());
            } 
        }
        catch (Exception e) {
            System.out.println(e);
        }


    endTime = System.currentTimeMillis();
    System.out.println("startTime:" + startTime + ",endTime:" + endTime);

Out is ,
Interrupting the save thread at 1531214514754
finish workbook.save!
startTime:1531214514567,endTime:1531214516101


#7

@dropin,

I am afraid, in Aspose.Words for Java we do not have InterruptionToken class because Aspose.Words for Java relies on Java’s Thread functionality. The Java code will look something like this:

public void testThreadInterruption() throws InterruptedException
{
    // run Aspose.Words long operation in a separate thread
    Thread infiniteThread = new Thread(infiniteProcessingLoop());
    infiniteThread.start();

    // interrupt it in a while
    Thread.sleep(2000);
    infiniteThread.interrupt();

    // wait till it dies
    infiniteThread.join(5000);

    // check if it is dead
    if (infiniteThread.getState() == Thread.State.RUNNABLE)
        Assert.fail("Thread didn't stop properly");
}

private static Runnable infiniteProcessingLoop()
{
    return new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                while (true)
                {
                    new Document("pathToTestDoc").updatePageLayout();
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    };
}

#8

@dropin,

I noticed it saves the Excel file too. We will check it out soon.
For the time being could you add a line to your code segment, it should work fine:
e.g
Sample code:


startTime = System.currentTimeMillis();
workbook.save(pdfFilePath, FileFormatType.PDF);//add a variable to specify the .pdf file.
workbook.save(dstFilePath, FileFormatType.XLSX);
System.out.println(“finish workbook.save!”);

A corrupted PDF file might be created with less file size. The XLSX file will not be created.

Let us know if it works fine.


#9

It works fine when add a line to save to PDF, but is there other better solutions ?


#10

So there’s no Interruption in Aspose.Words for Java ? This Java code cannot stop the save process.


#11

@dropin,

Please spare us some time as we need to evaluate it thoroughly and may come with better alternative (if possible).


#12

@dropin,

We have evaluated the issue regarding saving XLSX file in interruption process. We need to improve the relevant internal modules by adding more check points (higher frequency) while saving the Excel files. I have logged a ticket with an id “CELLSJAVA-42677” for your issue. We will look into it soon.

Once we have an update on it, we will let you know here.


#13

@dropin,

This is to inform you that we have fixed your issue (logged earlier as “CELLSJAVA-42677”) now. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.

Keep in touch.


#14

@dropin,

Please try our latest version/fix (attached): Aspose.Cells for Java v18.7.5:

Your issue “CELLSJAVA-42677” should be fixed in it.

Let us know your feedback.
Aspose.Cells for Java v18.7.5.zip (6.3 MB)


#15

The issues you have found earlier (filed as CELLSJAVA-42677) have been fixed in Aspose.Cells for Java 18.8. You can also get the latest Aspose.Cells for Java version from Maven repos. with simple configurations. Please see the document for your reference: https://docs.aspose.com/display/cellsjava/Installation

This message was posted using BugNotificationTool from Downloads module by Amjad_Sahi