If you have sucessfully completed Steps 1 through 6, the DemoBean should
now be deployed into the Enterprise JavaBeans container and ready to accept
a client call to any of the methods specified in the remote interface.
- Review the client code below.
- Save the code to the indicated file.
Overview of Writing the Client
Writing the client is another place in the process where you get to write
some code, apart from the Enterprise JavaBeans bean business logic itself.
The client to an Enterprise JavaBeans bean can be a variety of things:
for example, a servlet, an applet, or perhaps a C/C++ program. The example
DemoClient.java
below is the client to the DemoBean
Enterprise JavaBean, created in the previous steps. The important things
to note about this program are:
- Establish the JNDI initial Context.
- Locate the Home interface of the Enterprise
JavaBean using JNDI.
- Use the Home interface instruct the Container
to create an instance of the Enterprise JavaBean.
- The use of the remote interface to instruct the
container to execute the methods of the Enterprise
JavaBeans bean.
Another thing to note is that as you deploy the bean in different
containers/servers, there will need to be differing versions of the client
code. The differences in client code are not expected to be major, but there
might be issues, such as the correct strings for getting the initial connection.
For example, consider the following code, which sets up the Properties object
for BEA WebLogic to retrieve the JNDI initialContext
object. It may
differ from the Oracle Properties string to get the initialContext
object.
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.T3InitialContextFactory");
There are a variety of other small issues that might require some
tuning and recompilation of the client code, but these are not expected
to require huge amounts of work.
The sample Enterprise JavaBeans client below demontrates how to locate
an Enterprise JavaBean and to invoke its remote methods.
DemoClient.java
(source)
/**
* DemoClient -- demonstrates using a minimal
* Java application to talk to the DemoBean
* stateless session bean
*/
package ejb.demo;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
/**
* DemoClient demonstrates using a minimal stateless
* session bean.
* Remember view session beans as an extension of your
* client running in the server.
*/
public class DemoClient {
public static void main(String[] args) {
System.out.println("\nBegin DemoClient...\n");
parseArgs(args);
try {
// Create A DemoBean object, in the server
// Note: the name of the class corresponds to the
// JNDI property declared in the
// DeploymentDescriptor
// From DeploymentDescriptor ...
// beanHomeName demo.DemoHome
Context ctx = getInitialContext();
DemoHome dhome = (DemoHome)
ctx.lookup("demo.DemoHome");
// Now you have a reference to the DemoHome object
// factory use it to ask the container to creat an
// instance of the Demo bean
System.out.println("Creating Demo\n");
Demo demo = dhome.create();
// Here is the call that executes the method on the
// server side object
System.out.println("The result is "
+ demo.demoSelect());
}
catch (Exception e) {
System.out.println(" => Error <=");
e.printStackTrace();
}
System.out.println("\nEnd DemoClient...\n");
}
static void parseArgs(String args[]) {
if ((args == null) || (args.length == 0))
return;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-url"))
url = args[++i];
else if (args[i].equals("-user"))
user = args[++i];
else if (args[i].equals("-password"))
password = args[++i];
}
}
static String user = null;
static String password = null;
static String url = "t3://localhost:7001";
/**
* Gets an initial context.
*
* @return Context
* @exception java.lang.Exception if there is
* an error in getting a Context
*/
static public Context getInitialContext()
throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL, url);
if (user != null) {
System.out.println ("user: " + user);
p.put(Context.SECURITY_PRINCIPAL, user);
if (password == null)
password = "";
p.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(p);
}
}
<< BACK
NEXT >>