< Previous | Next >

Lesson 2: Develop an EJB

About this task

In this lesson you create a session bean, develop a local interface, and develop the implementation class for the bean. You also use the manage EJB exports dialog to expose the EJB as an OSGi service.

Creating a session bean

About this task

In this section, you create a session bean and specify the local interface.

Procedure

  1. Open the session bean wizard. In the EJB project, right-click ejbModule and select New > Session Bean (EJB 3.x).
  2. Create the bean. Enter the following values in the wizard:
    Project
    EJB
    Java™ package
    com.ibm.example.impl
    Class name
    EJBConverter
    State type
    Stateless
    Local
    Select the Local check box and enter the following value: com.ibm.example.EJBConverterLocal
    No-interface View
    Clear No-interface View
    Create EJB 3.x Session Bean
    Note: The application that is developed in this tutorial uses a local interface. If you want to use remote interfaces, select the Remote check box in the wizard and then develop the corresponding interface. JNDI lookups for remote interfaces work differently. With remote interfaces, adjust the lookup code. The next lesson will give an example.
  3. Click Finish. The session bean is created and the class opens in the editor.

Developing the local interface

About this task

When you created the session bean, the EJBConverterLocal interface was created in the EJBClient project. In this section, you add two methods to the interface. The methods describe basic temperature conversion operations.

Procedure

  1. Open the EJBConverterLocal interface. In the EJBClient project, expand the ejbModule folder and the com.ibm.example package. Double-click the EJBConverterLocal interface. The interface opens in the editor.
  2. Add methods to the interface. Add the following methods to the interface:
    public double convertToFahrenheit(double celsius);
    public double convertToCelsius(double fahrenheit);
    The following code is the result:
    package com.ibm.example;
    
    public interface EJBConverterLocal {
    
       public double convertToFahrenheit(double celsius);
    
       public double convertToCelsius(double fahrenheit);
    
    }
  3. Save your changes. Note that when you save the interface file, errors are displayed for the EJBConverter class because it does not yet implement the methods that you added to the interface.

Developing the implementation class

About this task

In this section, you implement the temperature conversion methods that are required by the local interface.

Procedure

  1. Open the EJBConverter class. In the EJB project, expand the ejbModule folder and the com.ibm.example.impl package. Double-click the EJBConverter class. The class opens in the editor.
  2. Remove the default constructor.
  3. Add the implementations for the two methods you added to the EJBConverterRemote interface. Add the following method implementations to the class:
    public double convertToCelsius(double fahrenheit) {
       return (fahrenheit - 32) * (5 / 9d);
    }
    
    public double convertToFahrenheit(double celsius) {
       return celsius * (9 / 5d) + 32;
    }
    The completed class looks like the following code. Note that the original code generated by the wizard contains an annotation, @Stateless, and an annotation, @Local, that specifies the local interface that you entered in the session bean wizard.
    package com.ibm.example.impl;
    
    import com.ibm.example.EJBConverterLocal;
    import javax.ejb.Local;
    import javax.ejb.Stateless;
    
    @Stateless
    @Local(EJBConverterLocal.class)
    public class EJBConverter implements EJBConverterLocal {
    
       public double convertToCelsius(double fahrenheit) {
          return (fahrenheit - 32) * (5 / 9d);
       }
    
       public double convertToFahrenheit(double celsius) {
          return celsius * (9 / 5d) + 32;
       }
    }
  4. Save your changes.

Exposing an EJB as an OSGi service

About this task

By default, EJBs that you add to an OSGi project that has EJB support are automatically exposed as OSGi services. Exposing OSGi services is controlled through the Export-EJB header in the OSGi manifest. In this section, you check the manifest and use the manage EJB exports dialog to confirm that EJBConverter is exposed as an OSGi service.

Procedure

  1. Open the manifest. In the EJB project, double-click Manifest:EJB. The manifest opens. Click the MANIFEST.MF tab to view the file in text format. Note that there is an Export-EJB header with an entry for the EJBConverter EJB. Note that you can manage EJB exports by manually adding entries to the Export-EJB header. EJBs can be added as a comma-separated list.
    Note: There are two special cases of the Export-EJB header to be aware of:
    NONE
    If you specify NONE as an entry for the Export-EJB header, no EJBs are exposed as services. If you specify NONE, but then also add an EJB to the list, a warning is displayed by the tools.
    BLANK
    If you have an Export-EJB header in the manifest but there are no entries, by default all EJBs in the project are exposed as services.
  2. Open the manage EJB export dialog. Right-click the EJB project and select OSGi > Expose EJBs as OSGi services. The manage EJB exports dialog opens. In the dialog, confirm that the box next to the EJBConverter EJB is selected and click OK. You can use this dialog in your projects to add and remove EJBs that are exposed as services. Changing the EJBs selected in this dialog changes the entries to the Export-EJB header in the manifest.
    Manage EJB exports dialog
    Note: If you create new EJBs to the project, they are automatically exposed as OSGi services. You can use the manage EJB exports dialog to clear any EJBs that you do not want to expose.
< Previous | Next >
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: osgi_ejb_2.html