Threading tips

The ActiveX to enterprise bean bridge supports both free-threaded and apartment-threaded access and implements the Free Threaded Marshaler to work in a hybrid environment (such as the Active Server Pages environment). Each thread created in the ActiveX process is mirrored in the Java environment when the thread communicates through the ActiveX to enterprise bean bridge.

In addition, when all references to Java objects (there are no JObjectProxy or JClassProxy objects) are loaded in an ActiveX thread, the ActiveX to enterprise bean bridge detaches the thread from the JVM code. Therefore, be careful that any Java code that you access from a multi-threaded Windows application is thread-safe. Visual Basic and VBScript applications are both essentially single-threaded. Therefore, Visual Basic and VBScript applications do not have threading issues in the Java programs they access. Active Server Pages and multi-threaded C and C++ programs can have issues.

Consider this scenario:

  1. A multi-threaded Windows Automation Container (the ActiveX Process) starts. It exists on Thread A.
  2. The ActiveX Process initializes the ActiveX to enterprise bean bridge, which starts the JVM code. The JVM attaches to the same thread and internally calls it Thread 1.
  3. The ActiveX Process starts two threads: B and C.
  4. Thread B in the ActiveX Process uses the ActiveX to enterprise bean bridge to access an object that was created in Thread A. The JVM attaches to thread B and calls it Thread 2.
  5. Thread C in the ActiveX Process never talks to the JVM code, so the VM never needs to attach to it. This is a case where the JVM code does not have a one-to-one relationship between ActiveX threads and Java threads.
  6. Thread B later releases all of the JObjectProxy and JClassProxy objects that it used. The Java Thread 2 is detached.
  7. Thread B again uses the ActiveX to enterprise bean bridge to access an object that was created in Thread A. The JVM code attaches again to the thread and calls it Thread 3.

This list shows the ActiveX threads and in which step of the scenario they were created:

This list shows the JVM threads and in which step of the scenario they were attached:

Threads and Active Server Pages

The Active Server Pages (ASP) environment in Microsoft's Internet Information Server is multi-threaded. When you create the XJB.JClassFactory object, you can store it in the Application collection as an Application-global object. All threads within your ASP environment can now access the same ActiveX to enterprise bean bridge object. Active Server Pages by default creates 10 Apartment Threads per ASP process per CPU. This means that when your ActiveX to enterprise bean bridge object is initialized any of the 10 threads can call this object, not just the thread that created it.

To simulate single-apartment behavior, you can create a Single-Apartment Threaded ActiveX Dynamic Link Library (DLL) in Visual Basic and encapsulate the ActiveX to enterprise bean bridge object there. This encapsulation guarantees that all access to the JVM object is on the same thread. You need to use the <OBJECT> tag to assign the XJB.JClassFactory to an Application object and must be aware of the consequences of introducing single-threaded behavior to a Web application.

The Microsoft KnowlegeBase has several articles about ASP and threads, including these:

The Microsoft KnowledgeBase is located at http://support.microsoft.com. Link outside Information Center