}
return result;
}
}
Mark the class as COM and MTS enabled.
To use the class in MTS you must mark the class as a COM and MTS class. Visual J++
provides a simple way to mark a class as a COM and MTS class through the Class Outline.
Activate the Class Outline by opening JavaServer.java, selecting Other Windows in the View
menu and selecting Document Outline. Right click the JavaServerClass in the Class Outline
window and select Class Properties. Check the MTS Support Enabled checkbox. Select
Supported in the drop down list. You will notice new code appear in JavaServerClass.java
marking the class as a COM and MTS class that looks something like the following:
/**
* @com.register ( clsid=A6D25C85-B6DC-11D1-9BAD-00A0C905438F,
typelib=A6D25C86-B6DC-11D1-9BAD-00A0C905438F )
* @com.transaction (required)
*/
Build the project.
To build the project simply choose Build under the Build menu. Building the project
creates a COM aware DLL file called JavaServer.dll. This DLL contains the
JavaServerClass.class and the appropriate stub code so that the DLL can be registered just
like any COM DLL written in C++ or Microsoft® Visual Basic®. This DLL file will be deployed to MTS in
the next step.
Deploy your DLL into Microsoft Transaction Server.
Start the MTS Explorer and create a new package named JavaServer. If you've never
created a new MTS package, see "Creating MTS Packages" in the Microsoft
Transaction Server Administrator's Guide in Microsoft Transaction Server Help. Add the new
JavaServer.dll component to the JavaServer package you created. Be sure to select Install
new components in the MTS Explorer Component Wizard rather than Import components.
When we use the MTS Explorer to deploy our component, it will see the required
attribute that Visual J++ marked the class with and recognize our component as needing a
transaction. Alternatives to required are supported, notSupported, and requiresNew.
If you make changes to the project and rebuild it after deploying to MTS, you will need
to re-register your component in MTS. You can do this by right clicking My Computer in the
MTS Explorer and selecting Refresh all components.
Now that we have written, built, and deployed an MTS Java component; we need a client
that can consume the component. The client can be written in Java, Visual Basic or C++.
The following explains the steps for creating a simple Windows Foundation Classes (WFC)
based client using Visual J++ 6.0 that can consume the MTS component for Java we created
in the previous section. For more information about WFC see the "Introduction to WFC
Programming" section in the Visual J++ 6.0 Programmers Guide.
Create a new Windows Application.
You can easily create a Windows Application project in Visual J++ by selecting New
Project in the File menu then choosing the Windows Application project template under
Visual J++ Applications in the tree view. Name the project JavaClient, be sure that Close
current solution is selected and choose Open .
Add COM wrappers for Java MTS component.
Although both the component and the client are written in Java, the client must access
the component through COM rather than a direct Java call. Creating and calling the
component through COM allows MTS to hook into the creation of the compoonent and provide
the right services. An additional benefit of using COM is that it allows us to communicate
with this component between machines in the future.
Setup COM access to the Java MTS component by adding COM wrappers to the component.
Select Add COM Wrapper in the Project menu. Check the box next to JavaServer in the list
of installed COM components in the COM Wrappers dialog and press OK. You will notice a
package called javaserver has been added to the JavaClient project.
Add code to call the Java MTS component from the Java client.
Now we need to add code to the application's form to interact with the Java MTS
component. First, lets add a label to the form. JavaClient has a default form called
Form1. Double click Form1.java in the Project Explorer and Form1 will appear in the Form
Designer. Select Toolbox in the View menu to view the toolbox and select the WFC Controls
tab. Drag a Label onto the form. It will be named label1 by default.
Double click on the Form1 in the Form Designer and the code for the click event for the
form will appear. Add code to import the javaserver COM wrappers by placing the following
import statement after the existing import statements in Form1.java:
import javaserver.*;
Add code to call the Java MTS component from the form's click event method by placing
code that calls the Java MTS component method and updates the label with the results. The
Form1_click method should look something like the following:
private void Form1_click(Object sender, Event e)
{
JavaServerClass_Dispatch server = JavaServerClass_Dispatch)new
JavaServerClass();
label1.setText(server.MTSMethod());
}
When JavaClient runs and we click on the form, a call will be made to the Java MTS
component and a transaction will be started. If the component executes without error, the
call to MTSMethod will update the result, call SetComplete() and the transaction will be
committed. The string will be returned to the client and lable1 will be updated with the
text "Hello from JavaServer".
Build and Run the Client on the local machine.
Select Build under the Build menu to build the project. Building the project creates a
Windows executable with all the class files needed to execute JavaClient, including the
COM wrappers for JavaServer, packaged into one executable file.
Run the client.
If the JavaClient project and the JavaServer project are part of the same solution,
before you can run the client from the environment you must first mark the JavaClient
project as the startup project. Right click on JavaClient in the Project Explorer and
select "Set as startup project" in the context menu. To run the client select
Start from the Debug menu. Run JavaClient and click on the form. After a short pause, you
should see the label updated with the text "Hello from JavaServer". You can view
the transaction statistics of the component as it is being called from the client in the
Transaction Server Explorer.
With Visual J++ 6.0 you can debug MTS components within the Visual J++ Integrated
Development Environment (IDE). To debug your Java MTS component in Visual J++ use these
steps to set up debugging. Each of these steps is made either inside the MTS Explorer or
inside of a Visual J++ session with your JavaServer COM DLL project.
Shutdown server processes using the MTS Explorer. To do this, right-click My Computer,
and select Shutdown Server Process.
In your Visual J++ session, select JavaServer Project Properties from the Project menu
and select the Launch tab. Select Custom to activate the custom lunch properties. Set the
Program field to the full path of the Mtx.exe process, for example:
"c:\winnt\system32\MTx.exe"
In the same property sheet, set the Arguments field to the following string:
/p:”JavaServer”. Be sure that there are no spaces in the argument.
Set breakpoints in the JavaServer component DLL source, and you are ready to debug.
Select Start from the Debug menu to run the process.
Now run JavaClient and the breakpoints you set in step 4 should be hit.
Note Debugging MTS Java components with Visual J++ 6.0 requires Microsoft® Windows NT® 4.0 Service
Pack 4 to work correctly.
Once you have written and debugged the MTS Java component you will want to setup the
the component on a machine other than your development machine such as a staging server or
a production server. It is very easy to setup your component on a different machine. All
you have to do is copy the Java MTS component DLL (JavaServer.dll in this case) to the
other machine and then register the component in the MTS Explorer the same way you did on
the development machine. See step 5 of Writing and Building an MTS Java Component above
for the steps to register the component in MTS.
With Visual J++ version 6.0 you can package all the files required to run and
register your client application in a single self-extracting setup executable. The
excellent capabilities of Microsoft® Visual Studio® make this very easy.
Create a self-extracting setup project
To create a self-extracting setup project, select New Project from the File menu and
then select Self-Extracting Setup under Visual Studio, Distribution Units. Name the
project JavaClientSetup and select Open.
Choose the files to be included in the setup.
The setup must contain all the files required to run the client. JavaClient.exe
contains most, but not all the files required. In addition, we need to include two
important files that allow the client on one machine to communicate with the MTS Java
component on the separate server machine. These files are JavaServer.tlb and
JavaServer.vbr.
Select Add Item in the Project menu and browse to the JavaClient project directory.
Select the file JavaClient.exe and select Open to add it to the setup. Select Add Item in
the Project menu, but this time browse to the JavaServer project directory. Select the
files JavaServer.tlb and JavaServer.vbr and select Open.
Configure the remote components
Here is where the .tlb and .vbr files that we added to the project in the previous step
come into play. The setup has to know how to configure the client machine to communicate
with the MTS Java component on the server. The .tlb and .vbr file work together to supply
this information to the setup. First the setup must be made aware of the remote component.
Select JavaClientSetup Properties from the Project menu and select the Remote
Components tab. JavaServer should be in the list of Remote Components. Check the box next
to JavaServer. Doing this activates the remote server machine options. You can choose a
remote server machine that the setup will always use by entering the name of the machine
in the Use Remote Server text box. Or you can check the Always prompt user for server so
that the setup will always ask when setup is run. Press OK or Apply to commit the
settings.
Configure the target settings
You can customize various settings of the setup by selecting JavaClientSetup Properties
from the Project menu and selecting the Target tab. In this case, change the Application
title and Installation folder to JavaClient. Check the Create an entry in the Start menu
check box and change the Shortcut name to JavaClient. Finally enter the following string
into the Command line Program text box: <appdir>\JavaClient.exe. Now when you run
your setup on a machine a Start menu item will allow you to quickly and easily run the
client application.
Build the self-extracting setup
To create the setup program select Build in the Build menu. You now have all the files
and code required to setup the Java client that access the MTS Java component in a single,
self-extracting setup program.
Note Self-extracting setups created with Visual J++ 6.0 Technology Preview 1 do
not include redistributable files required for setting up WFC and other important files.
Without these files, the client will not execute correctly. The final released version of
Visual J++ 6.0 will include these files in the setup. Until then, applications installed
with a self-extracting setup will only run on a machine with Visual J++ 6.0 Technology
Preview 1 installed.
Signing your setup.
One great feature of the self-extracting setup project is that it automatically signs
the setup for you. A test certificate is automatically used by default, but you can
customize this setting by selecting the project Properties in the Project menu and
selecting the Signing tab.
Deploy and run the client
You can deploy your client setup from a web page, an FTP site, or whatever way is
convenient. As a simple test, run JavaClientSetup.exe on a separate machine. When prompted
for the remote machine name enter the name of the machine you setup the JavaServer
component on. Launch JavaClient from the Start menu entry. When the JavaClient form
appears, click on the form. After a short pause, you should see the label updated with the
text "Hello from JavaServer" except this time the data displayed is coming from
a different machine!
You can see from this example that Visual J++ 6.0 makes developing, debugging and
deploying Microsoft Transaction Server based Java solutions very easy. This sample did not
need to use a transaction since it only manipulates very simple data. It's not doing any
work on shared, durable backend data. It's not calling any Resource Managers. But the
sample serves as a good starting point.
As a next step you could add code to manipulate a database such as a Microsoft SQL
Server database using the Microsoft® ActiveX® Data Objects (ADO) support in WFC. For more
information on using ADO from Java see the MTS samples in the Visual J++ 6.0 samples.
Microsoft® SQL Server provides a Resource Manager and Microsoft Transaction Server takes
full advantage of this when the database is accessed from your Java component through ADO,
providing a truly robust transaction based server-side Java solution.
|
|
|
|
© 1999
Microsoft Corporation. All rights reserved. Terms of Use. |