Chapter 17. EJB application support 537
Figure 17-32 ClientAsync ending
17.4.5 Scenario 5: Accessing Host Publisher EJB from user EJBs
In this scenario, we describe how to access the Host Publisher EJB Access Bean
from other user EJBs. To do this, a very simple EJB included in the Websphere
Application Server samples is used, called the HelloBean sample program.
The sample HelloBean application includes the following files:
򐂰 Hello.java
򐂰 HelloBean.java
򐂰 HelloHome.java
򐂰 HelloBeanResourceBundle.java
In this scenario, the Hello.java and HelloBean.java programs will be used. The
Hello.java file is the Hello interface and represents the EJB object instance that a
client can use to invoke remote methods. This interface is implemented by the
HelloBean. You can use the HelloHome interface to create a Hello session bean.
Step 1: Add method to access EJB Access Bean
In this sample scenario, the first step is to add a method in the sample EJB
(Hello.java) that will interface with the EJB Access Bean (see Figure 17-33 on
page 538).
Note: Source files of the sample HelloEJB java program can be found in the
WebSphere\AppServer\ sub-directory:
\hosts\default_host\WSsample_app\servlets\WebSphereSamples\HelloEJB\
538 IBM WebSphere Host Publisher Version 3.5
Figure 17-33 Hello.java interface
Step 2: Sample getClientData method (inquiries)
The sample method (getClientData) is written to instantiate and execute the EJB
Access Bean generated by the Host Access (Studio) for your Integration Object.
The method in the HelloBean.java is shown in Figure 17-34.
Figure 17-34 Sample getClientData method
package com.ibm.ejb.cb.samples.hello.tier2;
import javax.ejb.EJBObject;
import java.lang.String;
import java.rmi.RemoteException;
import IntegrationObject.*;
public interface Hello extends EJBObject {
public String getMessage() throws RemoteException;
// Invokes EJB Access Bean to access HPubEJB and obtain client data
public String[] getClientData(String Account) throws RemoteException;
}
public String[] getClientData(String Account) throws RemoteException
{
Hobj01Access0 client = new Hobj01Access0();
client.setHPubStartPoolName("hpool01");
client.setAccountVar(Account);
try
{
client.processRequest(); // execute request
}
catch(Exception e) { // add catch code here
}
String[] answer = new String[4];
if (client.getHPubErrorOccurred()==0)
{
answer[0] =client.getAccountOut();
answer[1]=client.getFirstnameOut();
answer[2]=client.getSurnameOut();
answer[3]=client.getAddressOut();
}
else
{
answer[0]=client.getHPubErrorMessage();
}
return answer;
}
Chapter 17. EJB application support 539
Step 3: Invoke the sample method getClientData
In this step, we show how the sample getClientData (Account) method can be
used to execute the Integration Object.
The sample code to do this is part of the HelloEJBServlet.java, which invokes
HelloEJB to instantiate the EJB Access Bean. In turn, the EJB Access Bean
communicates with the Host Publisher EJB via RMI/IIOP. The sample code is
shown in Figure 17-35.
Figure 17-35 HelloEJBServlet extract of doGet method
Note: To properly compile this Java servlet, you will also need to include the
statement import IntegrationObject.* at the beginning of the file.
try {
// create ejb
System.out.println( msgs.handleGetObject( "creating" ) );
Hello hello = getHome().create();
System.out.println("****creating hello");
// call methods
System.out.println( msgs.handleGetObject( "retrieving"));
out.println( hello.getMessage() );
out.println("<br>");
out.println("<br>");
//Calling HostPublisher EJB via HelloEJB
String[] ans =new String[4];
String Account=req.getParameter("accountVar");
ans=hello.getClientData(Account);
System.out.println("getClientData");
out.println("<b>Account: </b>");
out.println(ans[0]);
out.println("<br>");
out.println("<b>Firstname: </b>");
out.println(ans[1]);
out.println("<br>");
out.println("<b>Surname: </b>");
out.println(ans[2]);
out.println("<br>");
out.println("<b>Address: </b>");
out.println(ans[3]);
// destroy ejb
System.out.println( msgs.handleGetObject( "removing" ) );
hello.remove();
System.out.println( msgs.handleGetObject( "done") );
}
540 IBM WebSphere Host Publisher Version 3.5
Step 4: Class path files
In this scenario, the following files are required when you compile and run this
scenario:
򐂰 _wlm_DeployedHPubEJB.jar
򐂰 Hobj01Access0.jar
򐂰 HPubCommon.jar
In addition, they also need to be the classpath environment variable. In the
scenario, files are in the D:\EJBClasses directory, as shown in Figure 17-36.
Figure 17-36 Classpath files
Step 5: Update the classpath
In this step, you will add the required .jar files to the Application Server classpath
by using the -classpath directive in the command line arguments entry, as
shown Figure 17-37 on page 541.
For example, in this scenario the following statement can be used:
-classpath
d:\EJBClasses\_wlm_DeployedHPubEJB.jar;d:\EJBClasses\Hobj01Access0.jar;
d:\EJBClasses\HPubCommon.jar.
Chapter 17. EJB application support 541
Figure 17-37 Setting the classpath in WAS
Step 6: Update the EJB Access Bean properties file
Always remember to update the <IONameAccess.properties> file; the
PROVIDER_URL parameter must point to the server. For example, as shown in
Figure 17-38 on page 542, in this scenario the statement looks like this:
PROVIDER_URL=iiop://hostname
where hostname is the server name.

Get A Comprehensive Guide to IBM WebSphere Host Publisher Version 3.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.