|
|
The client of our EJB can be many things - a Swing application, a servlet
or JSP, a unit test, or even an XML or SOAP generator.
-
Either way, it's based on RMI-IIOP, so it should look familiar.
-
We use a naming service, just like we did in our RMI example. This
keeps the actual location transparent.
-
First, we look up a home object, HelloHome or HelloLocalHome.
We use a unique name such as HelloWhirled to identify our desired home
object.
-
Next, we prompt the home object to create the EJB object. home.create(),
for instance.
-
Then we can call the methods on the EJB Object; sayHello in our
case.
-
Some things we need to do in our client (see snippet below)...
-
Get the JNDI properties, set them into a context.
-
These properties must include the name of the initial context factory and
the provider URL. They will generally be read from a file.
-
Get an object representing the home interface from JNDI.
-
Call the narrow method on java.rmi.PortableRemoteObject
to ensure that the object returned from JNDI can be cast to our expected
home interface.
-
Call the create method on that home interface.
-
Call the business methods on the returned EJB.
-
Snippet:
Properties properties = System.getProperties();
Context context = new InitialContext(properties);
Object object = ctx.lookup("HelloWhirled");
HelloHome home = (HelloHome) javax.rmi.PortableRemoteObject.narrow(object,
HelloHome.class);
Hello hello = home.create();
System.out.println(hello.sayHello());
hello.remove();
A look at our client
|