Visual Basic Accessing EJBs Hosted in WebSphere Application Server |
This example shows you how to allow COM clients, such as Visual Basic clients, to access Enterprise JavaBeans (EJBs) hosted in the IBM WebSphere Application Server, using J-Integra®.
To illustrate how easy it is to access EJBs hosted in IBM's WebSphere Application Server from COM clients using J-Integra®, we use an example. The example used in this article is based on the standard Account EJB sample that comes with the default installation of IBM's WebSphere Application Server Advanced Edition. In this example, IBM demonstrates a persistent entity bean that models banking accounts. Here, we'll use the same example but instead of using a Java servlet as the client, we use a Visual Basic client.
Download and install the IBM WebSphere 4.0 sample server. NOTE: You MUST use the IBM JDK that comes with WebSphere. This is due to the fact that WebSphere 4.0 uses a built in IBM Corba classes that do NOT interoperate with the SUN Corba classes.
Download and install J-Integra®.
Before trying to get the VB client working you MUST get the standard Account example from IBM working first. Note that the Account sample uses a DB2 database to store persistent data so you'll need to make sure that DB2 is installed and set up correctly. Once you have everything set up, try running the standard Account example. Enter 1020 for the account number, click Savings, and enter a starting balance of 2000. Click Create and a message at the bottom of the page should tell you that account 1020 was created. If not, please refer to IBM's documentation for information on how to set things up.
The example is divided into two parts:
The first part describes the steps to be performed on the server machine on which WebSphere and J-Integra® are installed. This may be on any platform supporting WebSphere.
In order for the VB client to talk to the EJB, a bridging JVM will run, which the VB client talks to. It in turn talks to the EJB. Setup and run this bridging JVM on the same machine as the WebSphere Application Server.
This is the code associated with the bridging JVM:
import javax.naming.*; import java.util.Hashtable; import com.linar.jintegra.*; public class ComToWebSphere { public ComToWebSphere() { } public void registerJVMandSleep() { try { // For COM access to objects loaded via JNDI lookup Jvm.register("wsejb", new EjbInstanciator()); while (true) { Thread.sleep(5000); } } catch (Exception eX) { System.err.println("Exception: " + eX.getMessage()); } } class EjbInstanciator implements Instanciator { Context ctx; EjbInstanciator() throws NamingException { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, com.ibm.websphere.naming.WsnInitialContextFactory ); env.put(Context.PROVIDER_URL, "iiop://localhost:900"); ctx = new InitialContext(env); } public Object instanciate(String javaClass) throws AutomationException { try { System.err.println("Class requested is..." + javaClass); if (findSlash(javaClass)) { Object obj = ctx.lookup(javaClass); return obj; } return Class.forName(javaClass).newInstance(); } catch (Throwable t) { t.printStackTrace(); throw new AutomationException(new Exception("Unexpected: " + t)); } } } protected boolean findSlash(String s) { char[] characters = s.toCharArray(); for (int i = 0; i < characters.length; i++) { if (characters[i] == '/') { return true; } } return false; } public static void main(String[] args) { com.linar.jintegra.Log.logImmediately(3, "jintegra.log"); ComToWebSphere bridge = new ComToWebSphere(); bridge.registerJVMandSleep(); } } |
If you are using WebSphere 5.1, use this code instead. Thanks to Joe Fraser for his input.
To setup and run the bridging JVM, follow these steps:
Update your CLASSPATH environment variable to include the J-Integra® runtime (jintegra.jar) from the J-Integra® kit. Create a new directory and in it a file called "COMtoWS.java". Copy and paste the content from the above box into the file and compile it.
This is the code associated with runjvm.bat:
@rem +--------------------------+ @rem +-------------------------------------------------------------+ SET COMPUTERNAME=JINTEGRASERVER1 @rem +----------------------+ set WAS_CP=%WAS_HOME%\lib\websphere.jar;%WAS_HOME%\lib\ @rem +----------------------------------------------------------------------+ set PATH=%JAVA_HOME%\bin;%PATH% @rem +--------------------------------------------------------------------------------+ set JARS_NEEDED=%WAS_HOME%\installedApps\Samples.ear\AccountAndTransferEJBean.jar @rem +-------------------------------+ java -DJINTEGRA_DCOM_PORT=7051 COMtoWS endlocal |
On the Windows client machine, you will register a JVM name which, which can be used to instantiate Java objects in the JVM from COM clients. Run the c:jintegra\bin\regjvm.exe command, which is installed as part of the J-Integra® kit. Click on New JVM, and enter wsjvm as the name. In the Hostname box, enter the TCP/IP name of the machine running the bridging JVM. In the Port box, enter 7051
You can now use this JVM name to instantiate Java objects as COM objects, and access them as COM objects.
Start up Visual BASIC, and create a new Standard EXE project. Create a form that looks like the figure below and use the specified names for the individual controls:
Account Number textbox: AccountNum.
Savings radio button: Savings.
Checking radio button: Checking.
Starting Balance textbox: StartBalance.
Double-click Create, and enter the following code:
Private Sub Command1_Click() ' The Account is accessed via JNDI On Error Resume Next If Form1.Savings.Value = True Then If account Is Nothing Then End Sub |
When you run this client, enter an account number, select an account type, and enter a starting balance before clicking Create. The VB client will then talk to the EJB and return the relevant messages:
Create a new account that doesn't exist yet. Click Create.
You receive a message saying the bank account doesn't yet exist. Click OK.
Next you receive a message indicating that the new account is being created.
If you try to create an account that already exists, this is the message you get.