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

Step 3: Create the VisitTrackerBean Session Bean

In the preceding step you created an entity bean that models a visit. That bean represents a row of visit data in the database. In this step, you'll create the session bean (another kind of EJB) that models visit tracking.

As described in the WebLogic Server topic, How Do Applications Use EJBs?, a session bean implements business logic and acts on behalf of the client. In the case of the application you're building with this tutorial, the client will be a test web page (and, by extension, you, the page's user). In order to meet its client's requests, the session bean you're about to create will know how to find a visitor by name, know how to increment the visit number, and so on.

In this section, you will create the VisitTrackerBean source files.

To Create VisitTrackerBean Source Files

  1. In the Package Explorer, expand VisitEJBProject> src, right-click hello, then click New > WebLogic Session Bean.
  2. In the New Session Bean dialog, in the File name box, enter VisitTrackerBean, then click Finish.

    As with the VisitBean entity bean, this new session bean comes with a few annotations already entered for you. These are:

    • @Session— Defines the class-scope properties of a session bean.

      Attribute Description
      ejbName Descriptive name of the session bean.
    • @JndiName — Specifies the local and remote JNDI name of an EJB; that is, the JNDI name associated with its local and remote interface. The attributes shown here in code include:

      Attribute Description
      remote Remote JNDI name of the EJB.
    • @FileGeneration — Specifies the interface, compound primary key, and value classes that are to be auto-generated during build.

      Attribute Description
      localClass Whether to generate the local interface of the EJB.
      localHome Whether to generate the local home interface of the EJB.
      remoteClass Whether to generate the remote interface of the EJB.
      remoteHome Whether to generate the remote home interface of the EJB.
      valueClass Specifies whether to generate the value class for this EJB.
  3. Add the following import statements to support code you're about to add.
  4. import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.ejb.EJBException;
    import javax.ejb.FinderException;
    import javax.ejb.CreateException;
    import weblogic.ejbgen.EjbLocalRefs;
    import weblogic.ejbgen.EjbLocalRef;
  5. Above the @Session annotation, paste the following @EjbLocalRefs annotation, along with the nested @EjbLocalRef annotation.
        @EjbLocalRef(home = "hello.VisitBeanLocalHome", 
            jndiName = "ejb.VisitBeanLocalHome", 
            local = "hello.VisitBeanLocal", 
            name = "ejb/VisitBean",
            type = Constants.RefType.ENTITY)
    The following table describes the attributes you're setting.

    Attribute Description
    home Local home interface of the referenced EJB.
    jndiName Local JNDI name of the referenced EJB.
    local Local (business) interface of the referenced EJB.
    name Name used to reference the other bean.
    type EJB type of the referenced bean.
  6. Add the following visitHome field above the ejbCreate method that was added for you. Your code will use this variable later to create a VisitBean instance for a particular visitor.
  7. private VisitBeanLocalHome visitHome;
  8. Edit the ejbCreate method so that it looks like the following. This code will retrieve a VisitBean instance.
  9. public void ejbCreate()
            javax.naming.Context initialContext = new InitialContext();
            visitHome = (VisitBeanLocalHome) initialContext.lookup("java:comp/env/ejb/VisitBean");
        } catch (NamingException ne)
            throw new EJBException(ne);
  10. Beneath the ejbCreate method code, add the following greetVisitor method code.
  11. public String greetVisitor(String visitorName)
        VisitBeanLocal theVisit;
        int visitNumber;
            // Try to find the visitor in the database.
            theVisit = visitHome.findByPrimaryKey(visitorName);
        } catch (FinderException fe) 
                // If the visitor isn't in the database, 
                // create a new visitor with the name given.
                // Greet the new visitor.
                return "Hello, " + visitorName + "!";
            } catch (CreateException ce) 
                throw new EJBException(ce);
        // Get the returning visitor's visit number and 
        // increment it from this visit.
        visitNumber = theVisit.getVisitNumber();
        theVisit.setVisitNumber(visitNumber + 1);
        // Greet the returning visitor.
        if (visitNumber == 1) 
            return "Hello again, " + visitorName + "!";
        } else 
            return "Hello, " + visitorName + "! This is visit number "
                + theVisit.getVisitNumber() + ".";
  12. With a cursor in the greetVisitor method code, locate the RemoteMethod property in Annotations view.
  13. Right-click the RemoteMethod property, then click Add Annotation. The @RemoteMethod() annotation should be added immediately preceding the greetVisitor method.
  14. Press Ctrl+S to save your work.

With code for your entity and session beans, you're ready to start testing them. You'll get set up to do this in the next step.

Related Topics

Developing Session Beans

Click one of the following arrows to navigate through the tutorial:


Skip navigation bar   Back to Top