Sun Microsystems Logo

 


 

 

Sun[tm] ONE Studio 4, Enterprise Edition for Java[tm] with Application Server 7 Tutorial
Creating a Session Bean with the EJB[tm] Builder
PREV PREV NEXT

 

The session bean that you will create is called GreeterDB. It generates a greeting message based on the current time upon request.

  1. Create the Session Bean
  2. Modify JNDI Name
  3. Modify Automatically Generated Methods
  4. Create a Business Method


1. Create the Session Bean

To create the session bean using the IDE's EJB Builder facility:

1. In the IDE, click the Explorer window's Filesystems tab.

2. Select the Filesystems node, right-click, select Mount -> Local Directory.

The New Wizard - Local Directory window is displayed.

3. Navigate to the IDE user directory that you specified during the first startup of the IDE:

<ide_user_dir>/sampledir/examples

For example:

C:\ide-userdir\sampledir\examples

The user directory is automatically created when you start the IDE for the first time. You may have specified a different base user directory than presented in this example, but the sampledir directory is created automatically.

4. Click the Create New Folder icon in the upper left hand portion of the window.

A folder named New Folder appears.

5. Click on the New Folder item and enter the name "jdbc".

6. Double click the newly named "jdbc" directory and create a new folder named "mysimple" under this directory.

7. Repeat this operation to create a directory named "src" under the mysimple directory.

8. Click once on the "src" directory and click the Finish button.

The newly created jdbc/mysimple/src directory appears as a mounted filesystem in the Explorer window.

9. Right-click the new filesystem and choose New -> Java Package.

You will use this Java package to hold the EJB source code of the application.

10. Name the new Java package:

samples.jdbc.simple.ejb

Click Finish.

A new directory named samples/jdbc/simple/ejb appears under the mysimple/src filesystem.

11. Right-click the ejb directory node and choose New -> J2EE -> Session EJB.

The Session Bean Name and Properties pane of the New Wizard is displayed.

12. Type GreeterDB in the EJB Name field and click Finish.

The default settings are used when creating the session bean and then the GreeterDB session bean is displayed in the Explorer window.

Note the presence of a new GreeterDB (EJB) node and three other nodes:

The GreeterDB (EJB) node is a logical representation of the session bean while the three other nodes represent the class files of the remote, implementation and home interfaces. The IDE creates the minimal set of required methods in the EJB implementation class file.

2. Modify JNDI Name

To avoid the possibility of naming conflicts with previously deployed versions of the JDBC Simple application, you need to modify the JNDI Name of the EJB.

1. Right-click the GreeterDB (EJB) node and select Properties.

2. Select the Sun ONE AS tab.

3. Enter ejb/my-jdbc-simple in the JNDI Name field.

4. Close the Properties window.

This JNDI name will uniquely identify this EJB throughout the application server instance to which the EJB is deployed.

3. Modify Automatically Generated Methods

Your next step is to enhance the automatically generated methods of the session bean. These modifications will result in output being written to the stdout stream when each standard method is invoked by the EJB container of the application server. Since output written to stdout is automatically redirected to the application server's event log, you will see this output in the server's log file when you test the session bean.

To update methods:

1. Double-click the GreeterDBBean node that represents the implementation class of the session bean.

The source editor shows the GreeterDBBean.java file.

2. Add the following code (the bold text only) to the body of the session bean methods to display the status of the bean instance:

/**
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() {
  System.out.println("ejbActivate() on obj " + this);
}          
...          
/**
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() {
  System.out.println("ejbPassivate() on obj " + this);
}           
...          
/**
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() {
  System.out.println("ejbRemove() on obj " + this);
}           
...          
/**
* See section 7.10.3 of the EJB 2.0 specification
*/
public void ejbCreate() {
  System.out.println("ejbCreate() on obj " + this);
} 

3. In the IDE, choose File -> Save All.

This will save the GreeterDB session bean and the changes that you have made thus far.

Why select Save All? In many cases, your work is saved automatically by the IDE. However, use File -> Save All whenever you wish to either leave the tutorial for an extended period of time or want to make sure all your work is saved. You may also use the Save and Save All buttons in the toolbar save modifications.


4. Create a Business Method

The EJB Builder automatically generated the required methods of the session bean and you have customized these methods with output statements. Now you need to add a simple business method to the bean. This method, getGreeting(), generates a greeting based on the current time.

To create the getGreeting() business method:

1. Right-click the GreeterDB (EJB) node and choose Add Business Method.

The Add New Business Method dialog box appears.

2. Type getGreeting in the Name field.

3. Choose java.lang.String in the Return Type field.

The completed dialog window should look like this:

4. Click OK.

The getGreeting() business method is added to the GreeterDB session bean and its method signature is automatically added to the GreeterDB bean's remote interface.

5. Expand the GreeterDB (EJB) node and its Business Methods node.

The getGreeting() method node is displayed under the Business Methods node.

6. Double-click the getGreeting() method node.

The source editor displays the content of the bean implementation source file and places the cursor at the start of the getGreeting() method.

7. Add the following code (the bold text only) to the body of the getGreeting() method:
public java.lang.String getGreeting() {
  System.out.println("GreeterDB EJB is determining message..."); 
  String message = null; 
  Calendar calendar = new GregorianCalendar(); 
  int currentHour = calendar.get(Calendar.HOUR_OF_DAY); 
  if(currentHour < 12) {
    message = "morning";
  } else { 
    if((currentHour >= 12) && 
       (calendar.get(Calendar.HOUR_OF_DAY) < 18)) {
      message = "afternoon";
    } else {
      message = "evening";
    }
  } 
  System.out.println("- Message determined successfully");
  return message; 
}

8. Add an import statement (the bold text only) for the java.util.* package at the top of the source file.

package samples.jdbc.simple.ejb;


import javax.ejb.*; import java.util.*;
...

8. In the source editor window, right-click and select Save to ensure that your changes have been saved.

9. In the Explorer window, right-click the GreeterDB (EJB) node and select Validate EJB.

An Output Window specifying "Finished GreeterDB (EJB)" should be displayed.

If this message is not displayed, the code segments above were not copied properly into the source file. Based on the specific error displayed as a result of the validation step, go back and double check your source code.

What happens during Validate? The IDE's EJB Builder contains a custom compiler that not only compiles the EJB source code, but also validates your enterprise beans against the Enterprise JavaBeans[tm] specification.

After the bean validates successfully, close the source code editing window and proceed to Testing the Session Bean to use the IDE's built-in EJB test facility.

PREV PREV NEXT