2.6.4 Searching from the client

This subsection describes how to search the Enterprise Beans or J2EE resources that are assigned optional names, from the client.

Organization of this subsection
(1) Relation between the settings for the client source and the search destination object
(2) Searching the Enterprise Beans
(3) Searching the J2EE resources

(1) Relation between the settings for the client source and the search destination object

This subsection describes the specification method in the client source and the settings in the search destination object when an Enterprise Bean or J2EE resource that is assigned an optional name is searched.

In the client source, you specify the optional name of the search destination object as the name to be looked up. In the search destination object, you use the annotation or property file to set up the corresponding optional name.

The following figure shows the relation between the coding of the source at the search source and the settings in the search destination object.

Figure 2-8 Relation between the settings for the client source and the search destination object

[Figure]

(2) Searching the Enterprise Beans

The following is an example of coding in the client when an optional name is used to search an Enterprise Bean. In this example, an EJB home object specifying an optional name "MyCart" is searched.

...
javax.naming.Context ctx = new javax.naming.InitialContext();
Object obj = ctx.lookup("MyCart");
SampleHome home =
(SampleHome)javax.rmi.PortableRemoteObject.narrow(obj, SampleHome.class);
Sample mybean = home.create(); // Generating a remote object
String name = mybean.ping(); //Executing the business method
...

The operations after acquiring the EJB home objects are similar to the operations for the EJB home objects acquired without using the optional names.

The following figure shows the procedure of lookup and acquisition of objects when you look up the EJB home object references.

Figure 2-9 Procedure of lookup and acquisition of objects for the EJB home object references

[Figure]

(3) Searching the J2EE resources

The following is an example of coding in the client when an optional name is used to search a J2EE resource. In this example, a J2EE resource specifying an optional name "jdbc/DBOpt" is searched.

Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup("jdbc/DBOpt");

Note that the same operations are executed after acquiring the J2EE resource objects that are executed for the objects acquired without using the optional names.

Note
You cannot search the J2EE resources from the EJB client applications. If you perform such a search, the exception javax.naming.NameNotFoundException occurs.