Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Berner Fachhochschule Technik und Informatik Stateless Session Beans Course Multi Tier Business Applications with Java EE Prof. Dr. Eric Dubuis Berner Fachhochschule Biel Content ● ● ● ● ● ● ● April 14, 2011 Characteristics of stateless session beans Architectural view Key concepts and characteristics of stateless session beans Example: Business interface, bean implementation Bean's life cycle Callback annotations Client view MTA with Java EE / Stateless Session Beans 2 Architectural View: Application Servers, EJB Containers Revisited ● Application server and EJB container (Glassfish): 1 3700 (Glassfish) : Client Naming Service (JNDI) IF 3 10 IF: “business interface” 2 IF : Stub 5 4 9 EJB container anonymous 8 : 6 7 : Session Bean Message legend: see next slide ● About enterprise beans: – they live in the EJB container – there are session beans (stateful, stateless) and message-driven beans April 14, 2011 MTA with Java EE / Stateless Session Beans 3 Messages Explained (1) Client looks up, by name, remote service at naming service (2) Naming service returns remote reference (stub [proxy] object, standard requires that all appservers support also CORBA object reference) (3) Client operates on stub (4) Stub serializes parameters and delegates request over the network (5) Appserver gets request, de-serializes parameters, and passes it over to helper object. This object is an instance of a generated class (6) Helper object delegates request to session bean after EJB container has managed (order may change): retrieving bean from pool, injecting dependencies, handling transactions, handling security, ... (7) Session bean returns response (8) Appserver serializes response data (9) Response is passed back over the network to client's stub (10) Stub de-serializes response data and passes response to client April 14, 2011 MTA with Java EE / Stateless Session Beans 4 Some Key Concepts of Enterprise Bean Class The Enterprise Bean Class ● The “component” ● Primary artifact in EJB programming ● Is a “normal” Java class ... – ... but it is annotated – ● Alternatively or additionally: XML deployment descriptor The annotation specifies the semantics of the bean and the requirements for the EJB container The Remote Business Interface ● Sometimes referred to as the remote component interface ● Is a plain Java interface. Optional: May have an EJB3 annotation, too. ● An enterprise bean class may implement more than one remote business interface April 14, 2011 MTA with Java EE / Stateless Session Beans 5 Characteristics of a Stateless Session Beans ● ● ● ● ● ● ● Provides a service to its client. The client-bean association is short-lived; it exists during the execution of one of the bean's methods only. Cannot maintain per-client state between two succinct method calls: – client must pass all state information to the bean as parameters. Can maintain global state information such as JNDI context, ... Can easily be pooled by the container. Are typically used to provide actions in business processes such as "Process Order". "Stateless session bean" is a misnomer; it should be called "service bean". April 14, 2011 MTA with Java EE / Stateless Session Beans 6 A Stateless Session Bean Example: The Business Interface /** * Defines a simple calculator component. */ // @Remote public interface Calculator { public double add(double x, double y); public double subtract(double x, double y); public double multiply(double x, double y); public double divide(double x, double y); } Remarks ● No interface inheritance of a specialized, remote interface! ● No remote exception even for the case that there is a remote implementation. ● Can have a @javax.ejb.Remote EJB3 annotation. (Omitted here to make interface technology independent.) April 14, 2011 MTA with Java EE / Stateless Session Beans 7 The Calculator Bean Class import javax.ejb.Remote; import javax.ejb.Stateless; /** * Implements a simple calculator component. */ @Stateless @Remote(value = { Calculator.class }) public class CalculatorBean implements Calculator { public double add(double x, double y) { return x + y; } public double subtract(double x, double y) { return x – y; } public double multiply(double x, double y) { return x * y; } public double divide(double x, double y) { return x / y; } } April 14, 2011 MTA with Java EE / Stateless Session Beans 8 Remarks Regarding The Calculator Bean Class ● ● The @Stateless annotation tells the EJB container that CalculatorBean is a stateless session bean. The @Remote annotation designates the remote interface of the bean. – – – ● Must be added if corresponding interface lacks @Remote annotation. The value element must be specified if the annotation is applied to the bean class. Must be used if bean class implements more than on remote interface. Since EJB 3.1, JNDI name space syntax is standardized [EJB3.1, 4.4]: – – – portable session bean global JNDI name: java:global[/<app-name>]/<module-name>/<bean-name> [!<fully-qualified-interface-name>] application-specific name space within a Java EE application: java:app/<module-name>/<bean-name> [!<fully-qualified-interface-name>] module-specific name space within a Java EE application: java:module/<bean-name>[!<fully-qualified-interface-name> JNDI: Java Naming and Directory Interface April 14, 2011 MTA with Java EE / Stateless Session Beans 9 Stateless Session Bean Life Cycle [EJB3.1, 4.7.1] April 14, 2011 MTA with Java EE / Stateless Session Beans 10 Callbacks for Stateless Session Beans Supported life cycle event callbacks for stateless session beans: ● @PostConstruct: A method being executed ... – ... after any dependency injection (not discussed in this session) was made – ... before the first business method is executed Usage pattern: For example to establish a connection to a database ● @PreDestroy: A method being executed ... – ... at the time the bean instance is destroyed. Usage pattern: For example to disconnect the database connection Above kind of methods occur in a unspecified transaction and security context. April 14, 2011 MTA with Java EE / Stateless Session Beans 11 Client View ● Communication in a Distributed Environment 1 Naming Service (JNDI) : Client Calculator 3 8 April 14, 2011 Calculator Calculator 2 : Stub Message legend: see next slide – EJB container 5 4 7 : 6 : Calculator Bean "The communication stubs used on the client side are artifacts generated at the enterprise bean’s deployment time by the Container Provider’s tools. The stubs used on the client are specific to the wire protocol used for the remote invocation." [EJB3.1 Core, 15.1.1] MTA with Java EE / Stateless Session Beans 12 Messages Explained (1) Client looks up, by name, remote service at naming service (2) Naming service returns remote reference (a stub [proxy] object, standard requires that all appservers support also CORBA object reference) (3) Client operates on stub (4) Stub serializes parameters and sends request over the network (5) After de-serialization, helper object (= instance of generated class) delegates request to session bean (6) Session bean returns response (7) Appserver's helper object serializes response data and sends it back over the network (8) Stub de-serializes response data and passes response to client April 14, 2011 MTA with Java EE / Stateless Session Beans 13 Client View: Getting Remote Reference via JNDI ● To get a remote reference in a client: import javax.naming.Context; import javax.naming.InitialContext; «interface» Context lookup list ... // in some method: { // Get the initial JNDI context. Context ctx = new InitialContext(); 1 // Get a Calculator session bean. Calculator calculator = (Calculator) ctx.lookup( 2 Initial Context lookup list ... “java:global/calculator1/calculator1-ejb/CalculatorBean”); // Alternative: // delegate the lookup to a Service Locator 3 } ● // Use the bean. calculator.add(12, 25); In a client: Removal of a stateless session bean "The lifecycle of a stateless session bean does not require that it be removed by the client. Removal of a stateless session bean instance is performed by the container, transparently to the client." [EJB3.1 Core, 3.4.5] April 14, 2011 MTA with Java EE / Stateless Session Beans 14 Client View: Getting Remote Reference by Dependency Injection ● To get a remote reference in a client running in an “client application container”: (Note: Can be used in the client's “main” class only) import javax.ejb.EJB; public class Main { 1 @EJB private static Calculator calculator; 2 // for example, in the main() method: public static void main(String[] args) { 3 } April 14, 2011 } // Just use the bean. calculator.add(12, 25); MTA with Java EE / Stateless Session Beans 15 Session Object Identity ● For stateless session beans, the following code fragment is valid [EJB3.1, 3.4.7.2]: Example of dependency injection within a web or EJB container @EJB Cart cart1; @EJB Cart cart2; ... if (cart1.equals(cart1)) { ... } ... if (cart1.equals(cart2)) { ... } April 14, 2011 // this test returns true // this test also returns // true MTA with Java EE / Stateless Session Beans 16 References [EJB3.1] April 14, 2011 JSR 318: Enterprise JavaBeans, Version 3.1 EJB Core Contracts and Requirements http://jcp.org/aboutJava/communityprocess/final/jsr318/index.html MTA with Java EE / Stateless Session Beans 17