December 18, 2014
Hot Topics:

EJB 3 Session Beans

  • December 22, 2006
  • By Raghu R. Kodali, Jonathan R. Wetherbee, and Peter Zadrozny
  • Send Email »
  • More Articles »

The Business Interface

A stateless session business interface is a standard Java interface that does not extend any EJB-specific interfaces. This interface has a list of business method definitions that will be available for the client application. Every session bean must have a business interface that can be implemented by the bean class, generated at design time by tools such as Oracle JDeveloper, NetBeans, or Eclipse; or generated at deployment time by the EJB container.

Business interfaces can use annotations as well, as described in the following list:

  • The @Remote annotation can be used to denote the remote business interface.
  • The @Local annotation can be used to denote the local business interface.

If no annotation is specified in the interface, then it is defaulted to the local interface.

If your architecture has a requirement whereby the client application (web application or rich client) has to run on a different JavaVirtual Machine (JVM) from the one that is used to run the session beans in an EJB container, then you need to use the remote interface. The separate JVMs can be on the same physical machine or on separate machines. If your application architecture is going to use the same JVM for both the client application and the session beans, then use the local interface.

It is possible that your application architecture requires both remote and local interfaces. For example, an enterprise might have an order entry application that is developed using session beans that have business methods for submitting new orders and also addressing administrative tasks, such as data entry for the products. Potentially, you could have two different client applications that access the back-end order entry application, as follows:

  • A web client application (as shown in Figure 3) that can be run in the same JVM as the session bean and used to submit new orders
  • A rich client application (as shown in Figure 4) that runs on an end-user desktop machine and is used by the administrator for data entry purposes

Figure 3. A web client using local interfaces of session beans



Click here for a larger image.

Figure 4. A rich client using remote interfaces of session beans

The SearchFacade session bean has both remote and local interfaces, as shown in Figure 5.



Click here for a larger image.

Figure 5. The business interfaces of the SearchFacade session bean

Listing 2 shows the code snippet for the SearchFacade remote business interface, with an @Remote annotation and a wineSearch() method declaration. The wineSearch() method takes one parameter that represents the type of the wine, and returns a list of wines that match the wine type criteria.

Listing 2. SearchFacade.java

package com.apress.ejb3.chapter02;

import java.util.List;

import javax.ejb.Remote;

@Remote
public interface SearchFacade {
   List wineSearch(String wineType);
}

Listing 3 shows the code snippet for the SearchFacade local business interface, with an @Local annotation and a wineSearch() method declaration.

Listing 3. SearchFacadeLocal.java

package com.apress.ejb3.chapter02;

import java.util.List;

import javax.ejb.Local;

@Local
public interface SearchFacadeLocal {
   List wineSearch(String wineType);
}

Business Methods

The methods implemented in the bean class must correspond to the business methods declared in the remote or local business interfaces. They are matched up based on the convention that they have the same name and method signature. Other methods in the bean class that do not have the corresponding declaration in the business interfaces will be private to the bean class methods.

The SearchFacade bean implements one method, wineSearch(), which has been declared in both remote and local business interfaces. The wineSearch() method returns a static wines list based on the type of wine. Listing 4 shows the implementation for wineSearch().

Listing 4. SearchFacadeBean.java

package com.apress.ejb3.chapter02;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

@Stateless(name="SearchFacade")

public class SearchFacadeBean implements SearchFacade,
   SearchFacadeLocal {
   public SearchFacadeBean() {
   }

   public List wineSearch(String wineType) {
      List wineList = new ArrayList();
      if (wineType.equals("Red"))
      {
         wineList.add("Bordeaux");
         wineList.add("Merlot");
         wineList.add("Pinot Noir");
      }

      else if (wineType.equals("White"))
      {
         wineList.add("Chardonnay");
      }

      return wineList;
   }
}

Dependency Injection

In this section, you will look into using dependency injection in stateless session beans.

EJB 3 containers provide the facilities to inject various types of resources into stateless session beans. Typically, in order to perform user tasks or process requests from client applications, the business methods in the session bean require one or more types of resources. These resources can be other session beans, data sources, or message queues.

The resources that the stateless session bean is trying to use can be injected using annotations or deployment descriptors. Resources can be acquired by annotation of instance variables or annotation of the setter methods. Listing 5 shows an example of setter and instance variable.based injection of myDb, which represents the data source.

Listing 5. Data Source Injection

@Resource
DataSource myDb;

or

@Resource
public void setMyDb(DataSource myDb) {
   this.myDb = myDb;
}

You typically use the setter injections to preconfigure or initialize properties of the injected resource.





Page 2 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel