Previous Next vertical dots separating previous/next from contents/index/pdf

Defining a Session Bean

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:

Creating a Session Bean

The WebLogic Session Bean wizard makes it easy to start creating an session bean from scratch. If you are designing a new session bean in a WebLogic EJB project, using the wizard will create basic session bean code from a template. To use the wizard, in Workshop for WebLogic right-click your WebLogic EJB project folder, point to New, then click WebLogic Session Bean.

For step-by-step information on creating new beans, see Tutorial: Building Enterprise JavaBeans.

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.

Defining a Basic Session Bean

The following 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.

package mypackage;

import javax.ejb.*;
import weblogic.ejb.*;
import weblogic.ejbgen.*;

@Session(ejbName = "PriceChecker")
@JndiName(local = "ejb.PriceCheckerLocalHome")
@FileGeneration(remoteClass = Constants.Bool.FALSE, 
        remoteHome = Constants.Bool.FALSE, 
        localClass = Constants.Bool.TRUE, 
        localClassName = "PriceCheckerLocal", 
        localHome = Constants.Bool.TRUE, 
        localHomeName = "PriceCheckerLocalHome")
@EjbLocalRefs( { 
    @EjbLocalRef(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);
        }
    }

    @LocalMethod()
    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 @Session annotation contains the actual name of the session bean. For stateful session beans this annotation will contain the attribute type = Session.SessionType.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 @EjbLocalRef 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 Workshop for WebLogic, all the information needed to make a session bean is 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 @FileGeneration annotation specifies the names of the local home and business interface for the PriceChecker bean, and the @LocalMethod annotation on the component method specifies that the method should be defined in the local business interface.

You can view the generated JAVA files in Resource view. In that view, expand the .apt_src folder to view folders corresponding to your source packages. You'll find the generated files in these folders. You can view the CLASS files compiled from the generated files (again, in Resource view) by expanding the project's build folder.

Predefined and Callback Methods

The interfaces of session (and entity) beans extend a particular interface which contains various useful methods. These interfaces include:

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 Life Cycle of a Session Bean.

Related Topics

Life Cycle of a Session Bean

@FileGeneration Annotation

@LocalMethod Annotation

@Session Annotation

 

 

Skip navigation bar   Back to Top