The next step is to create an EJB. For simplicity, it will be a stateless session bean, but others types are also easy to write.
Create a new Session EJB. Select File > New > Other... and choose JBoss-IDE > EJB Components > Session Bean. | ![]() |
The package will be tutorial.ejb and the class name “FiboBean”. Leave the default options selected and be sure that ejbCreate() method is checked. | ![]() |
Click on “Finish”. The class is then created and you should have a project like this. Note that all the method stubs are created with the default ejbCreate method. | ![]() |
To make this interesting, we will create a business method for our EJB that computes a Fibonacci suite.
Right-click the FiboBean class, under the FiboBean Java file. You should see a J2EE menu. Select J2EE > Add Business Method. | ![]() |
In the method wizard, enter compute as the method name, double[] for the return type and add a parameter called number of type int. Click on Finish. | ![]() |
A new method has been added to the FiboBean class. | ![]() |
In the text editor, complete the body of the compute method as below :
public double[] compute(int number) { if (number < 0) { throw new EJBException("Argument should be positive"); } double[] suite = new double[number + 1]; suite[0] = 0; if (number == 0) { return suite; } suite[1] = 1; for (int i = 2; i <= number; i++) { suite[i] = suite[i - 1] + suite[i - 2]; } return suite; }
As you may have noticied, each wizard adds all of the required XDoclet tags. Go to the top of the class and complete the attributes of the tag with the following values (by pressing CTRL+Space for each attribute, you will get an auto-compled list) :
/** * @ejb.bean name="Fibo" * display-name="Name for Fibo" * description="Description for Fibo" * jndi-name="ejb/Fibo" * type="Stateless" * view-type="remote" */ public class FiboBean implements SessionBean {
After that, the file should look like this. Now, we are ready to run XDoclet on the file to generate the EJB interfaces.
package tutorial.ejb; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; /** * @ejb.bean name="Fibo" * display-name="Name for Fibo" * description="Description for Fibo" * jndi-name="ejb/Fibo" * type="Stateless" * view-type="remote" */ public class FiboBean implements SessionBean { /** * */ public FiboBean() { super(); // TODO Auto-generated constructor stub } /* * (non-Javadoc) * * @see javax.ejb.SessionBean#ejbActivate() */ public void ejbActivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see javax.ejb.SessionBean#ejbPassivate() */ public void ejbPassivate() throws EJBException, RemoteException { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see javax.ejb.SessionBean#ejbRemove() */ public void ejbRemove() throws EJBException, RemoteException { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext) */ public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { // TODO Auto-generated method stub } /** * Default create method * * @throws CreateException * @ejb.create-method */ public void ejbCreate() throws CreateException { // TODO Auto-generated method stub } /** * Business method * * @ejb.interface-method view-type = "remote" */ public double[] compute(int number) { if (number < 0) { throw new EJBException("Argument should be positive"); } double[] suite = new double[number + 1]; suite[0] = 0; if (number == 0) { return suite; } suite[1] = 1; for (int i = 2; i <= number; i++) { suite[i] = suite[i - 1] + suite[i - 2]; } return suite; } }