Visual Basic Accessing EJBs Hosted in BEA WebLogic 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 BEA WebLogic Platform, 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.

The standard example has a Java client which accesses a bank account, and adds/removes money, etc. Below we show a VB client which does pretty much the same thing. The EJB is unchanged, and has no knowledge that it is being accessed as a COM object.

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. WebLogic Server 8.1 (6.0 Service Pack 1 or greater) Developer Edition (a free trial version can be downloaded from the BEA website).

  4. Microsoft Visual Basic 6.

The example is based on the Bean Managed Enterprise Java Bean (EJB) example which is part of the WebLogic installation. Before proceeding, you must have the EJBeanManagedClient.jsp example working using the standard Java client which is part of the example. To do so, click Start Menu > All Programs > BEA WebLogic Platform 8.1 > Examples > WebLogic Server Examples > Launch WebLogic Server Examples. The screen below should then appear:

Scroll it down and click the button Launch Examples (next to the link Bean Managed under the section Enterprise Java Bean.

Then you should be able to see the webpage below:

The Steps Involved

The VB example is divided into two parts:

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

  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. On the WebLogic machine, make sure jintegra.jar and weblogic.jar are in your CLASSPATH, then create a file named COMtoWebLogic.java in the directory C:\bea\weblogic81\samples\domains\examples by cutting-and-pasting the following implementation from your browser. If you are not running WebLogic using the default configuration then you will need to modify the initial context:

    // This is the bridge.  The VB client talks to this process via DCOM,
    // and this process talks to WebLogic via RMI. 
    // It all happens behind the scenes in the J-Integra® runtime.

    import javax.naming.*;
    import java.util.Hashtable;
    import com.linar.jintegra.*;

    public class COMtoWebLogic {
      public COMtoWebLogic() {
      }
      public void registerJVMandSleep() {
        try {
          Jvm.register("ejb", new EjbInstanciator()); // For COM access to objects loaded via JNDI lookup
          while (true) {
            Thread.sleep(5000);
          }
        } catch (Exception eX) {
          eX.printStackTrace();
        }
      }
      public static void main(String[] args) throws Exception  {
        COMtoWebLogic bridge = new COMtoWebLogic();
        System.out.println("COMtoWebLogic started.");
        bridge.registerJVMandSleep();
      }
     
      class EjbInstanciator implements Instanciator {
        Context ctx;
        EjbInstanciator() throws NamingException {
          Hashtable env = new Hashtable(11);
          env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          env.put(Context.PROVIDER_URL, "t3://localhost:7001");
          ctx = new InitialContext(env);
        }
        public Object instanciate(String javaClass) throws AutomationException {
          try {
            // try {
            //   return Class.forName(javaClass).newInstance();
            // } catch(Exception e) {
            //   e.printStackTrace();
            // }
            return ctx.lookup(javaClass);
          } catch (Throwable t) {
            t.printStackTrace();
            throw new AutomationException(new Exception("Unexpected: " + t));
          }
        }
      }
    }

  2. Make sure your CLASSPATH includes the necessary WebLogic classes, and the runtime of J-Integra®. You can use the following batch file called C:\bea\weblogic81\samples\domains\examples\setExamplesEnv.cmd. The lines in red are added to the original setExamplesEnv.cmd file.

    Note If  WebLogic is installed on drive c:, then WL_HOME would be C:\bea\weblogic81

    @rem *************************************************************************
    @rem This script is used to set up your environment for compiling and running
    @rem the code examples included with WebLogic Server.
    @rem *************************************************************************

    @echo on
    set WL_HOME=C:\bea\weblogic81
    @rem Set Production Mode. When this is set to true, the server starts up in
    @rem production mode. When set to false, the server starts up in development
    @rem mode. If it is not set, it will default to false.
    set PRODUCTION_MODE=

    @rem Set JAVA_VENDOR to java virtual machine you want to run on server side.
    set JAVA_VENDOR=Sun

    @rem Set JAVA_HOME to java virtual machine you want to run on server side.
    set JAVA_HOME=C:\bea\jdk142_04
    set JINTEGRA_HOME=C:\Program Files\J-Integra\com

    call "%WL_HOME%\common\bin\commEnv.cmd"

    set SAMPLES_HOME=C:\bea\weblogic81\samples
    set EXAMPLES_CONFIG=%SAMPLES_HOME%\domains\examples
    set EXAMPLES_HOME=%SAMPLES_HOME%\server\examples
    set EXAMPLES_BUILD=%EXAMPLES_HOME%\build

    set APPLICATIONS=%EXAMPLES_CONFIG%\applications
    set CLIENT_CLASSES=%EXAMPLES_BUILD%\clientclasses
    set SERVER_CLASSES=%EXAMPLES_BUILD%\serverclasses
    set COMMON_CLASSES=%EXAMPLES_BUILD%\common
    set EX_WEBAPP_CLASSES=%EXAMPLES_BUILD%\examplesWebApp\WEB-INF\classes

    set CLASSPATH=.;%JINTEGRA_HOME%\lib\jintegra.jar;%WL_HOME%\server\lib\weblogic.jar;%WL_HOME%\server\lib\webservices.jar;%WL_HOME%\server\lib\ejbgen.jar;%CLIENT_CLASSES%;%SERVER_CLASSES%;%POINTBASE_CLASSPATH%;%POINTBASE_TOOLS%;%COMMON_CLASSES%;%CLIENT_CLASSES%\utils_common.jar;%WEBLOGIC_CLASSPATH%


    set PATH=%PATH%;%POINTBASE_HOME%\tools
    กก


  3. Compile COMtoWebLogic.java, and then run it, setting the JINTEGRA_DCOM_PORT property to 7050

    > setExamplesEnv
    > javac COMtoWebLogic.java
    > java -DJINTEGRA_DCOM_PORT=7050 COMtoWebLogic

The Client Machine

  1. On your Windows VB client machine, tell J-Integra® about the location of the JVM, and the name that can be used to access Java objects through it. You will need to know the TCP/IP host name of the machine running the bridge you just started. If it is the same machine as the VB client, then use localhost, otherwise use the appropriate host name:

    > C:\jintegra\bin\regjvmcmd.exe ejb localhost[7050]

    Refer to the documentation of regjvm tool for more information.

  2. Start up Visual BASIC, creating a standard project. Test your GUI design skills to the limit by creating a form that looks something like this:

  3. Double-click the button Access EJB from VB, and enter the following code for the "Click" event:

    Dim accountId As String
    accountId = 10020
    Dim home As Object
    Dim balance As Integer
    balance = 3000
    Dim account As Object

    ' The accountHome is accessed via JNDI
    ' You may need to change "ejb20-beanManaged-AccountHome" if your WebLogic
    ' server is older than 8.1
    Set home = GetObject("ejb:ejb20-beanManaged-AccountHome")
    On Error Resume Next
    MsgBox "Looking up account " & accountId & " ..."
    Set account = home.findByPrimaryKey(accountId)
    If Err.Number <> 0 Then
      MsgBox "Did not find " + accountId & " (" & Err.Description & ")"
    End If
    On Error GoTo 0 ' clear error trap
    If account Is Nothing Then
      MsgBox "Account " + accountId + " being created; opening balance is $" & balance
      Set account = home.Create(accountId, balance)
    Else
      balance = account.balance
      MsgBox "Account " & accountId & " found; balance is $" & account.balance
    End If

    The above VB code is based on C:\bea\weblogic81\samples\server\examples\src\examples\jsp\EJBeanManagedClient.jsp. You can compile them to see how the Java code is mapped to VB code.

  4. Run the example:

    1. If you get Automation Error No Object for moniker, close your VB project, make sure that COMtoWebLogic.java is running on port 7050, and then open the VB project and try again.