Java Accessing MTS COM+ Component

Java/J2EE COM Interoperability Products Page

This is a simple step-by-step example showing you how you can take advantage of the power of MTS and COM+ to host your Java objects using any JVM.

Prerequisites

The following software is required to run the example:

This example assumes you have installed:

The J-Integra® bin directory must be in your PATH at the System level, since the J-Integra® runtime (jintegra.jar) needs to load native code from there.

The Steps

Prepare the Java Server

  1. Create a directory such as d:\pure\nativemts\javaserver
  2. Open a DOS window and change directory to d:\pure\nativemts\javaserver
  3. Run the J-Integra® 'com2java' tool from the command line. If the command is not found, then you have not put the J-Integra® 'bin' directory into your PATH at SYSTEM leve.
  4. In the main dialog box, select \winnt\system32\comsvcs.dll as the type library.
  5. Enter d:\pure\nativemts\javaserver into the Output Dir field
  6. Enter comservices into the Java Package field
  7. Click on the Generate Proxies...

You have just generated Java proxies which allow you to access MTS/COM+ objects such as ObjectControl and ObjectContext.

Next cut and paste the following code from your browser into a new file called d:\pure\nativemts\javaserver\JavaMtsServer.java

import comservices.*;
import com.linar.jintegra.Dispatch;

public class JavaMtsServer implements ObjectControl {

  IObjectContext context;

  public void activate() {
    try {
      context = new IObjectContextProxy(Dispatch.getMtsObjectContext());
    } catch (Exception e) {
    }
  }

  public String isInTransaction() {
    try {
      if (context != null) {
        if (context.isInTransaction() == 0) {
          return "Object has no transaction";
        } else {
          context.setComplete();
          return "Object has a transaction";
        }
      } else {
        return "Object not in MTS";
      }
    } catch (Exception e) {
      return e.toString();
    }
  }

  public void deactivate() {
  }

  public boolean canBePooled() {
    return false;
  }
}

The remaining Java server steps all involve commands invoked from the command line of your DOS window:

  1. set your CLASSPATH to include current directory and the J-Integra® runtime (jintegra.jar):

    set CLASSPATH=.;d:\jintegra\lib\jintegra.jar

  2. compile your Java server and the proxies generated by com2java:

    javac JavaMtsServer.java

  3. Next you will generate proxies to allow your Java server to be accessed by COM. Run

    java2com from the command line.

    Specify JavaMtsServer as the class to be analyzed and JavaMts as the name of the idl file.

  4. You need to compile the generated IDL file using the Microsoft idl compiler (midl.exe). This is available with Visual C++. There is a batch file called VCVARS32.BAT in the Visual C++ bin directory which set your environment variables appropriately: Compile the generated IDL file:

    midl JavaMts.idl

  5. Compile the proxies generated by java2com:

    javac IID*.java

  6. Run regjvm to register a Java virtual machine called "mtsjvm" to be used to access your Java objects:

  7. Run regtlb to associate your new JVM name ("mtsjvm") with your newly generated type library (generated by MIDL):

    regtlb JavaMts.tlb mtsjvm

You have now finished the steps required to set up your Java server.

Registering your Java server under COM+

  1. Launch "Administrative tools-Component Services"

  2. Expand console root tree to "Console Root-Component Services-Computers-My Computer-COM+ Applications"
  3. Select the node "COM+ Applications" and create a new application Make sure it is an "Empty application" and call it "JavaMtsApplication". Leave all other settings set to their default values.
  4. Expand the JavaMtsApplication tree, click on the components folder and create a new component: Select "Import component(s) that are already registered". Select JavaMts.JavaMtsServer from the list.

Building COM client

  1. Create a directory: d:\pure\nativemts\comclient

  2. Launch Visual Basic and create a standard.exe project
  3. Under the Projec|References... menu item find the reference labelled "JavaMts generated from JavaMtsServer (J-Integra®)..." and check the box next to it.
  4. Place a Button in your default form with the caption "Get Transaction Status"
  5. Double click on the button to edit the code associated with it, and add the following code:

    Private Sub Command1_Click()
        Dim JavaServer As New JavaMts.JavaMtsServer
        MsgBox JavaServer.isInTransaction
    End Sub

  6. Hit F5 to start your application running. Click on your button. Your COM server should be activated and you should see the following message:

When running under Visual Basic in this way the object does not run in MTS. In order to run your component under MTS, do the following:

  1. Create a VB executable by using the "File-Make Project1" menu item, placing it in d:\pure\nativemts\comclient.

  2. Select the properties of you're component in MTS/COM+ and on the "Transactions" tab set the transaction value to Requires New

  3. Double click on your VB executable again. You should see:

Conclusion

In this example you have invoked a Java object from Visual Basic, and the Java object has in turn invoked a COM object.

You now know that you can select any JVM for your Java component development for MTS, and even integrate existing Java components into MTS.