[Enterprise Extensions only]

ActiveX to EJB bridge, using threading

The ActiveX to EJB bridge supports both free-threaded and apartment-threaded access and implements the Free Threaded Marshaler to work in a hybrid environment such as Active Server Pages. Each thread that is created in the ActiveX process will be mirrored in the Java environment when the thread communicates through the ActiveX to EJB bridge. In addition, once all references to Java objects (there are no JObjectProxy or JClassProxy objects) in a ActiveX thread, the ActiveX to EJB bridge will detach the thread from the JVM. Therefore, you must 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 don't really need to worry about threading issues in the Java programs they access. Active Server Pages and multi-threaded C and C++ programs do need to worry.

Consider the following scenario:

  1. A multi-threaded Windows Automation Container (our ActiveX Process) starts. It exists on Thread A.
  2. The ActiveX Process initializes the ActiveX to EJB bridge, which starts the JVM. The JVM attaches to the same thread and internally calls it Thread1.
  3. The ActiveX Process starts two threads: B and C.
  4. Thread B in the ActiveX Process uses the ActiveX to EJB 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, so the VM never needs to attach to it. This is a case where the JVM doesn't 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. Java's Thread 2 is detached.
  7. Thread B again uses the ActiveX to EJB bridge to access an object that was created in Thread A. The JVM again attaches to thread and calls it Thread 3.

ActiveX Process JVM Access by ActiveX Process
Thread A - Created in 1 Thread 1 - Attached in 2
Thread B - Created in 4 Thread 2 - Attached in 4, detached in 6 Thread 3 - Attached in 7
Thread C - Created in 4  


Threads and Active Server Pages

Active Server Pages in Microsoft's Internet Information Server is a multi-threaded environment. When you create the XJB.JClassFactory object, you can store it in the Application collection as an Application-global object. This allows all threads within your ASP environment to access the same ActiveX to EJB bridge object. Active Server Pages by default creates 10 Apartment Threads per ASP process per CPU. This means that when your ActiveX to EJB bridge object is initialized it can be called by any of the 10 threads, not just the thread who created it.

If you need to simulate single-apartment behavior, you can create a Single-Apartment Threaded ActiveX DLL in Visual Basic and encapsulate the ActiveX to EJB bridge object there. This guarantees that all access to the JVM uses 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: