Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences.

Similar presentations


Presentation on theme: "1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences."— Presentation transcript:

1 1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences CG0165: Advanced Applications Development in Java

2 2 Session Beans l Enterprise Beans are of two types: l Session Beans n Performs a task for a client; optionally may implement a web service l Message Beans n Acts as a listener for a particular messaging type, such as the Java Message Service API

3 3 Session Bean l represents a single client inside the Application Server. l to access an application that is deployed on the server, the client invokes the session bean’s methods. l the session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server. l is similar to an interactive session l is not shared; it can have only one client, in the same way that an interactive session can have only one user. l like an interactive session, a session bean is not persistent. (its data is not saved to a database.) l when the client terminates, its session bean appears to terminate and is no longer associated with the client

4 4 Session Bean Types: Stateful and Stateless l in a stateful session bean, the instance variables represent the state of a unique client-bean session l often called the conversational state l the state is retained for the duration of the client-bean session l if the client removes the bean or terminates, the session ends and the state disappears l this transient nature of the state is not a problem n when the conversation between the client and the bean ends there is no need to retain the state

5 5 Stateless Session Bean l does not maintain a conversational state with the client l when a client invokes the method of a stateless bean, the bean’s instance variables may contain a state, but only for the duration of the invocation l when the method is finished, the state is no longer retained l except during method invocation, all instances of a stateless bean are equivalent n allowing the EJB container to assign an instance to any client l stateless session beans can support multiple clients n they can offer better scalability for applications that require large numbers of clients l typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients

6 6 When to Use Session Beans l if the following circumstances hold: l at any given time, only one client has access to the bean instance l the state of the bean is not persistent, existing only for a short period l the bean implements a web service

7 7 Using Stateful SBs l Stateful session beans are appropriate if any of the following conditions are true: l the bean’s state represents the interaction between the bean and a specific client l the bean needs to hold information about the client across method invocations l the bean mediates between the client and the other components of the application n presenting a simplified view to the client l behind the scenes, the bean manages the work flow of several enterprise beans

8 8 Using Stateful SBs … l a Session Bean has a lifetime only of the current session n not persistent l but it does maintain state information between business method invocations l for example, a stateful session EB for a “Shopping Cart” n would provide business methods for adding and removing items from the cart n the EB would store state information such as u number of items in the cart u quantity of each item in the cart u total cost of items in the cart

9 9 Stateful Session Bean Example – The Interest Calculator l From Deitel, Deitel and Santry: Advanced Java, chap 14

10 10 Using Stateless Session Beans l a stateless session bean can improve performance: l to be used in any of the following conditions: l the bean’s state has no data for a specific client l in a single method invocation, the bean performs a generic task for all clients l for example, you might use a stateless session bean to send an email that confirms an online order.

11 11 Stateless Session Beans l maintain no state information between business method invocations l the EJB container can use any stateless bean instance session to respond to any client’s request l may perform better than stateful session EB because a single stateless session bean can be shared among many clients l the Converter example presented in the Introductory lecture is stateless. another Example: MathTool n a stateless session EB with business methods for generating a Fibonacci series u getFibonacciSeries(...) u 1,1,2,3,5,8,13,21,... n and calculating factorials. u getFactorial(...)  4! = 4*3*2*1 = 24 etc

12 12 The MathTool Stateless Session EJB l From Deitel, Deitel and Santry: Advanced Java, HTP

13 13 How Clients access a Session Bean l a client can access a session bean only through the methods defined in the bean’s business interface l the business interface defines the client’s view of a bean l all other aspects of the bean—method implementations and deployment settings—are hidden from the client l well-designed interfaces simplify the development and maintenance of Java EE applications l three types of client access are allowed by the enterprise beans: remote, local, or web service:

14 14 Remote Client Access to Session Bean l a remote client of an enterprise bean has the following traits: l it can run on a different machine and a different Java virtual machine (JVM) than the enterprise bean it accesses. n It is not required to run on a different JVM. l it can be a web component, an application client, or another enterprise bean. l to a remote client, the location of the enterprise bean is transparent l to create an enterprise bean that has remote access, you must annotate the business interface of the enterprise bean as a @Remote interface

15 15 Local Client Access to Session Bean l a local client has these characteristics: l it must run in the same JVM as the enterprise bean it accesses l it can be a web component or another enterprise bean l to the local client, the location of the enterprise bean it accesses is not transparent l to build an enterprise bean that allows local access, you must annotate the business interface of the enterprise bean as a @Local interface l the local interface defines the bean’s business and lifecycle methods

16 16 Choosing between Remote or Local Access l depends on the following four factors: l tight or loose coupling of related beans: l type of client: l component distribution: l performance: l Note 1: if unsure, choose remote access. n this decision gives more flexibility; in the future you can distribute components to accommodate the growing demands on the application. l Note 2: Occasionally both can be allowed for an Enterprise Bean

17 17 Web Service Client access to Session Bean l a web service client accesses a stateless session bean through the bean’s web service endpoint implementation class. l only business methods annotated as a @WebMethod may be invoked by a web service client l for a code sample, see A Web Service Example: HelloServiceBean in the Java EE Tutorial

18 18 Life Cycle of a Stateful Session Bean

19 19 Life Cycle of a Stateless Session Bean

20 20 The Cart Example l the cart session bean represents a shopping cart in an online bookstore l the bean’s client can add a book to the cart, remove a book, or retrieve the cart’s contents l to assemble cart, you need the following code: n Remote business interface (Cart) n Session bean class (CartBean) n See Listings in Practical handout

21 21 Cart.java package com.sun.tutorial.javaee.ejb; import java.util.List; import javax.ejb.Remote; @Remote public interface Cart { public void initialize(String person) throws BookException; public void initialize(String person, String id) throws BookException; public void addBook(String title); public void removeBook(String title) throws BookException; public List getContents(); public void remove(); }

22 22 Lifecycle Callback Methods l methods in the bean class may be declared as a lifecycle call back method by annotating the method with the following annotations: l javax.annotation.PostConstruct l javax.annotation.PreDestroy l javax.ejb.PostActivate l javax.ejb.PrePassivate l Note: Lifecycle callback methods must be public, return void, and have no parameters.

23 23 Running the Cart example l ant l ant deploy l ant run (>appclient -client cartClient.jar) bpp-run-app-client: [echo] running application client container. [exec] Infinite Jest [exec] Bel Canto [exec] Kafka on the Shore [exec] Caught a BookException: "Gravity's Rainbow" not in cart.

24 24 The HelloService Example l this example demonstrates a simple web service that generates a response based on information received from the client l HelloServiceBean is a stateless session bean that implements a single method, sayHello l HelloServiceBean is the endpoint implementation class l the endpoint implementation class is typically the primary programming artifact for enterprise bean web service endpoints l the web service endpoint implementation class has several requirements.

25 25 HelloServiceBean.java package com.sun.tutorial.javaee.ejb; import javax.ejb.Stateless; import javax.jws.WebMethod; import javax.jws.WebService; @Stateless @WebService public class HelloServiceBean { private String message = "Hello, "; public void HelloServiceBean() {} @WebMethod public String sayHello(String name) { return message + name + "."; }

26 26 Testing HelloService Hello, my name is Duke. What's yours?


Download ppt "1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences."

Similar presentations


Ads by Google