uCosminexus Application Server, EJB Container Functionality Guide
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.
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:
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(); |
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); |
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. } |
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:
To invoke an Enterprise Bean using the business interface, first of all generate the InitialContext.
// Generate the InitialContext InitialContext ctx = new InitialContext(); |
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"); |
When the references of the business interface are obtained, the business method can be invoked.
// Invoke the business method String result = sample.getData("data"); |
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.