Visual Basic Accessing EJBs Hosted in JBoss Application Server |
This article shows you how you can allow COM clients such as Visual BASIC to access Enterprise JavaBeans (EJBs) hosted in the JBoss Application Server using J-Integra®.
The example used in this article is based on the standard Interest EJB example that can be found in the JBoss documentation. This example demonstrates a stateless 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®.
Install the J-Integra® license.
JBoss Application Server
-
Download JBoss and refer to its
on-line manual
- Apache Ant
JBoss' documentation-example.zip which contains the source code of standard Interest example and can be downloaded from http://www.jboss.ru/jbosss.org/DocExamples/
Microsoft Visual Basic 6.0
You can run the JBoss on any 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.
Before trying to get the VB client working you MUST get
the standard Interest example from JBoss working first. Note that the Interest example does not use any databases
or engines, so no additional installations are necessary. Once you have
everything set up, run the standard Interest example. You should see a
message: "Someone called 'calculateCompoundInterest!'" on the
JBoss console.
If you do not see this message, please refer to the JBoss
website
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 JBoss.
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.Properties; 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 JbossBridge { public JbossBridge() { System.out.println("JbossBridge created."); } public static void main(String args[]) { try { System.out.println("Performing JVM registration..."); Log.logImmediately(3, "jintegra.log"); Jvm.register("JbossJvm", new EjbInstanciator()); System.out.println("JbossJvm registered successfully."); while (true) Thread.sleep(100000);//Prevents the JVM from exiting. } catch (Exception ex) { ex.printStackTrace(); } finally { System.out.println("JbossBridge 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 { Properties env = new Properties(); //These properties define the initial context. They are taken from the Interest Example. Please see //JBoss documentation for detailed explanation. env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); env.setProperty("java.naming.provider.url", "jnp://localhost:1099"); env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); 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:
Update the CLASSPATH to include the necessary JBoss classes, and the Interest EJB:
set CLASSPATH=%CLASSPATH%;c:\Jboss\jboss\client\ejb.jar; set CLASSPATH=%CLASSPATH%;c:\Jboss\jboss\client\jaas.jar; set CLASSPATH=%CLASSAPTH%c:\Jboss\jboss\client\jbosssx-client.jar set CLASSPATH=%CLASSPATH%;c:\Jboss\jboss\client\jboss-client.jar; set CLASSPATH=%CLASSPATH%;c:\Jboss\jboss\client\jnp-client.jar set CLASSPATH=%CLASSPATH%c:\Jboss\jboss\deploy\interest.jar
Create a new directory and in it a file called "JbossBridge.java". Copy and paste the content from the above box into the file and compile it.
Launch the JBoss Application Server.
Launch the bridging JVM as follows:
java -DJINTEGRA_DCOM_PORT=9999 JbossBridge
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
jintegra/bin directory. Click on the New JVM...
button, and enter "JbossJvm" as the 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, the hostname is "localhost".
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.
Double-click Calculate Compound Interest and enter the code in the box below into the Private Sub Command1_Click()
units = Text1.Text intr = Text2.Text periods = Text3.Text Set home = GetObject("JbossJvm:interest/Interest") 'Note: "JbossJvm" is the name of the Bridging JVM, which is treated like any other 'COM server. 'interest/Interest is the name of the EJB we are accessing. It is treated like a regular 'COM object. The object is called "interest/Interest", because this name was 'specified in jboss.xml deployment descriptor in the interest.jar file. Please refer to 'the JBoss Interest EJB example for detailed explanation of JBoss naming. Set interest = home.Create MsgBox "Interest on " & units & " units, at " & (intr * 100) & _ "% per period, compounded over " & periods & " periods is: " _ & interest.calculateCompoundInterest(units, intr, periods) units = 0 intr = 0 years = 0 |
Double-click on the client to run it; enter the values into the fields and
click Calculate Compound Interest.
You will receive a message with the amount of compound interest.
|
|