Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.

Slides:



Advertisements
Similar presentations
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB JavaForum.
JBoss Seam: Contextual Components Jason Bechtel
Enterprise Applications & Java/J2EE Technologies Dr. Douglas C. Schmidt Professor of EECS.
Enterprise Java Beans Welcome to the world of “Distributed System” Presented By: Sameer Nanda Date: 12/17/03.
Application Server Lecture Paulo Barroso Kris Carver Todd Kitterman Eric Silva.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Enterprise Java Beans (EJB)
EJB Fundamentals Celsina Bignoli
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.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Developing and Deploying Business Components using PowerJ.
Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
6st ACS Workshop UTFSM ACS Course Component, Container, Lifecycle Management 6st ACS Workshop UTFSM, Valparaiso, Chile H. Sommer, G. Chiozzi.
By Lecturer / Aisha Dawood 1.  You can control the number of dispatcher processes in the instance. Unlike the number of shared servers, the number of.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
September 16, 2010COMS W41561 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser
1 Stateful Session Beans Stateless Session Beans Michael Brockway Sajjad Shami Northumbria University School of Computing, Engineering & Information Sciences.
The Triad of Beans I Oleh: Dini Addiati ( ) Fahrurrozi Rahman ( Y) Irfan Hilmy ( ) Salman Azis A ( ) Aziiz Surahman.
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
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
SCALABLE EVOLUTION OF HIGHLY AVAILABLE SYSTEMS BY ABHISHEK ASOKAN 8/6/2004.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Collaborate Lesson 4C / Slide 1 of 22 Collaborate Knowledge Byte In this section, you will learn about: The EJB timer service Message linking in EJB 2.1.
Multithreading in JAVA
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
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.
1 Distributed Systems Distributed Object-Based Systems Chapter 10.
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.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Middleware Technology (J2EE/EJB) EJB Fundamentals.
EJB Enterprise Java Beans JAVA Enterprise Edition
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
EJB. Introduction Enterprise Java Beans is a specification for creating server- side scalable, transactional, multi-user secure enterprise-level applications.
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved DISTRIBUTED SYSTEMS.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved DISTRIBUTED SYSTEMS.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Session Beans and The Timer Service. Contents  Session Beans  The Timer Service.
Topic: Java Garbage Collection
Chapter 7: User-Defined Functions II
Java Servlets By: Tejashri Udavant..
EJB (Enterprise Java Beans)
Chengyu Sun California State University, Los Angeles
Activities and Intents
Singleton Pattern Command Pattern
Chapter 3: Using Methods, Classes, and Objects
Programming Models for Distributed Application
J2EE Application Development
DISTRIBUTED SYSTEMS Principles and Paradigms Second Edition ANDREW S
Chengyu Sun California State University, Los Angeles
Understanding and Designing with EJB
Chapter 10 Object States and The Statechart Diagram
Objectives In this lesson, you will learn to:
Objectives In this lesson you will learn about: Need for servlets
Understanding and Designing with EJB
CS520 Web Programming Spring – Aspect Oriented Programming
Enterprise Java Beans.
Presentation transcript:

Callbacks and Interceptors

Contents  Session Beans Life Cycle  Interceptors

Life cycle means that a session bean goes through a predefined set of state transitions.  Depending on the type of your bean (stateless, stateful, singleton), the life cycle will consist of different states.  Each time the container changes the life-cycle state, it can invoke methods that are annotated with callback annotations.

Interceptors allow you to add cross-cutting concerns to your beans.  When a client invokes a method on your session bean, the container is able to intercept the call and process business logic before the bean’s method is invoked.

1. Session Beans Life Cycle  Stateless and Singleton  Stateful  Callbacks

1.1.Stateless and Singleton Stateless and singleton beans have in common the fact that they don’t maintain conversational state with a dedicated client. Life cycle of stateless and singleton beans  1. The life cycle of a stateless or singleton session bean starts when a client requests a reference to the bean (using either dependency injection or JNDI lookup). The container creates a new session bean instance.

 2. If the newly created instance uses dependency injection etc.) or deployment descriptors, the container injects all the needed resources.  3. If the instance has a method annotated the container invokes it.

 4. The bean instance processes the call invoked by the client and stays in ready mode to process future calls. Stateless beans stay in ready mode until the container frees some space in the pool. Singletons stay in ready mode until the container is shut down.  5. The container does not need the instance any more. It invokes the method annotated if any, and ends the life of the bean instance.

Stateless and singleton bean life cycle

1.2.Stateful Stateful beans maintain conversational state with their client, and therefore have a slightly different life cycle. The container generates an instance and assigns it only to one client. Then, each request from that client is passed to the same instance.

The stateful bean life cycle  1. The life cycle of a stateful bean starts when a client requests a reference to the bean (either using dependency injection or JNDI lookup). The container creates a new session bean instance and stores it in memory.  2. If the newly created instance uses dependency injection etc.) or deployment descriptors, the container injects all the needed resources.

 3. If the instance has a method annotated the container invokes it.  4. The bean executes the requested call and stays in memory, waiting for subsequent client requests.  5. If the client remains idle for a period of time, the container invokes the method annotated if any, and passivates the bean instance into a permanent storage.

 6. If the client invokes a passivated bean, the container activates it back to memory and invokes the method annotated if any.  7. If the client does not invoke a passivated bean instance for the session timeout period, it is destroyed by the container.  8. Alternatively to step 7, if the client calls a method annotated the container then invokes the method annotated if any, and ends the life of the bean instance.

Stateful bean life cycle

1.3.Callbacks The container lets you optionally provide your own business code when the state of the bean changes. The change from one state to another can be intercepted by the container to invoke methods annotated by one of the annotations.

A callback method is required to have the following signature: void (); The following rules apply to a callback method:  The method must not have any parameters and must return void.  The method must not throw a checked exception but can throw runtime exceptions. Throwing a runtime exception will roll back the transaction if one exists.

 The method can have public, private, protected, or package-level access, but must not be static or final.  A method may be annotated with multiple annotations. However, only one annotation of a given type may be present on a bean.  A callback method can access the beans’ environment entries.

2.Interceptors  Around-Invoke Interceptors  Method Interceptors  Life-Cycle Interceptor

2.1.Around-Invoke Interceptors Aspect-Oriented Programming (AOP)  AOP is a programming paradigm that separates cross-cutting concerns (concerns that cut across the application) from your business code.  Most applications have common code that is repeated across components. These could be technical concerns. These concerns can be applied automatically through AOP to your entire application or to a subset of it.

EJBs support AOP-like functionality by providing the ability to intercept method invocation through interceptors. Interceptors are automatically triggered by the container when an EJB method is invoked. A container intercepting a call and invoking interceptors

Interceptors fall into three types  Around-invoke interceptors  Business method interceptors  Life-cycle callback interceptors

2.1.Around-Invoke Interceptors Adding annotation in the bean itself. CustomerEJB Uses an Interceptor

Any client invocation to createCustomer() or findCustomerById() methods will be intercepted, and the logMethod() will be applied.

@AroundInvoke Object (InvocationContext ic) throws Exception  The following rules apply to an around-invoke method: The method can have public, private, protected, or package-level access, but must not be static or final. The method must have a javax.interceptor.InvocationContext parameter and must return Object, which is the result of the invoked target method (if the method returns void, it returns null). The method can throw a checked exception.

2.2.Method Interceptors To isolate a cross-cutting concern into a separate class and tell the container to intercept the calls on several session beans. To specify an interceptor, you need to develop a separate class and instruct the container to apply it on a specific bean or bean’s method.

An Interceptor Class Logging a Method on Entering and Exiting

The LoggingInterceptor can now be wrapped transparently by any EJB interested in this interceptor. To do this, the bean needs to inform the container with annotation.

If you want the calls to both methods to be intercepted, you can add annotation either on both methods or on the bean itself.

If your bean has several methods, and you want to apply an interceptor to the entire bean except for a specific method, you can use annotation to exclude a call from being intercepted.

2.3.Life-Cycle Interceptor With a callback annotation, you can inform the container to invoke a method at a certain life- Life-cycle interceptors allow you to isolate some code into a class and invoke it when a life-cycle event is triggered.

A Life-Cycle Interceptor Defining Two Methods

Reference Antonio Goncalves, Beginning Java EE 6 Platform with GlassFish 3, Chapter 8, Apress 2009