uCosminexus Application Server, EJB Container Functionality Guide

[Contents][Glossary][Index][Back][Next]

3.4.2 Implementation for invoking an Enterprise Bean

To invoke an Enterprise Bean from the EJB client application, use JNDI. This subsection describes how to invoke an Enterprise Bean when the references of the EJB home object are look up and also when the references of the business interface are look up.

Organization of this subsection
(1) Invoking an Enterprise Bean by searching the references of the EJB home object
(2) Invoking an Enterprise Bean by searching the references of the business interface

(1) Invoking an Enterprise Bean by searching the references of the EJB home object

The method of invoking an Enterprise Bean using look up the references of the EJB home object is described as follows, based on an example of implementation:

(a) Generating the JNDI naming context

Generate the JNDI naming context to be used for the look up of the references of the EJB home object.

javax.naming.Context ctx = new javax.naming.InitialContext();
(b) Searching and obtaining the references of the EJB home object

Use the generated JNDI naming context to obtain the references of the EJB home object. To obtain the references of the EJB home object, perform lookup with either the automatically bound name (Portable Global JNDI name or a name starting with HITACHI_EJB) or the name provided by using the user-specified name space functionality. In the following example, lookup is performed using the user-specified name space and the references are obtained. For details on how to perform lookup, see 2.3 Binding to and looking up an object in the JNDI namespace in the uCosminexus Application Server Common Container Functionality Guide.

String ejbName = "MySample";
java.lang.Object obj = ctx.lookup(ejbName);
SampleHome sampleHome =
(SampleHome)javax.rmi.PortableRemoteObject.narrow(obj, SampleHome.class);
(c) Generating the Enterprise Beans and invoking the methods

Generate the instances of the Enterprise Beans using the create method of the EJB home object. By doing this, the methods of the Enterprise Beans required in the application can be invoked.

Sample remoteSample = sampleHome.create();
//Generate Enterprise Bean instances
String result = remoteSample.getData("data");
//Invoke the business methods

In the Entity Bean, when you use the find method that returns the collection type, the objects obtained from the collection must be narrowed in the Enterprise Bean class.

Collection c = home.findByXXX(keyValue);
Iterator i=c.iterator();
while (i.hasNext()) {
  Sample remoteSample=(Sample)javax.rmi.PortableRemoteObject.narrow(i.next(), Sample.class);
                //Invoke a business method in RemoteSample.
}

(2) Invoking an Enterprise Bean by searching the references of the business interface

The method of invoking an Enterprise Bean using look up of the references of the business interface is described as follows as per the implementation example:

(a) Generating the InitialContext

To invoke an Enterprise Bean using the business interface, first of all generate the InitialContext.

// Generate the InitialContext
InitialContext ctx = new InitialContext();
(b) Searching and obtaining the references of the business interface

Use the generated InitialContext to obtain the references of the business interface. To obtain the references of the business interface, perform lookup with either the automatically bound name or the name provided by using the user-specified name space functionality. For details on how to look up a business interface using an automatically bound name, see 2.5 Lookup by a name starting with HITACHI_EJB in the uCosminexus Application Server Common Container Functionality Guide.

// Obtain the reference of the business interface
Sample sample = (Sample)ctx.lookup("HITACHI_EJB/SERVERS/MyServer/EJBBI/SampleApp/Sample");
(c) Calling a method

When the references of the business interface are obtained, the business method can be invoked.

// Invoke the business method
String result = sample.getData("data");