This topic discusses how to create a session bean, what a session bean minimally must contain, and provides an overview of the various interfaces extended by a session bean. This topic contains the following sections:
To create a session bean, you can choose one of the following methods:
Note. If you have existing session beans that you plan to invoke in the application, for instance via another EJB or an EJB control, but you do not intend to change their definitions, you can suffice by adding the EJB Jar to the application. For more information, see How Do I: Add an Existing Enterprise JavaBean to an Application?
The following figure shows the design view of a basic session bean called PriceCheckerBean:
This stateless session bean's component method receives the name of a product and returns the price when known, or a 'product unknown' message if the product cannot be found. It uses the ProductBean, shown in Defining an Entity Bean, to look up a product in the database and return its price. The source code of the PriceChecker bean is given below:
package myBeans; import javax.ejb.*; import javax.naming.InitialContext; import javax.naming.NamingException; import weblogic.ejb.*; /** * @ejbgen:session * ejb-name = "PriceChecker" * * @ejbgen:jndi-name local="ejb.PriceCheckerLocalHome" * * @ejbgen:file-generation remote-class="false" remote-home="false" local-class="true" * local-class-name = "PriceCheckerLocal" local-home="true" local-home-name = "PriceCheckerLocalHome" * * @ejbgen:ejb-local-ref link="Product" */ public class PriceCheckerBean extends GenericSessionBean implements SessionBean { private ProductHome productHome; public void ejbCreate() { try { javax.naming.Context ic = new InitialContext(); productHome = (ProductHome)ic.lookup("java:comp/env/ejb/Product"); } catch (NamingException ne) { throw new EJBException(ne); } } /** * @ejbgen:local-method */ public String returnPrice(String product) { Product theProduct; int visitNumber; try { theProduct = productHome.findByPrimaryKey(product); } catch(FinderException fe) { return "Product not known"; } return "The price of this product is " + theProduct.getPrice(); } }
The @ejbgen:session annotation contains the actual name of the session bean. For stateful session beans this tag will contain the attribute type="Stateful". In the ejbCreate method a reference to the Product entity bean's local home interface is obtained. The JNDI reference Product used in the lookup method to look up the Product bean, is mapped to this bean's local interface using an @ejbgen:ejb-local-ref Annotation, which is defined at the top of the PriceChecker bean class definition. To learn more about JNDI naming, consult your favorite J2EE documentation or go to http://java.sun.com.
The method returnPrice implements the business logic for this class. It finds a particular product using the Product bean and returns its price. If the product cannot be found in the database, it returns a Product not known message instead.
In WebLogic, all the information needed to make a session bean are stored in a single file, instead of separate JAVA files for the bean class, the local business interface, the local home interface, and so forth. When you build a session bean, these classes are auto-generated. Various ejbgen annotations are used to hold the information required to make this generation possible. Specifically, the @ejbgen:file-generation annotation specifies the names of the local home and business interface for the PriceChecker bean, and the @ejbgen:local-method annotation on the component method specifies that the method should be defined in the local business interface. To verify that these JAVA and corresponding CLASS files are generated, expand the JAR file created during a build (located in the Modules folder in the Application pane), and locate and open the generated files in the folder reflecting the package name. For the PriceCheckerBean, the PriceCheckerBean.java (bean definition), PriceCheckerLocal.java (local interface definition), and PriceCheckerLocalHome.java (local home interface definition) files are auto-generated.
The interfaces of session (and entity) beans extend a particular interface which contains various useful methods. Specifically:
For example, the interfaces contain a remove method to remove a bean instance and, for a stateful session bean, end the conversation. Complete details about these interfaces and the methods they define can be found in your favorite J2EE documentation and the API reference at http://java.sun.com.
Every session bean must implement the javax.ejb.SessionBean interface. This interface defines callback methods that are called by the EJB container at specific times. The callback methods are setSessionContext, ejbActivate, ejbPassivate, and ejbRemove. When you define a session bean from scratch, it will extend weblogic.ejb.GenericSessionBean, which contains empty implementations of these callback methods. In other words, you will only need to define these methods if you need to override the empty implementation. If you import a session bean, these callback methods will probably be implemented directly in the bean's ejb file. For more details about the callback methods and their role in the interaction between the session bean and the EJB container, see The Life Cycle of a Session Bean.