EJB Development Quickstart with MyEclipse---8
天下维客,你可以修改的网络知识库
Creating a Session EJB - Part-2
In Part-1, Creating a Session EJB, we demonstrated the process for creating the TraderBean class. In this section we will add business methods as well as XDoclet annotations to the TraderBean class and then use XDoclet to update the Trader and TraderHome interfaces created in Section 7.2.
When we created the initial TraderBean.java class, the EJB Creation Wizard added an example business method that you can use as a pattern for adding your own business methods. Notice that this method includes the javadoc tag @ejb.interface-method. This custom ejbdoclet tag identifies the method as an implementation of an EJB interface method. When XDoclet is run on the this class, the Trader.java EJB interface class will be revised to include all methods with the @ejb.interface-method annotation.
TraderBean example method /**
* An example business method
*
* @ejb.interface-method view-type = "remote"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void replaceWithRealBusinessMethod() throws EJBException {
// rename and start putting your business logic here< BR > }
*
1. Begin by adding the following the following 2 methods to the TraderBean.java class and saving your changes.
TraderBean Business Methods
/**
* Buys shares of a stock for a named customer.
*
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return Number of shares purchased
*
* @ejb.interface-method
*/
public int buy(String stockSymbol, int shares) {
System.out.println("Buying "+shares+" shares of "+stockSymbol + ". Good move.");
return shares; }
/**
* Sells shares of a stock for a named customer.
*
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return The number for shares sold
*
* @ejb.interface-method
*/
public int sell(String stockSymbol, int shares) {
System.out.println("Selling "+shares+" shares of "+stockSymbol);
return shares; }
2. Next rerun XDoclet on the firstejb project to resynchronize the Trader and TraderHome classes and the deployment descriptors. 3. Open the Trader.java EJB interface in the Java editor and notice the addition of the buy() and sell() business methods.
Trade EJB Interface
/*
* Generated by XDoclet - Do not edit! */
package com.genuitec.trader.interfaces;
/**
* Remote interface for Trader. * @author XDoclet */
public interface Trader
extends javax.ejb.EJBObject
{
/**
* Buys shares of a stock for a named customer.
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return Number of shares purchased
*/
public int buy( java.lang.String stockSymbol,int shares )
throws java.rmi.RemoteException;
/**
* Sells shares of a stock for a named customer.
* @param customerName String Customer name
* @param stockSymbol String Stock symbol
* @param shares int Number of shares to buy
* @return The number for shares sold
*/
public int sell( java.lang.String stockSymbol,int shares )
throws java.rmi.RemoteException;
}
As you enter XDoclet annotations in the Java editor, note that XDoclet code completion features are available. MyEclipse extends the Java editor with the addition of this feature as well as the ability to invoke code completion on EJB APIs.
4. From the Java editor open on the TraderBean.java go to the class javadoc and place the cursor in the "view-type" XDoclet annotation. Enter ctrl+space to invoke code completion suggestions for this annotation.
Figure 18. XDoclet property value code completion
- Place your cursor on a new line after the jndi-name tag.
- Enter Ctrl+Space to see XDoclet code completion suggestions for other available @ejb.bean tags not already specified.
Figure 19. XDoclet property code completion




