Java - Jacob Multitheading

762 views Asked by At

In my website, there's a feature which opens an Excel file on the machine and deals with it thanks to Jacob library (jacob-1.14.3).

This code is called from a new thread everytime:

//In a new thread everytime
ActiveXComponent excelApp = null;
try {
    info("Openning Excel application..", 1);

    ComThread.InitSTA();
    //ComThread.InitMTA();  
    //ComThread.InitSTA(true);
    //ComThread.InitMTA(true);

    excelApp = new ActiveXComponent("Excel.Application");
    excelApp.setProperty("DisplayAlerts", false);
    excelApp.setProperty("Visible", false);


    [...]

catch (Exception e) {
    throw e;
} finally {
    if (excelApp != null) {
        info("Closing Excel app..", 1);
        excelApp.invoke("Quit");
        ComThread.Release();
        System.gc();
    }
}

As soon as there are more than 1 thread dealing with Excel, I get this error:

com.jacob.com.ComFailException: Can't map name to dispid: XXX

XXX stands for any property I'm looking for (Name, Workbooks, Quit, etc.).The error doesn't appear with 1 single thread running.

After spending hours on the internet, I only have a documentation that I don't understand: http://danadler.com/jacob/JacobThreading.html

I've tried ComThread.InitSTA/MTA functions but nothing seems to solve the problem.

How can I make multiple Excel instances communicate with my app from different threads?

0

There are 0 answers