![]() |
![]() ![]() |
![]() |
![]() |
|
![]() |
The session bean that you will create is called GreeterDB. It generates a greeting message based on the current time upon request.
|
![]() |
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.
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.
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.
|
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.
|
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.
![]() |
![]() |
![]() |