In this tutorial, you first have to create and implement an EJB project. This provides you with the service that is used by the application client.
The
perspective is opened.1. Create the EJB Project
You develop the EJB classes in an EJB project in the Java EE perspective.
Choose
from the main menu.Select Next .
, then chooseEnter CalculatorServiceEJB in the Project Name field.
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.
Select Add Project to an EAR . Enter CalculatorServiceEAR in the EAR Project Name field. This instantly creates the enterprise project. You can use also an already existing enterprise project.
Choose Finish .
Result
The EJB project and its corresponding Enterprise Application project appear in the Project Explorer .
2. Implement the Session Bean
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
Copy the Java source below.
Expand the project CalculatorServiceEJB .
In the context menu of ejbModule folder, choose Paste .
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); }
The implementation of this interface is a simple Java object. The annotation @Stateless means that the EJB bean is stateless and the annotation @Remote indicates that the CalculatorRemote.class is a remote interface for this bean.
Implementing the Session Bean Class
Copy the Java source below.
Expand the project CalculatorServiceEJB .
In the context menu of ejbModule folder choose Paste .
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; } }
Result
You have implemented the session bean class.