Visual Basic Accessing EJBs Hosted in HP Bluestone Application Server |
This article shows you how you can allow COM clients such as Visual Basic to access Enterprise JavaBeans (EJBs) hosted in HP Bluestone Total-E-Server 7.3 Application Server using the J-Integra®
The example used in this article is based on "Standalone EJB Server" tutorial that can be found in the Bluestone documentation. The tutorial demonstrates a session bean that computes compound interest. Here, we use the same EJB, but instead of using a Java client such as a standalone Java application, we use a COM client written in Visual Basic.
J-Integra® is a bi-directional pure Java-COM bridge. It incorporates a pure Java implementation of a DCOM engine (the wire protocol defined by Microsoft to allow remote access to COM objects). This means that it allows COM clients running on MS Windows to access Java objects as if they were COM objects, where Java objects are running on any Operating System running on any platform; and it allows Java clients running anywhere to access Windows based COM objects.
The following items were installed and used in this example:
Download J-Integra®
Ensure that you have you have installed the J-Integra® license.
HP Bluestone Total-E-Server Application Server version 7.3 (It can be downloaded from the Bluestone Web Site).
Microsoft Visual Basic 6.0
You can run the Bluestone Server on MS Windows or any other platform that has a Java Virtual Machine. You do not need to install a JVM on the Windows client machine, although you can run everything on the same machine if you wish. This example assumes the Bluestone Server is installed on Windows. However, the basic procedure should not be any different on other platforms.
Before trying to get the VB client working you MUST get the standard LoanCalculator example from Bluestone's "Standalone EJB Server Tutorial" working first. The tutorial can be found at Bluestone documentation, under Total E-Server | Bluestone Universal Business Server | Tutorials | Standalone EJB Server Tutorial. Note that the example does not use any databases or engines, so no additional installations are necessary. Once you have everything set up, run the standard LoanCalculator example. If something goes wrong, please refer to the Bluestone documentation for troubleshooting tips.
In order for the VB client to talk to the EJB, a "bridging JVM" is
required. The VB client communicates with the bridging JVM using DCOM. The bridging
JVM then interacts with the EJB just like any Java client. This bridging JVM
runs on the same machine as Bluestone.
This is the bridging JVM code:
//This is the bridge. //The VB client talks to this process via DCOM, and this process talks to the EJB. //It all happens behind the scenes in the J-Integra® runtime. import java.util.Hashtable; import javax.naming.*; //J-Integra imports: import com.linar.jintegra.Jvm; import com.linar.jintegra.Log; import com.linar.jintegra.AutomationException; import com.linar.jintegra.Instanciator; public class BluestoneBridge { public BluestoneBridge() { System.out.println("BluestoneBridge created."); } public static void main(String args[]) { try { System.out.println("Performing JVM registration..."); Log.logImmediately(3, "jintegra.log"); Jvm.register("BluestoneJvm", new EjbInstanciator()); System.out.println("BluestoneJvm registered successfully."); while (true) { Thread.sleep(100000);//Prevents the JVM from exiting. } } catch (Exception ex) { ex.printStackTrace(); } finally { System.out.println("BluestoneBridge destroyed. JVM exiting"); } } } //This is the code that instanciates any Java Class created by VB, including EJBs: class EjbInstanciator implements Instanciator { Context ctx; EjbInstanciator() throws NamingException { Hashtable env = new Hashtable(); //Defines the intial context. Taken directly from the Bluestone "Standalone EJB server tutorial". // Please see Bluestone documentation for details. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory"); ctx = new InitialContext(env); System.out.println("got Initial Context"); } // This method is called by the J-Integra® runtime when a COM client tries // to instantiate an object. public Object instanciate(String javaClass) throws AutomationException { try { try { return Class.forName(javaClass).newInstance(); } catch (Exception e) { } return ctx.lookup(javaClass); } catch (Throwable t) { t.printStackTrace(); throw new AutomationException(new Exception("Unexpected: " + t)); } } } |
Follow these steps to setup and run the bridging JVM:
Launch the Bluestone server as per "Standalone EJB server" tutorial instructions.
Open a new DOS console, and update your CLASSPATH environment variable to include the necessary Bluestone classes by running the "common.bat" batch file from the command line as follows:
runapp common
Note: common.bat is located in the bin directory of the Bluestone Server installation.
Update your CLASSPATH environment variable to include the J-Integra® runtime (jintegra.jar) from the J-Integra® kit. Assuming the J-Integra® home directory is c:\jintegra, do it as follows:
set CLASSPATH=%CLASSPATH%;c:\jintegra\lib\jintegra.jar
Create a new directory and in it a file called "BluestoneBridge.java". Copy and paste the content from the above box into the file and compile it.
Launch the bridging JVM as follows:
java -DJINTEGRA_DCOM_PORT=9999 BluestoneBridge
On the Windows client machine, you register the JVM with an id which can be used from COM clients to instantiate Java objects in the JVM .
Run the regjvm.exe tool which is installed as part of the J-Integra® kit. The tool is located in the jintegra/bin directory. Click on the New JVM button, and enter BluestoneJvm as the JVM id. Enter the TCP/IP name of the machine running the bridging JVM under Hostname, and 9999 as the Port:
In this example the VB client runs on the same machine as the bridging JVM. Therefore, enter localhost as the Hostname.
Now you can use this JVM id to instantiate Java objects as COM objects.
Start up Visual BASIC, and create a new Standard EXE project.
Create a simple form that looks like the figure below.
Click Calculate and enter the code in the box below into the Private Sub Command1_Click()
'Declare and Create the EJB Dim loanCalculatorHome As Object Dim loanCalculator As Object Set loanCalculatorHome = GetObject("BluestoneJvm:LoanCalculator") 'BluestoneJvm is the id of the Bridging JVM. Note that it is treated like any other 'COM server. 'LoanCalculator is the name by which the EJB can be accessed on the Bluestone Server. 'It is treated like any other COM object. Please refer to the Bluestone documentation 'for information on naming and accessing EJBs. Set loanCalculator = loanCalculatorHome.Create 'Get the user input: price = Text1.Text downPayment = Text2.Text interestRate = Text3.Text years = Text4.Text 'Invoke methods on the EJB: loanCalculator.setValue (price) loanCalculator.setDownPayment (downPayment) loanCalculator.setInterestRate (interestRate) loanCalculator.setLoanDuration (years) 'Output the results of the calcuation: Label8.Caption = loanCalculator.getLoanAmount Label9.Caption = loanCalculator.getMonthlyPayment |
Save the project and build the client application.
Double click on the client to run it; enter the values into the fields and click on the Calculate button.
The Loan Amount and Monthly Payment sums will be output.