COM to EJB WebLogic[tm] example: Any JVM, any OS, any platform.

Updated for WebLogic 5.1

This example is based on the WebLogic Enterprise JavaBean container-managed persistence example, which is included in the standard WebLogic kit.

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.

The bridge and your WebLogic server can run on any JVM (including Java 2), on any platform (eg UNIX).

In order to run this example you will need to download the J-Integra®[tm] kit from http://j-integra.intrinsyc.com/

Unzip the kit on the VB Windows client (below we assume it has been unzipped to D:\jintegra). Copy the jintegra.jar file from the 'lib' subdirectory to the machine running the WebLogic server.

Follow the instructions for running the WebLogic container managed example which on my machine are in /weblogic/examples/ejb/basic/containerManaged/index.html. Make sure this standard WebLogic example runs correctly before proceeding. Examine the Client Java implementation to understand what it is that the VB client will do.

On the machine running the WebLogic server

On the WebLogic machine, make sure the 'jintegra.jar' and 'weblogic.jar' are in your CLASSPATH, then create a file "COMtoWebLogic.java" file, 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. See http://www.linar.com/jintegra/doc/
import javax.naming.*;
import java.util.Hashtable;
import com.linar.jintegra.*;

public class COMtoWebLogic {
public static void main(String[] args) throws Exception {
Jvm.register("ejb", new EjbInstanciator()); // For COM access to objects loaded via JNDI lookup
Thread.sleep(10000000);
}
}

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) {}
return ctx.lookup(javaClass);
} catch (Throwable t) {
t.printStackTrace();
throw new AutomationException(new Exception("Unexpected: " + t));
}
}
}

Make sure your CLASSPATH includes the necessary WebLogic classes, and the J-Integra® runtime: (When running under Windows, my CLASSPATH contained: d:\jintegra\lib\jintegra.jar;.;D:\weblogic\lib\weblogicaux.jar;D:\weblogic\classes).

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

> set CLASSPATH=.;C:\jintegra\lib\jintegra.jar;C:\bea\weblogic81\server\lib\weblogic.jar
> javac COMtoWebLogic.java
> java -DJINTEGRA_DCOM_PORT=7050 COMtoWebLogic

On the Windows VB client machine

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:

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

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

Double-click on the button, and enter the following code for the "Click" event:


Dim home As Object
Dim balance As Integer
balance = 3000
Dim account As Object

' The accountHome is accessed via JNDI
Set home = GetObject("ejb:containerManaged.AccountHome")

On Error Resume Next
Set account = home.findByPrimaryKey(Form1.AccountID.Text)
If Err.Number <> 0 Then
MsgBox "Could not find " + Form1.AccountID.Text & " (" & Err.Description & ")"
End If
On Error GoTo 0 ' clear error trap

If account Is Nothing Then
MsgBox "Account " + Form1.AccountID.Text + " being created; opening balance is $" & balance
Set account = home.Create(Form1.AccountID.Text, balance, "Savings")
Else
balance = account.balance
MsgBox "Account " & Form1.AccountID & " found; balance is $" & account.balance
End If

If Form1.Deposit.Value = True Then
MsgBox "Depositing $" & Form1.Amount.Text
balance = account.Deposit(Form1.Amount.Text)
Else
MsgBox "Withdrawing $" & Form1.Amount.Text
On Error Resume Next
Err.Clear
balance = account.Withdraw(Form1.Amount.Text)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
End If

MsgBox "Balance is now $" & balance

Run the example:

Add some money

1 2
345

Try to withdraw too much

12
3
4

If it doesn't work then check out the J-Integra® KB.

If you are familiar with COM then you will be pleased to know that J-Integra® also supports early bound access to COM objects.

J-Integra® also has a native mode, whereby the JVM can be loaded in-process into the COM client, so that the COM client talks RMI directly to the EJB.

Contact us with bug reports or enhancement suggestions at the J-Integra® discussion forum.