Creating Session Beans
Objectives
What Is a Session Bean?
Stateless Versus Stateful Session Beans
Life Cycle of a Stateless Session Bean
Creating a Stateless Session Bean
Define the Stateless Session Bean
Create the Remote and Local Interfaces
Create a Test Client for the SLSB
Life Cycle of a Stateful Session Bean
Passivation and Activation Concepts
Creating a Stateful Session Bean
Define the Stateful Session Bean
Create the Remote and Local Interfaces
Create a Test Client for the SFSB
Interceptor Methods and Classes
Interceptor Method
Interceptor Classes
Summary
1.01M
Категория: ПрограммированиеПрограммирование

Creating Session Beans

1. Creating Session Beans

2. Objectives

After completing this lesson, you should be able to:
– Describe session beans
– Create stateless and stateful session beans by using annotations
– Understand the passivation and activation of stateful session beans
– Use interceptor methods and classes
Страница 2
www.specialist.ru

3. What Is a Session Bean?

A session bean is a type of Enterprise JavaBean (EJB) that:
– Implements a business process
– Represents a client/server interaction
– Has a short lifespan
– Lives in memory rather than in persistent storage
– Is used to create a session facade
Страница 3
www.specialist.ru

4. Stateless Versus Stateful Session Beans

There are two types of session beans:
– Stateless session bean (SLSB)
Conversation is contained in a single method call.
Business process does not maintain client state.
– Stateful session bean (SFSB)
Conversation may invoke many methods.
Business processes can span multiple method requests, which may require
maintaining state.
EJB container
Pool of SLSBs
EJB container
SFSBs
Client 1
Client 1
Client 2
Client 2
Страница 4
www.specialist.ru

5. Life Cycle of a Stateless Session Bean

Creation
Does not
exist
Ready
Method
Destruction
Страница 5
www.specialist.ru

6. Creating a Stateless Session Bean

To create a stateless session bean:
1. Define the stateless session bean.
2. Define the local and remote interfaces (as needed).
Страница 6
www.specialist.ru

7. Define the Stateless Session Bean

// HelloWorldBean.java
1
package helloworld.ejb
import javax.ejb.Stateless;
2
@Stateless(name="HelloWorld")
4
public class HelloWorldBean implements HelloWorld
{
3
public void sayHello()
5
{
System.out.println("Hello World!");
}
}
Страница 7
www.specialist.ru

8. Create the Remote and Local Interfaces

// HelloWorld.java
package helloworld.ejb
import javax.ejb.Remote;
1
2
@Remote
3
public interface HelloWorld {
public void sayHello(); 4
}
// HelloWorldLocal.java
package helloworld.ejb
import javax.ejb.Local;
1
2
3
@Local
public interface HelloWorldLocal {
public void sayHello(); 4
}
Страница 8
www.specialist.ru

9. Create a Test Client for the SLSB

// HelloWorldClient.java
import helloworld.ejb;
1
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class HelloWorldClient {
public static void main(String [] args)
throws NamingException {
2
try {
final Context context = new InitialContext();
HelloWorld helloWorld =
(HelloWorld)context.lookup("HelloWorld");
helloWorld.sayHello( );
3
} catch (Exception ex) { ex.printStackTrace(); }
};
}
Страница 9
www.specialist.ru

10. Life Cycle of a Stateful Session Bean

Ready (in TX)
TX method
Commit/
rollback
TX Method
Creation
Does not
exist
Method
Ready
Destruction
Passivation
Timeout
Страница 10
Activation
Passive
www.specialist.ru

11.

Passivation and Activation Concepts
Passivation and activation are stages in a session bean’s life cycle
controlled by the EJB container:
– Passivation
Serializes the bean state to secondary storage
Removes the instance from memory
– Activation
Restores the serialized bean’s state from secondary storage
Creates a new bean instance or uses a bean from the pool (initialized with
the restored state)
Страница 12
www.specialist.ru

12. Passivation and Activation Concepts

Creating a Stateful Session Bean
To create a stateful session bean:
1.
Define the stateful session bean.
2.
Define the local and remote interfaces (as needed).
Страница 13
www.specialist.ru

13. Creating a Stateful Session Bean

Define the Stateful Session Bean
// CartBean.java
package cart.ejb
1
import javax.ejb.Stateful;

2
@Stateful(name="Cart")
public class CartBean implements Cart {
private ArrayList items;
@PostConstruct 3
public void initialize() { items = new ArrayList(); }
public void addItem(String item) { items.add(item); }
public void removeItem(String item) {
items.remove(item); }
public Collection getItems() { return items; }
@Remove
4
public void dumpCart() {System.out.println("BYE!");};
}
Страница 14
www.specialist.ru

14. Define the Stateful Session Bean

Create the Remote and Local Interfaces
// Cart.java
package cart.ejb
1
import javax.ejb.Remote;

2
@Remote
public interface Cart {
public void addItem(String item);
public void removeItem(String item);
public Collection getItems();
}
3
// CartLocal.java
package cart.ejb
1
import javax.ejb.Local;

2
@Local
public interface CartLocal { …
Страница 15
www.specialist.ru

15. Create the Remote and Local Interfaces

Create a Test Client for the SFSB
// CartClient.java
import …
public class CartClient {
public static void main(String[] args) throws
Exception {
Context context = new InitialContext();
Cart cart = (Cart) context.lookup("Cart");
cart.addItem("Item1");
cart.addItem("Item2");
Collection items = cart.getItems();
for (Iterator i = items.iterator(); i.hasNext();) {
String item = (String) i.next();
System.out.println(" " + item);
}
cart.dumpCart();
}
}
Страница 16
www.specialist.ru

16. Create a Test Client for the SFSB

Interceptor Methods and Classes
EJB 3.0 introduces the ability to create custom interceptor methods
and classes that are called before invoking the methods they
intercept. Interceptors:
– Are available for only session beans (stateless and stateful) and
message-driven beans
– Provide more granular control of a bean’s method invocation flow
– Can be used to implement custom transaction or security processes
instead of having those services provided by the EJB container
– Are a new feature whose implementation details are not fully defined
and are subject to change
Страница 17
www.specialist.ru

17. Interceptor Methods and Classes

Interceptor Method
import javax.ejb.Stateless;
1
import javax.ejb.AroundInvoke;
import javax.ejb.InvocationContext;
@Stateless
public class HelloWorldBean implements HelloWorld
{
2
@AroundInvoke
3
public Object myInterceptor(InvocationContext ctx)
throws Exception {
System.out.println("Ahem...");
return ctx.proceed(); 4
}
public void sayHello()
{ System.out.println("Hello World!"); }
}
Страница 18
www.specialist.ru

18. Interceptor Method

Interceptor Classes
External interceptor classes can be created to abstract the behavior
of interceptors and to define multiple interceptors for a bean.
// Bean Class
@Stateless
@Interceptor(CheckUserInterceptor.class)
@Interceptor(LogActivity.class)
public class HelloWorldBean implements HelloWorld
{… }
// Interceptor Class
public class CheckUserInterceptor {
@AroundInvoke
public Object checkId(InvocationContext ctx) {…}
}
Страница 19
www.specialist.ru

19. Interceptor Classes

Summary
In this lesson, you should have learned how to:
– Describe session beans
– Create stateless and stateful session beans by using annotations
– Understand the passivation and activation of stateful session beans
– Use interceptor methods and classes
Страница 20
www.specialist.ru

20. Summary

Practice 5 Overview:
Creating Session Beans
This practice covers the following topics:
– Using JDeveloper to generate ServiceRequestFacade as a stateless
session facade for entities
– Creating a Java client application for testing the session facade
functionality
Страница 21
www.specialist.ru
English     Русский Правила