July 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

EJB 3 Session Beans

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

The Business Interface

Business interfaces for stateful session beans are similar to those for stateless session beans, and are annotated in the same way, using @Local and @Remote annotations. The ShoppingCart session bean has both remote and local interfaces, as shown in Figure 6.



Click here for a larger image.

Figure 6. Business interfaces for ShoppingCart

You will primarily use the local interface from our web application. The remote interface is added to facilitate unit testing of the bean in this article.

Listings 10 and 11 show the remote and local ShoppingCart business interfaces, with @Remote and @Local annotations, respectively.

Listing 10. ShoppingCart.java

package com.apress.ejb3.chapter02;

import javax.ejb.Remote;

@Remote
public interface ShoppingCart {
}

Listing 11. ShoppingCartLocal.java

package com.apress.ejb3.chapter02;

import javax.ejb.Local;

@Local
public interface ShoppingCartLocal {
}

Alternatively, you can use the coding style shown in Listing 2-12, in which you can specify the @Local and @Remote annotations before specifying @Stateful or @Stateless with the name of the business interface.

Listing 12. ShoppingCartBean.java

package com.apress.ejb3.chapter02;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateful;

@Local({ShoppingCartLocal.class})
@Remote({ShoppingCart.class})
@Stateful(name="ShoppingCart")

public class ShoppingCartBean implements ShoppingCart,
   ShoppingCartLocal {
   public ShoppingCartBean() {
   }
}
Note: In this article, you will follow the earlier convention, in which @Local and @Remote annotations are marked on the business interfaces.

Business Methods

Business methods in stateful session beans are similar to those in stateless session beans. You will augment the ShoppingCart bean by adding business methods that will add and remove wines from the shopping cart, and return a list of cart items.

Listing 13 shows the ShoppingCart bean implementing the addWineItem(), removeWineItem(), and getCartItems() methods.

Listing 13. ShoppingCartBean.java

package com.apress.ejb3.chapter02;
import java.util.ArrayList;

import javax.ejb.Stateful;

@Stateful(name="ShoppingCart")

public class ShoppingCartBean implements ShoppingCart,
   ShoppingCartLocal {
   public ShoppingCartBean() {
   }
   public ArrayList cartItems;

   public void addWineItem(String wine) {
      cartItems.add(wine);
   }

   public void removeWineItem(String wine) {
      cartItems.remove(wine);
   }

   public void setCartItems(ArrayList cartItems) {
      this.cartItems = cartItems;
   }

   public ArrayList getCartItems() {
      return cartItems;
   }
}

Callback Methods

Stateful session beans support callback events for construction, destruction, activation, and passivation. Following are the callbacks that map to the preceding events:

  • PostConstruct: Denoted with the @PostConstruct annotation. Any method in the bean class can be marked with this annotation.

  • PreDestroy: Denoted with the @PreDestroy annotation.
  • PreActivate: Denoted with the @PreActivate annotation.
  • PrePassivate: Denoted with the @PrePassivate annotation.

The PostContruct callback happens after a bean instance is instantiated in the EJB container. If the bean is using any dependency injection mechanism for acquiring references to resources or other objects in its environment, the PostConstruct event happens after injection is performed and before the first business method in the bean class is called.

In the case of the ShoppingCart session bean, you could have a business method called initialize() that initializes the cartItems list, as show in Listing 14.

Listing 14. The PostConstruct Method

@PostConstruct
public void initialize()
   {
   cartItems = new ArrayList();
   }

The PreDestroy callback happens after any method with an @Remove annotation has been completed. In the case of the ShoppingCart session bean, you could have a business method called exit() that writes the cartItems list into a database. In this article, you will just print out a message to the system console to illustrate the callback. Listing 15 shows the code for the exit() method, which has the @PreDestroy annotation.

Listing 15. The PreDestroy Method

@PreDestroy
public void exit()
   {
   // items list into the database.
   System.out.println("Saved items list into database");
   }

The @Remove annotation is a useful life cycle method for stateful session beans. When the method with the @Remove annotation is called, the container will remove the bean instance from the object pool after the method is executed. Listing 16 shows the code for the stopSession() method, which has the @Remove annotation.

Listing 16. The Remove Method

@Remove
public void stopSession()
   {
   // The method body can be empty.
   System.out.println("From stopSession method with @Remove
                       annotation");
   }

The PrePassivate callback kicks in when a stateful session bean instance is idle for too long. During this event, the container might passivate and store its state to a cache. The method tagged with @PrePassivate is called before the container passivates the bean instance.

The PostActivate event gets raised when the client application uses a passivated stateful session bean again. A new instance with restored state is created. The method with the @PostActivate annotation is called when the bean instance is ready.

Interceptors

There are some minor differences between interceptors for stateless and stateful session beans. AroundInvoke methods can be used with stateful session beans. For stateful session beans that implement SessionSynchronization, afterBegin occurs before any methods that have AroundInvoke annotations, and before the beforeCompletion() callback method.





Page 4 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel