14
1 Module 7 Implementing EJB 3.0 Session Beans Implementing Session Beans 7 193 Omega Developing Applications for Java EE Platform Compare stateless and stateful behavior Describe the operational characteristics of a stateless session bean Describe the operational characteristics of a stateful session bean Create session beans Package and deploy session beans Create a session bean client Objectives

Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

  • Upload
    vuhanh

  • View
    224

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

1

Module 7Implementing EJB 3.0 Session Beans

Impl

emen

ting

Ses

sion

Bea

ns7

193 Ω Omega ΩDeveloping Applications for Java EE Platform

Compare stateless and stateful behavior

Describe the operational characteristics of a stateless session bean

Describe the operational characteristics of a statefulsession bean

Create session beans

Package and deploy session beans

Create a session bean client

Objectives

Page 2: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

2

Impl

emen

ting

Ses

sion

Bea

ns7

194 Ω Omega ΩDeveloping Applications for Java EE Platform

Session beans can be stateless or stateful. The statefulness of a bean depends on the type of business function it performs: In a stateless client-service interaction, no client-specific information is maintained beyond the duration of a single method invocation. Stateful services require that information obtained during one method invocation be available during subsequent method calls:

• Shopping carts• Multi-page data entry• Online banking

Comparison of Stateless and Stateful

Behavior

Impl

emen

ting

Ses

sion

Bea

ns7

195 Ω Omega ΩDeveloping Applications for Java EE Platform

With stateless session beans:

The bean does not retain client-specific information.

A client might not get the same session bean instance.

Any number of client requests can be handled by the same session bean instance. This has a profound impact on performance.

Stateless Session Bean Operational

Characteristics

Page 3: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

3

Impl

emen

ting

Ses

sion

Bea

ns7

196 Ω Omega ΩDeveloping Applications for Java EE Platform

Stateless Session Bean Cardinality

Impl

emen

ting

Ses

sion

Bea

ns7

197 Ω Omega ΩDeveloping Applications for Java EE Platform

With stateful session beans:

The bean belongs to a particular client for an entireconversation.

The client connection exists until the client removes the bean or the session times out.

The container maintains a separate EJB object and EJB

instance for each client.

Stateful Session Bean Operational

Characteristics

Page 4: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

4

Impl

emen

ting

Ses

sion

Bea

ns7

198 Ω Omega ΩDeveloping Applications for Java EE Platform

Stateful Session Bean Cardinality

Impl

emen

ting

Ses

sion

Bea

ns7

199 Ω Omega ΩDeveloping Applications for Java EE Platform

Declare a business interface for the session bean.

Create the session bean class that implements the business

interface.

Configure the session bean by either annotating the session bean class or providing a deployment descriptor (DD).

Creating Session Beans

Page 5: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

5

Impl

emen

ting

Ses

sion

Bea

ns7

200 Ω Omega ΩDeveloping Applications for Java EE Platform

A session EJB component’s business interface has the following characteristics:

Outlines the EJB component’s business methods.

Declares the Session as a distributed component (optional)

Declaring a Business Interface for the

Session Bean

import javax.ejb.*;

@Remotepublic interface HelloSession

public java.lang.String hello();

Impl

emen

ting

Ses

sion

Bea

ns7

201 Ω Omega ΩDeveloping Applications for Java EE Platform

A session EJB component’s class has the following characteristics:

Often referred to as the bean class, implementation instance, or EJB class

Implements the component’s business interface

Contains code that implements bean life-cycle methods(optional)

Creating the Session Bean Class That

Implements the Business Interface

Page 6: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

6

Impl

emen

ting

Ses

sion

Bea

ns7

202 Ω Omega ΩDeveloping Applications for Java EE Platform

Creating the Session Bean Class That

Implements the Business Interface

import javax.ejb.*;

@Statelesspublic class HelloSessionBean implements HelloSession

public java.lang.String hello() return "Hello World!";

Impl

emen

ting

Ses

sion

Bea

ns7

203 Ω Omega ΩDeveloping Applications for Java EE Platform

Declaring a Local Business Interface for a

Session Bean

@Local public interface Calculator ...@Stateless public class CalculatorBean implements Calculator ...

public interface Calculator ...@Local @Stateless public class CalculatorBean implements Calculator ...

public interface Calculator ...@Statelesspublic class CalculatorBean implements Calculator ...

How can a business interface be designated a local interface?

Page 7: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

7

Impl

emen

ting

Ses

sion

Bea

ns7

204 Ω Omega ΩDeveloping Applications for Java EE Platform

Declaring a Remote Business Interface for

a Session Bean

public interface Calculator ...@Remote @Stateless public class CalculatorBean implements Calculator ...

@Remote public interface Calculator ...@Statelesspublic class CalculatorBean implements Calculator ...

How can a business interface be designated a remote interface?

Are there any additional requirements for remote interfaces?

Can a session bean have more than one business interface?

Impl

emen

ting

Ses

sion

Bea

ns7

205 Ω Omega ΩDeveloping Applications for Java EE Platform

The class must be a top-level class. In addition, the class must be defined as public, must not be final, and must not be abstract.

The class must have a public constructor that takes no parameters. The container uses this constructor to createinstances of the session bean class.

The class must not define the finalize method.

The class must implement the methods of the bean’s business interface(s), if any.

Requirements for a Session Bean Class

Page 8: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

8

Impl

emen

ting

Ses

sion

Bea

ns7

206 Ω Omega ΩDeveloping Applications for Java EE Platform

Other common annotations: Transaction-related annotations Security-related annotations Life-cycle callback handler method annotations Resource-related annotations

Annotating the Session Bean Class

Impl

emen

ting

Ses

sion

Bea

ns7

207 Ω Omega ΩDeveloping Applications for Java EE Platform

PostConstruct callback PreDestroy callback

Life Cycle of a Stateless Session Bean

Page 9: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

9

Impl

emen

ting

Ses

sion

Bea

ns7

208 Ω Omega ΩDeveloping Applications for Java EE Platform

PostConstruct callback PreDestroy callback PostActivate callback PrePassivate callback

Life Cycle of a Stateful Session Bean

Impl

emen

ting

Ses

sion

Bea

ns7

209 Ω Omega ΩDeveloping Applications for Java EE Platform

A callback method is designated using the appropriate callback annotation. The same method can be designated to handle multiple callback events. Each callback event can only have one callback method designated to handle it. A callback method can access entries in the bean’s environment. Callback methods can throw runtime exceptions. Callback methods must not throw application exceptions. Callback methods can have any type of access modifier.

Defining Life-Cycle Event Handlers

Page 10: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

10

Impl

emen

ting

Ses

sion

Bea

ns7

210 Ω Omega ΩDeveloping Applications for Java EE Platform

import javax.ejb.*;

import java.util.*;

@Stateful public class ShoppingCartBean implements ShoppingCart

private float total;

private List productCodes;

public int someShoppingMethod() ...;

//...

@PreDestroy private void endShoppingCart() ...;

Example of a Callback Method in a Bean Class

Impl

emen

ting

Ses

sion

Bea

ns7

211 Ω Omega ΩDeveloping Applications for Java EE Platform

import javax.ejb.*;

import javax.annotation.*;

@Stateful

public class BankingBean implements Bank

@Resource private javax.ejb.SessionContext context;

The SessionContext Object

Use the SessionContext object to access EJB objects, obtain current transaction status, and obtain security information.

Page 11: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

11

Impl

emen

ting

Ses

sion

Bea

ns7

212 Ω Omega ΩDeveloping Applications for Java EE Platform

Packaging and deployment tasks: Optionally create a DD file for the session bean component Create a deployable session bean component archive Deploy the session bean component archive

Session Bean Packaging and Deployment

Impl

emen

ting

Ses

sion

Bea

ns7

213 Ω Omega ΩDeveloping Applications for Java EE Platform

<ejb-jar><enterprise-beans>

<session><ejb-name>ReportBean</ejb-name><business-remote>services.Report</business-remote><ejb-class>services.ReportBean</ejb-class><session-type>Stateful</session-type><transaction-type>Container</transaction-type>

</session><!-- Deployment information for additional beans goes here--></enterprise-beans><assembly-descriptor><!-- Add assembly information here--></assembly-descriptor></ejb-jar>

Session Bean DD Example

Page 12: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

12

Impl

emen

ting

Ses

sion

Bea

ns7

214 Ω Omega ΩDeveloping Applications for Java EE Platform

1. Create a working directory structure.2. Copy the EJB component class files and helper classes into their corresponding subdirectories. 3. Create a META-INF subdirectory off the root directory.4. From a command line, execute the jar utility to create the EJB JAR archive.

Creating a Session Bean Component Archive

Impl

emen

ting

Ses

sion

Bea

ns7

215 Ω Omega ΩDeveloping Applications for Java EE Platform

Example of the EJB Module JAR File

Page 13: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

13

Impl

emen

ting

Ses

sion

Bea

ns7

216 Ω Omega ΩDeveloping Applications for Java EE Platform

public class InternalClient

@EJB // step 1

private static Calculator calc;

public static void main (String args[])

int sum = calc.add(1, 2); // step 2

Creating a Session Bean Client

Any managed component, such as a session EJB, message EJB, or servlet can perform dependency injection to locate anEJB using annotations and an instance variable.

Creating a client using container services:

Impl

emen

ting

Ses

sion

Bea

ns7

217 Ω Omega ΩDeveloping Applications for Java EE Platform

public class InternalClient

public static void main (String args[])

// step 1 begin

InitialContext ctx = new InitialContext();

Object obj = ctx.lookup("ejb/Calculator");

Calculator calc = (Calculator)

PortableRemoteObject.narrow(obj, Calculator.class);

// step 1 end

int sum = calc.add(1, 2); // step 2

Creating a Session Bean Client

Creating a client without using container services:

Page 14: Module 7 Implementing EJB 3.0 Session Beans - ITUweb.itu.edu.tr/bkurt/Courses/bte531/bte531.module.07.implementing... · 1 Module 7 Implementing EJB 3.0 Session Beans Implementing

14

Impl

emen

ting

Ses

sion

Bea

ns7

218 Ω Omega ΩDeveloping Applications for Java EE Platform

<ejb-ref><ejb-ref-name>BrokerLookup</ejb-ref-name><ejb-ref-type>Session</ejb-ref-type><remote>trader.BrokerModel</remote>

</ejb-ref>try

InitialContext ctx = new InitialContext();model = (BrokerModel)ctx.lookup("java:comp/env/BrokerLookup");

catch(NamingException ne) ...

Portable JNDI Session Bean Clients

The EJB specification does not require local session beans to have adefault JNDI name. To perform portable JNDI lookups of a session bean requires editing the web.xml file or the ejb-jar.xml file depending on the client type. A web tier example: