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

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
6/11/2015Page 1 Web Services-based Distributed System B. Ramamurthy.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
1 CS 483 Enterprise and Web Application Programming Week 6 Session Beans JSP Tutorial.
ECSE Software Engineering 1I HO 7 © HY 2012 Lecture 7 Publish/Subscribe.
Enterprise Java Beans (EJB)
Introduction to EJB INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Struts 2.0 an Overview ( )
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
CSCI 6962: Server-side Design and Programming Course Introduction and Overview.
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
1 J2EE Components. 2 Application Servers relieve the programming burden for business distributed components. They provide support for system level services.
1 l Enterprise Computing -- 3-tier and multi-tier applications l Enterprise JavaBeans n EJB Features n Types of EJBs l Example – A Currency Converter n.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Developing and Deploying Business Components using PowerJ.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
September 16, 2010COMS W41561 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
Middleware Technology (J2EE/EJB) Stateful Session Bean.
Introduction to J2EE Architecture Portions by Kunal Mehta.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Session Beans INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
J2EE Structure & Definitions Catie Welsh CSE 432
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Chapter 6 Server-side Programming: Java Servlets
1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.
1 Java EE Programming Enterprise JavaBeans. 2 Topics J2EE Overview Enterprise JavaBeans Overview Enterprise Entity Beans Case Study How to build them.
1 Transactions Michael Brockway Sajjad Shami CG0165: Advanced Applications Development in Java Northumbria University School of Computing, Engineering.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to Enterprise JavaBeans Topics In Systems Architecture Barry Herbold
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Copyright © 2004, Keith D Swenson, All Rights Reserved. OASIS Asynchronous Service Access Protocol (ASAP) Tutorial Overview, OASIS ASAP TC May 4, 2004.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
EJB Enterprise Java Beans JAVA Enterprise Edition
DEVELOPING WEB SERVICES WITH JAVA DESIGN WEB SERVICE ENDPOINT.
September 28, 2010COMS W41561 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser
EJB. Introduction Enterprise Java Beans is a specification for creating server- side scalable, transactional, multi-user secure enterprise-level applications.
The Java API for XML-Based Web Services. A Web Service example in Java SOAP-awareServlet (e.g. Apache Axis2) SOAP-awareServlet Any class processing the.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.
EJB (Enterprise Java Beans)
Web Services-JAX-RPC JAX-RPC enables a Web Service endpoint to be developed using either a Java Servlet or Enterprise JavaBeans (EJB) component model.
Introduction to J2EE Architecture
Understanding and Designing with EJB
Luca Simone Software Engineering 2 a.a. 2001/2002
Understanding and Designing with EJB
Component-based Applications
Component Technology Bina Ramamurthy 2/25/2019 B.Ramamurthy.
Understanding and Designing with EJB
Distributed System using Web Services
Enterprise Java Beans.
Distributed System using Web Services
Presentation transcript:

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 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 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 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 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 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 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 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 Stateful Session Bean Example – The Interest Calculator l From Deitel, Deitel and Santry: Advanced Java, chap 14

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 that confirms an online order.

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 The MathTool Stateless Session EJB l From Deitel, Deitel and Santry: Advanced Java, HTP

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 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 interface

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 interface l the local interface defines the bean’s business and lifecycle methods

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 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 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 Life Cycle of a Stateful Session Bean

19 Life Cycle of a Stateless Session Bean

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 Cart.java package com.sun.tutorial.javaee.ejb; import java.util.List; import 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 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 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 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 HelloServiceBean.java package com.sun.tutorial.javaee.ejb; import javax.ejb.Stateless; import javax.jws.WebMethod; public class HelloServiceBean { private String message = "Hello, "; public void HelloServiceBean() public String sayHello(String name) { return message + name + "."; }

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