Java Reference
In-Depth Information
import javax.ejb.*;
@Stateless
@Local( { CatalogLocal.class })
public class CatalogBean implements CatalogLocal {
public String getJournal(String publisher) {
if (publisher.equals("Oracle Publisher"))
return new String("Oracle Magazine");
if (publisher.equals("OReilly"))
return new String("java.net");
}
}
In EJB 3.0, the component and home interfaces of EJB 2.x are replaced with a business
interface. The business interfaces for the session bean are POJIs, and do not extend
the
EJBLocalObject
or the
EJBObject
. A local business interface is denoted with the
annotation
@Local
. A remote business interface is denoted with the annotation
@Remote
. A remote business interface does not throw the
RemoteException
. The
local business interface corresponding to the session bean class is listed next:
import javax.ejb.*;
@Local
public interface CatalogLocal {
public String getJournal(String publisher);
}
A client for an EJB 2.x session bean gets a reference to the session bean with JNDI.
The JNDI name for the
CatalogBean
session bean is
CatalogLocalHome
. The local/
remote object is obtained with the
create()
method. The client class for the EJB 2.x
session bean is listed.
import javax.naming.InitialContext;
public class CatalogBeanClient {
public static void main(String[] argv) {
try {
InitialContext ctx = new InitialContext();
Object objref = ctx.lookup("CatalogLocalHome");
CatalogLocalHome catalogLocalHome = (CatalogLocalHome) objref;
CatalogLocal catalogLocal = (CatalogLocal) catalogLocalHome
.create();
String publisher = "OReilly";
String journal = catalogLocal.getJournal(publisher);
System.out.println("Journal for Publisher: " + publisher + " "
+
journal);
Search WWH ::
Custom Search