Java Accessing MTS COM+ Component |
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.
The following software is required to run the example:
This example assumes you have installed:
D:\JDK1.3.1
D:\JINTEGRA
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.
d:\pure\nativemts\javaserver
d:\pure\nativemts\javaserver
\winnt\system32\comsvcs.dll
as the type library.
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:
set your CLASSPATH to include current directory and the J-Integra® runtime (jintegra.jar):
set CLASSPATH=.;d:\jintegra\lib\jintegra.jar
compile your Java server and the proxies generated by com2java:
javac JavaMtsServer.java
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.
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
Compile the proxies generated by java2com:
javac IID*.java
Run regjvm to register a Java virtual machine called "mtsjvm" to be used to access your Java objects:
.
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.
Launch "Administrative tools-Component Services"
Create a directory: d:\pure\nativemts\comclient
Private Sub Command1_Click() Dim JavaServer As New JavaMts.JavaMtsServer MsgBox JavaServer.isInTransaction End Sub |
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:
Create a VB executable by using the "File-Make Project1" menu
item, placing it in d:\pure\nativemts\comclient.
Select the properties of you're component in MTS/COM+ and on the "Transactions"
tab set the transaction value to Requires New
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.