320 likes | 688 Vues
CG0165: Advanced Applications Development in Java. Stateful Session Beans Stateless Session Beans. Michael Brockway Sajjad Shami. Northumbria University School of Computing, Engineering & Information Sciences. Session Beans. Enterprise Beans are of two types: Session Beans
E N D
CG0165: Advanced Applications Development in Java • Stateful Session Beans • Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences
Session Beans • Enterprise Beans are of two types: • Session Beans • Performs a task for a client; optionally may implement a web service • Message Beans • Acts as a listener for a particular messaging type, such as the Java Message Service API
Session Bean • represents a single client inside the Application Server. • to access an application that is deployed on the server, the client invokes the session bean’s methods. • the session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server. • is similar to an interactive session • is not shared; it can have only one client, in the same way that an interactive session can have only one user. • like an interactive session, a session bean is not persistent. (its data is not saved to a database.) • when the client terminates, its session bean appears to terminate and is no longer associated with the client
Session Bean Types: Stateful and Stateless • in a stateful session bean, the instance variables represent the state of a unique client-bean session • often called the conversational state • the state is retained for the duration of the client-bean session • if the client removes the bean or terminates, the session ends and the state disappears • this transient nature of the state is not a problem • when the conversation between the client and the bean ends there is no need to retain the state
Stateless Session Bean • does not maintain a conversational state with the client • 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 • when the method is finished, the state is no longer retained • except during method invocation, all instances of a stateless bean are equivalent • allowing the EJB container to assign an instance to any client • stateless session beans can support multiple clients • they can offer better scalability for applications that require large numbers of clients • typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients
When to Use Session Beans • if the following circumstances hold: • at any given time, only one client has access to the bean instance • the state of the bean is not persistent, existing only for a short period • the bean implements a web service
Using Stateful SBs • Stateful session beans are appropriate if any of the following conditions are true: • the bean’s state represents the interaction between the bean and a specific client • the bean needs to hold information about the client across method invocations • the bean mediates between the client and the other components of the application • presenting a simplified view to the client • behind the scenes, the bean manages the work flow of several enterprise beans
Using Stateful SBs … • a Session Bean has a lifetime only of the current session • not persistent • but it does maintain state information between business method invocations • for example, a stateful session EB for a “Shopping Cart” • would provide business methods for adding and removing items from the cart • the EB would store state information such as • number of items in the cart • quantity of each item in the cart • total cost of items in the cart
Stateful Session Bean Example – The Interest Calculator • From Deitel, Deitel and Santry: Advanced Java, chap 14
Using Stateless Session Beans • a stateless session bean can improve performance: • to be used in any of the following conditions: • the bean’s state has no data for a specific client • in a single method invocation, the bean performs a generic task for all clients • for example, you might use a stateless session bean to send an email that confirms an online order.
Stateless Session Beans • maintain no state information between business method invocations • the EJB container can use any stateless bean instance session to respond to any client’s request • may perform better than stateful session EB because a single stateless session bean can be shared among many clients • the Converter example presented in the Introductory lecture is stateless. • another Example: MathTool • a stateless session EB with business methods for generating a Fibonacci series • getFibonacciSeries(...) • 1,1,2,3,5,8,13,21,... • and calculating factorials. • getFactorial(...) • 4! = 4*3*2*1 = 24 etc
The MathTool Stateless Session EJB • From Deitel, Deitel and Santry: Advanced Java, HTP
How Clients access a Session Bean • a client can access a session bean only through the methods defined in the bean’s business interface • the business interface defines the client’s view of a bean • all other aspects of the bean—method implementations and deployment settings—are hidden from the client • well-designed interfaces simplify the development and maintenance of Java EE applications • three types of client access are allowed by the enterprise beans: remote, local, or web service:
Remote Client Access to Session Bean • a remote client of an enterprise bean has the following traits: • it can run on a different machine and a different Java virtual machine (JVM) than the enterprise bean it accesses. • It is not required to run on a different JVM. • it can be a web component, an application client, or another enterprise bean. • to a remote client, the location of the enterprise bean is transparent • to create an enterprise bean that has remote access, you must annotate the business interface of the enterprise bean as a @Remote interface
Local Client Access to Session Bean • a local client has these characteristics: • it must run in the same JVM as the enterprise bean it accesses • it can be a web component or another enterprise bean • to the local client, the location of the enterprise bean it accesses is not transparent • to build an enterprise bean that allows local access, you must annotate the business interface of the enterprise bean as a @Local interface • the local interface defines the bean’s business and lifecycle methods
Choosing between Remote or Local Access • depends on the following four factors: • tight or loose coupling of related beans: • type of client: • component distribution: • performance: • Note 1: if unsure, choose remote access. • this decision gives more flexibility; in the future you can distribute components to accommodate the growing demands on the application. • Note 2: Occasionally both can be allowed for an Enterprise Bean
Web Service Client access to Session Bean • a web service client accesses a stateless session bean through the bean’s web service endpoint implementation class. • only business methods annotated as a @WebMethod may be invoked by a web service client • for a code sample, see A Web Service Example: HelloServiceBean in the Java EE Tutorial
The Cart Example • the cart session bean represents a shopping cart in an online bookstore • the bean’s client can add a book to the cart, remove a book, or retrieve the cart’s contents • to assemble cart, you need the following code: • Remote business interface (Cart) • Session bean class (CartBean) • See Listings in Practical handout
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<String> getContents(); public void remove(); }
Lifecycle Callback Methods • methods in the bean class may be declared as a lifecycle call back method by annotating the method with the following annotations: • javax.annotation.PostConstruct • javax.annotation.PreDestroy • javax.ejb.PostActivate • javax.ejb.PrePassivate • Note: Lifecycle callback methods must be public, return void, and have no parameters.
Running the Cart example • ant • ant deploy • 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.
The HelloService Example • this example demonstrates a simple web service that generates a response based on information received from the client • HelloServiceBean is a stateless session bean that implements a single method, sayHello • HelloServiceBean is the endpoint implementation class • the endpoint implementation class is typically the primary programming artifact for enterprise bean web service endpoints • the web service endpoint implementation class has several requirements.
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 + "."; } }
Testing HelloService Hello, my name is Duke. What's yours?