Visual Basic Accessing EJBs Hosted in JRun Application Server

Java/J2EE COM Interoperability Products Page

Summary

Having read this article you will have a clear understanding of how easy it is for software, written in languages supporting COM, to use J-Integra® in order to access Enterprise JavaBeans?(EJBs) hosted in the JRun Application Server, running on any platform.

Introduction

This example demonstrates how you can access an EJB from a Visual Basic client using late binding. This will require that some J-Integra® software be installed on the COM client machine (although no JVM will need to be installed).

Although you are going to use Visual Basic to access pure Java EJBs, you could in fact use any language which supports COM, such as Visual C++? I would also like to emphasize that this requires no alteration to the EJB or supporting files—the same EJB may be accessed from pure Java or Visual Basic.

Note In the example, commands are often entered on the command line. If this is the case then they are displayed using an italic font.
Note Wherever you see [JRUN_HOME] in the example, this refers to the home directory where JRun was installed.
e.g. If you installed JRun in c:\JRun then [JRUN_HOME]\samples\sample2a refers to c:\JRun\samples\sample2a.

Prerequisites

  1. Download and install a JDK (1.2.2 or higher recommended for this demo due to EJB requirements).

  2. Download and install J-Integra®.

  3. JRun Server (3.1 or greater) Developer Edition (a free trial version can be downloaded from the Macromedia web site).

  4. Microsoft Visual Basic.

  5. The example is based on the BalanceBean EJB example (sample 2a) which is part of the JRun installation. Before proceeding, you must have the BalanceBean example working using the standard Java client which is part of the example. By default, the BalanceBean example is installed in [JRUN_HOME]\samples\sample2a.

Note The instructions for deploying and running the BalanceBean example EJB can be found in Chapter 7 of the JRun Documentation (JRun Samples Guide).

The Steps Involved

The example is divided into two parts:

  1. The first part describes the steps to be performed on the server machine on which JRun and J-Integra® are installed. This may be on any platform supporting JRun.

  2. The second part describes the steps to be performed on the client machine, which must be a Windows machine and must have Visual Basic installed. You may, of course, run both the client and server on the same machine.

The Server Machine

  1. Open a command prompt window: cd [JRUN_HOME]\samples\sample2a

  2. Start the JRun EJB engine: makew standalone

  3. Create a new directory. The CLASSPATH needs to be set correctly within it in order to compile and run the Java bridge which provides the VB client with access to the EJB:

  4. Copy the following to a file called setup.bat.

    @rem +-------------------------------------------+
    @rem | YOU MUST FILL IN THE FOLLOWING VALUES!!   |
    @rem +-------------------------------------------+
    set JINTEGRA_HOME=
    set JRUN_HOME=
    
    @rem +-------------------------------------------+
    @rem | DO NOT EDIT THIS!                         |
    @rem +-------------------------------------------+
    set A=.
    set B=%JINTEGRA_HOME%\lib\jintegra.jar
    set C=%JINTEGRA_HOME%\lib
    set D=%JRUN_HOME%\servers\default\deploy\ejipt_exports.jar
    set E=%JRUN_HOME%\servers\default\deploy\sample2a_ejb.jar
    set F=%JRUN_HOME%\servers\default\deploy\ejipt_objects.jar
    set G=%JRUN_HOME%\lib\ejipt_client.jar
    set H=%JRUN_HOME%\lib\ext\ejb.jar
    set I=%JRUN_HOME%\lib\ext\jms.jar
    set J=%JRUN_HOME%\lib\ext\jndi.jar
    set K=%JRUN_HOME%\lib\ext\jta.jar
    set L=%JRUN_HOME%\lib\ext\jdbc.jar
    set M=%JRUN_HOME%\lib\ext\iioprt.jar
    
    @rem +-------------------------------------------+
    @rem | HERE'S THE CLASSPATH... DO NOT EDIT THIS! |
    @rem +-------------------------------------------+
    set CLASSPATH=%A%;%B%;%C%;%D%;%E%;%F%;%G%;%H%;%I%;%J%;%K%;%L%;%M%;

  5. Fill in your JINTEGRA_HOME and JRUN_HOME values. e.g. JINTEGRA_HOME=c:\JIntegra e.g. JRUN_HOME=c:\JRun

  6. Save setup.bat.

  7. Run setup.bat: setup.bat

  8. Create the Java bridge which provides the VB client with access to the EJB. This file, called Com2Jrun.java, should contain the following contents:

    // 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 com.linar.jintegra.*;
    import java.util.*;
    import javax.ejb.*;
    import javax.naming.*;
    import ejbeans.*;
    
    public class Com2Jrun {
      private static EjbInstanciator ejbIns = new EjbInstanciator();
    
      // main: This method registers the JVM and keeps the Com2Java
      // bridge running continuously so that COM clients can access it.
      public static void main(final String[] args) throws Exception {
        //com.linar.jintegra.Log.logImmediately(3, System.err);
        Jvm.register("myjvm", ejbIns);
        while (true) {
          Thread.sleep(100000);
        }
      }
    
      // login: This is JRun-specific and provides the login context to access
      // the Balance EJB. Please see EjbClient.java in the JRun sample2a directory
      // to see where this code comes from.
      public void login() {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                               "allaire.ejipt.ContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "ejipt://localhost:2323");
        properties.setProperty(Context.SECURITY_PRINCIPAL, "chief");
        properties.setProperty(Context.SECURITY_CREDENTIALS, "pass");
    
        try {
          ejbIns.context = new InitialContext(properties);
        } catch (NamingException e) {
          e.printStackTrace();
        }
      }
    
      // getHome: This is JRun-specific and returns the Balance EJB Home object.
      // Please see EjbClient.java in the JRun sample2a directory to see where this
      // code comes from.
      public BalanceHome getHome() {
        BalanceHome home = null;
    
        try {
          home = (BalanceHome)javax.rmi.PortableRemoteObject.narrow(
            ejbIns.context.lookup("java:comp/env/ejb/sample2a.BalanceHome"),
            BalanceHome.class);
        } catch (NamingException e) {
          e.printStackTrace();
        }
        return home;
      }
    
      // getBalance: This is JRun-specific and returns (or creates) the balance
      // associated with the Balance EJB. Please see EjbClient.java in the JRun
      // sample2a directory to see where this code comes from.
      public Balance getBalance(BalanceHome home) throws Exception {
        Balance b = null;
        int key = 123; // default key for accessing the balance in the JRun DB
    
        try {
          // return existing Balance if already exists
          b = home.findByPrimaryKey(new Integer(key));
        } catch(FinderException e) {
          // create new Balance if not there
          b = home.create(key);
        }
        return b;
      }
    }
    
    class EjbInstanciator implements Instanciator {
    
      public Context context;
    
      // instanciate: 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) {
            e.printStackTrace();
          }
          return context.lookup(javaClass);
        } catch (Throwable t) {
          t.printStackTrace();
          throw new AutomationException(new Exception("Unexpected: " + t));
        }
      }
    }

  9. Compile Com2Jrun: javac Com2Jrun.java

  10. Run the bridge: java -DJINTEGRA_MATCH_THREADS -DJINTEGRA_DCOM_PORT=9999 Com2Jrun

The Client Machine

  1. If you do not have J-Integra® installed on your client machine, copy the files jintmk.dll and regjvmcmd.exe from the bin directory of your J-Integra® installation on the server to a directory on the client.

  2. Open a DOS console on the client machine and change to the directory into which you copied the two files. Then tell J-Integra® about the location of the JVM, and the name used to access it. You will need to know the TCP/IP host name of the server machine. If it is the same machine as the client machine, then use 'localhost', otherwise use the appropriate host name. Run as follows: regjvmcmd myjvm yourservermachinename[9999]

  3. Start Visual Basic and create a new standard.exe project. Modify the initial form to look similar to the following:

    Please note:

  4. Double click on the form and replace the code in the editor with this code:

    Option Explicit
    Dim Bridge As Object
    Dim home As Object
    Dim balance As Object
    
    Private Sub Form_Load()
    Set Bridge = GetObject("myjvm:Com2Jrun")
    Bridge.login
    Set home = Bridge.getHome
    Set balance = Bridge.getBalance(home)
    vbAmount.Text = 100
    vbBalance.Text = balance.getValue
    End Sub
    
    Private Sub Save_Click()
    balance.Save (vbAmount.Text)
    vbBalance.Text = balance.getValue
    End Sub
    
    Private Sub Spend_Click()
    balance.Spend (vbAmount.Text)
    vbBalance.Text = balance.getValue
    End Sub

  5. Compile and run your project. You should see the following:

  6. Log in as the chief, with password chief, you should see a balance.

  7. Enter an amount and click either Save or Spend. The balance should update accordingly.

    Note that the balance is stored in a database within JRun and is persistent, meaning that the balance stays the same even if you close the application and re-run it again at a later date.

Troubleshooting

If you run into any problems with this example please check your JRun configuration and make sure that their standard Java client works first.