Is Aspose.word for Java thread safe?

Is Aspose.words for Java thread-safe? I’m considering using it in a multithreaded environment (Tomcat), where several threads would be using it at the same time.

If not, it will be easy to serialize access with a critical section (with some performance hit, of course), so no problem - I just want to know if I have to.

Thanks.

Hi,
Don’t worryJ. At least the half of our customers uses Aspose.Words for Java under Tomcat or other servlet containers.
Afraid, I don’t understand clearly, what you mean about “multithreaded environment (Tomcat)”. Users frequently confuse multithread and multiuser.
All classes of Aspose.Words for Java are not thread safe, as well as vast majority of other java classes and libraries. For instance, see javadoc for old thread-safe java.lang.StringBuffer and for new thread-unsafe java.lang.StringBuilder.
As with StringBuilder, you can instantiate as many com.asopse.words.Document classes as you want in any number of threads. But you should not use the instances concurrently: only one Document instance per one thread. As well you should watch over your template documents and other shared data. Sometimes users are trying to use them simultaneously without synchronization. The users get unexpected results in such cases, of course.
Best Regards,

I’m not worried, I just need to know. It’s no sin to not be thread safe, it just has to be documented so that the programmer can account for it.

Using Tomcat is a bit of a red herring - here, I have a pool of threads, and several get dispatched at once to do different instances of the same job (imaging a Word document).

I understand that each instance of the Document class can only be used by one thread, akin to StringBuilder. I am wondering, however, whether the class itself is threadsafe - ie. if the Document implementation uses some static state variables in an unsynchronized fashion.

So again, I’m not worried, I just want to know whether I need to write (in code destined to be executed by multiple threads simultaneously):

String[] fields = new Document(path).getMailMerge().getFieldNames();

or

// static lock to serialize access for ALL threads
static Object asposeLock = new Object();
…
String[] fields;
synchronized (asposeLock) {
fields = new Document(path).getMailMerge().getFieldNames();
}

Currently I have the first (unsynchronized) way implemented, and in testing under heavy load with several threads I sometimes get an error on one thread inside the Aspose libary:

2009-09-03 17:09:08 ERROR [ sig 8] Exception while generating field values
com.aspose.words.FileCorruptedException: The document appears to be corrupted and cannot be loaded.
at com.aspose.words.Document.a(Unknown Source)
at com.aspose.words.Document.a(Unknown Source)
at com.aspose.words.Document.(Unknown Source)
at com.aspose.words.Document.(Unknown Source)
at xetus.print.custom.WordGenerator.generate(WordGenerator.java:105)
at xetus.print.custom.ImageWordStart.process(ImageWordStart.java:104)
at xetus.print.custom.ImageWordStart.run(ImageWordStart.java:55)
at xetus.model.Signal$Invoker.run(Signal.java:548)
at xetus.util.ThreadPool$PoolThread.run(ThreadPool.java:213)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalStateException: Current state = FLUSHED, new state = CODING
at java.nio.charset.CharsetDecoder.throwIllegalStateException(CharsetDecoder.java:951)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:537)
at asposewobfuscated.lz.Q(Unknown Source)
at com.aspose.words.we.a(Unknown Source)
at com.aspose.words.ad.a(Unknown Source)
at com.aspose.words.cc.read(Unknown Source)
… 9 more