Exception while generating multiple documents

Hi,

I’m generating multiple documents concurrently using Aspose.Words for Java (I’m using a trial license at the moment). While generating multiple documents sometimes I get these types of exceptions when calling document.save method

java.lang.IndexOutOfBoundsException: index
at asposewobfuscated.b.removeAt(SortedIntegerList.java: 173)
at asposewobfuscated.b.remove(SortedIntegerList.java: 186)
at com.aspose.words.ace.dF(ParaPrFiler.java: 724)
at com.aspose.words.me.a(PrFiler.java: 38)
at com.aspose.words.ace.a(ParaPrFiler.java: 37)
at com.aspose.words.ep.gV(DocWriter.java: 408)
at com.aspose.words.ep.visitRowEnd(DocWriter.java: 387)
at com.aspose.words.Row.accept(Row.java: 148)
at com.aspose.words.CompositeNode.acceptChildren(CompositeNode.java: 782)
at com.aspose.words.Table.accept(Table.java: 93)
at com.aspose.words.CompositeNode.acceptChildren(CompositeNode.java: 782)
at com.aspose.words.Body.accept(Body.java: 69)
at com.aspose.words.ep.b(DocWriter.java: 233)
at com.aspose.words.ep.run(DocWriter.java: 168)
at com.aspose.words.ep.a(DocWriter.java: 27)
at com.aspose.words.Document.a(Document.java: 1501)
at com.aspose.words.Document.save(Document.java: 913)
at com.aspose.words.Document.save(Document.java: 886)
at poc.DocumentGenerator.run(DocumentGenerator.java:50)
at java.lang.Thread.run(Unknown Source)

OR

java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at asposewobfuscated.b.a(SortedIntegerList.java: 163)
at asposewobfuscated.b.set(SortedIntegerList.java: 147)
at com.aspose.words.adl.ns(AttrCollection.java: 76)
at com.aspose.words.ace.dF(ParaPrFiler.java: 483)
at com.aspose.words.me.a(PrFiler.java: 38)
at com.aspose.words.ace.a(ParaPrFiler.java: 37)
at com.aspose.words.ep.gV(DocWriter.java: 408)
at com.aspose.words.ep.visitRowEnd(DocWriter.java: 387)
at com.aspose.words.Row.accept(Row.java: 148)
at com.aspose.words.CompositeNode.acceptChildren(CompositeNode.java: 782)
at com.aspose.words.Table.accept(Table.java: 93)
at com.aspose.words.CompositeNode.acceptChildren(CompositeNode.java: 782)
at com.aspose.words.Body.accept(Body.java: 69)
at com.aspose.words.ep.b(DocWriter.java: 233)
at com.aspose.words.ep.run(DocWriter.java: 168)
at com.aspose.words.ep.a(DocWriter.java: 27)
at com.aspose.words.Document.a(Document.java: 1501)
at com.aspose.words.Document.save(Document.java: 913)
at com.aspose.words.Document.save(Document.java: 886)
at poc.DocumentGenerator.run(DocumentGenerator.java:50)

Does anyone know whats the cause of this exception. From what I can see, the stack trace is the same upto this point - PrFiler.java:38 but changes after that. I can post more details about my code if that helps.

Thanks in advance for any help…

Hi

Thanks for your request. Could you please attach the document you are getting issues with? I will check the problem on my side and provide you more information.
Best regards,

Thanks Andrey for the quick response. I am taking a document as a template and inserting some other sub-templates into it using NodeImporter class. The actual Document object which gets inserted is shared by all threads. Can that result in this problem? If yes, then what is the correct way to insert a document into another when there are concurrent threads trying to use NodeImporter on the same template document? I’m not sure if that’s the problem as the exception arises while saving the document. I can send you my code and the templates that I’m using but can that be private. I can’t send you the templates/code in an open forum due to my company’s policies…

Hi
Thanks for your request. You cannot use document object in multiple threads. Aspose.Words is multithread safe as long as only one thread works on a document at a time. It is a typical scenario to have one thread working on one document. Different threads can safely work on different documents at the same time.
Best regards,

Hey Andrey, thanks for your reply. My code works like this (the code uses the insertDocument method which is described here)

class DocumentGenerator implements Runnable
{
    public void run()
    {
        try
        {
            String filePath = createUniqueFile(); //this method returns path to a unique template which will only be used by this thread
            Document pocDocument = new Document(filePath);
            DocumentBuilder builder = new DocumentBuilder(pocDocument);
            builder.moveToMergeField("start_section2");
            Document companySection = new Document("C:\aspose\company_details_section.doc"); //will be shared by all threads
            insertDocument(builder.getCurrentParagraph(), companySection);
            pocDocument.save(filePath); //exception here
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

The document which actually gets modified is unique for each thread. But the “company_details_section.doc” document which is inserted into the main document is shared between all threads. Since the shared document isn’t actually modified so I thought it would work fine. Is there a workaround for this? I can’t create copies of “company_details_section.doc” and if I synchronize its access, the performance of the code will be severely effected.

Hi

Thank you for additional information. Could you also please attach your template and source document here? It will help as in my testing. It is safe to attach files in the forum. If you attach your document here, only you and Aspose staff members can download it. Also you can send the file to my e-mail as described here
https://forum.aspose.com/t/aspose-words-faq/2711
Best regards,

Hi
Thanks for your request. Actually, you can use the same document in multiple threads, but you should create a separate instance of the Document object for each thread. So just create an instance of Document for each thread. This should resolve the problem.
Best regards,

I am actually creating a new Document object for each thread, I tried it both ways i.e. creating a new instance for each thread and using the same instance in each thread. In both cases I’m getting the exception. I’m attaching my code (its an Eclipse project) and the templates that I’m using for your testing. The code is a unsystematic as it was created in a hurry for testing so if you need some help with the code let me know . The main method is in the poc.PocDocumentGenerator class and the paths to the templates have to be set in poc.Constants interface…

Hi there,
Thanks for your inquiry.
I was unable to reproduce the issue on my side. Could you please try changing your logic a bit so the CompanySection document is loaded in once and a cloned instance of the document is used in each thread instead.
If the exception still occurs then it must be something else which is the problem as cloning the document in memory avoids any sort of disk related calls.
Thanks,