| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This example creates a local counter using a session bean. Session beans have a similar API to stateless session beans, but have internal state. In other words, they're like servlets sessions. The EJB 2.0 spec introduced local interfaces. Local interfaces let you pass arguments by reference to your beans. This gives a small performance boost, but also adds a new use for EJB. EJBs can now be used to better structure web applications, even applications that have no need for distributed objects. For example, almost every web application needs some sort of database interface. Because EJB 2.0 provides strong support for container-managed-persistence, you can now use EJB for much of the database work without dealing with distributed objects or worrying about a performance.
The only exposed method in the local method is the method, used for the next count. Notice that it does not throw RemoteException, unlike a remote interface and that it extends EJBLocalObject instead of EJBObject.
The Home interface's sole responsibility is to create a new counter instance. For session beans, only methods belong in the home.
Developers will generally create a com.foo.AbstractSessionBean class which provides default implementations for the SessionBean methods. This example uses Resin's abstract implementation of in com.caucho.ejb that provides stubs for the SessionBean methods.The session bean needs to create exactly one method with no arguments to match the in SessionLocalHome. It must also define the business methods.
In general, EJB server configuration belongs in an file. In a jar, it belongs in .This name, of course, only makes sense if the EJB is put into a jar. Jars are inconvenient during development, so Resin-EJB lets you rename the ejb-jar.xml file as *.ejb and put it in WEB-INF. We'll name it .Local interfaces use and , corresponding to the familiar and . It's possible for a bean to have both local and remote interfaces.The ejb-jar is a deployment descriptor for the bean. It's part of the EJB spec. The ejb-jar for the stateless session example looks like the following:
Configuration for local beans is exactly the same as for remote beans. Since the ejb-jar is server-independent, it's still necessary to instantiate the bean in a server-dependent way. Since Resin-EJB is JNDI resource-based configuring the bean involves configuring a resource-ref.
The EJBServer automatically searches for all *.ejb files in WEB-INF. So it will find our hello.ejb deploy the session servlet. The parameter tells the EJB servlet its own URL. During initialization, servlets don't know their own URL, so we need to tell it.Finally, the servlet is a servlet so it will be initialized on server startup.
The JNDI configuration needs to link the local-ejbs into the full JNDI namespace. We'll put the EJB's at java:comp/env/local-ejb.
EJB clients follow the following steps in using a local object:
Typically, an EJB client will get the home handle using JNDI.
|