Show TOC

Procedure documentationCreating and Implementing an EJB 3.0 Project Locate this document in the navigation structure

 

In this tutorial, you first have to create and implement an EJB 3.0 project. This provides you with the service that will be used by the application client.

Prerequisites

The   Windows   Open Perspective   Other   Java EE   perspective is opened.

Procedure

1. Create the EJB 3.0 Project

You develop the EJB 3.0 classes in an EJB 3.0 project in the Java EE perspective.

  1. Choose   File   New   Project   from the main menu.

  2. Expand EJB folder and choose EJB Project. Choose Next.

    This graphic is explained in the accompanying text.

  3. Enter CalculatorServiceEJB in the Project name field. In this field you specify the name of your project.

  4. Leave the Default Configuration for SAP Libraries in the Configurations field. The default configurations include the EJB module 3.0 and Java 6.0 project facets.

  5. Select Add project to an EAR. Enter CalculatorServiceEAR in the EAR Project Name field. In this field you specify the name of a new enterprise project. This instantly creates the enterprise project. You can use also an already existing enterprise project.

    This graphic is explained in the accompanying text.

EJB 3.0 Project Settings

  1. Choose Finish.

Result

The EJB 3.0 project and its corresponding Enterprise project appear in the Project Explorer.

2. Implement the Session Bean of the EJB 3.0 Project

To use the calculator service from application clients, you must provide a remote interface.

The interface itself is a normal Java interface (also known as POJI - Plain Old Java Interface).

Implementing the Interface
  1. Copy the Java source below.

  2. Expand the project CalculatorServiceEJB.

  3. From the context menu of ejbModule folder, choose Paste.

    Syntax Syntax

    1. package com.sap.blogs.appclient.calculator.ejb;
      
      /**
       * Simple calculator with a single operation - addition
       */
      public interface CalculatorRemote {
      	/**
      	 * Addition operation
      	 * 
      	 * @param a
      	 *            the first addend
      	 * @param b
      	 *            the second addend
      	 * @return the sum of the two addends
      	 */
      	long add(int a, int b);
      }
      
    End of the code.

The implementation of this interface is a simple Java object. The annotation @Statelessmeans that the EJB bean is stateless and the annotation @Remoteindicates that the CalculatorRemote.class is a remote interface for this bean.

Implementing the Session Bean Class
  1. Copy the Java source below.

  2. Expand the project CalculatorServiceEJB.

  3. From the context menu of ejbModule folder choose Paste.

    Syntax Syntax

    1. package com.sap.blogs.appclient.calculator.ejb;
      
      import javax.ejb.Remote;
      import javax.ejb.Stateless;
      
      @Stateless
      @Remote(CalculatorRemote.class)
      public class CalculatorBean {
      	public CalculatorBean() {
      	}
      
      	public long add(int a, int b) {
      		return a + b;
      	}
      }
      
    End of the code.
Result

You have implemented the session bean class.