Title: EJB Session Beans
1EJB Session Beans
- Controlling your application
- (the business logic)
2What you need to know
- Advantage of using Session EJBs
- Guidelines for their use
- Know the Session EJB pattern
- Define and describe the role of a Java Interface
class - Stateful vs. stateless EJBs
- Passivation vs. activation
- Remote vs. local interface
- Bean vs. container managed Session EJBs
- Naming directory services and JNDI
- Algorithm for using Session EJBs
- Develop an application using Session EJBs and
describe the function of each statement in the
program.
3Advantages
- Automatic Transaction management
- Resource management
- Security
4Basic EJB Pattern
ltltEJBgtgt
Bean(Implementation)
Implementation
Client Interface
5Full Internal View
ltltEJB Session BeangtgtOrderControl
Client Interface
Implementation
6Remote vs. Local Entity Beans
- Remote Session EJB
- Anywhere on the network
- Pass by value
- Local EJB are faster but not as flexible,
scalable and adaptable - On same server as Session Beans
- No network traffic
- Pass by reference
7Stateful vs. Stateless Transactions
- A stateless transactions is
- Usually a one step transaction
- Does not the save transaction state data
- No memory is allocated in the pool for each
session with a client - EJB is never passivated or activated
- Stateful transaction
- Used for multi-step transactions
- Saves state data for each client session
- Memory is allocated in pool for each client
session - EJB is passivated and activated
8Stateless Session Bean LifeCycle
Does not exist
Class.newInstance(), _at_PostConstruct
Ready in pool
Business method
9Stateful EJB Session Bean Lifecycle
10Locating an EJB
- EJBs can be remote anywhere on the network
- Must use a directory service and DNS to locate
network objects - JNDI (Java Naming Directory Interface)
- Programming interface to the directory services
to locate any object in a network (files, EJBs,
web services, etc.)
11Directory Service names
- Most use X.500 naming standard
- c (country name)
- o (organizationName)
- ou (organizationUnitName)
- l (localityName)
- cn (commonName)
- dc (domainComponent)
- uid (userid)
- Each type of directory service has its own
naming syntax - LDAP example
- cnMartin Bond, ouAuthors, oSAMS, cus
- Microsoft Active Directory Service
- cnMartin Bond/ouAuthors/oSAMS/cus
12JNDI names
- JNDI names are not specific to directory services
- JNDI maps universal name to specific directory
service syntax - Typical JNDI name
- SAMS/authors/Martin Bond
13Guidelines for use
- Coarse-Grained better than Finely-Grained
- Use stateless whenever possible
- Use the remote interface for flexibility
14Outline for using remote EJBs
- Get initial naming context for EJB
- Use JNDI to lookup and find reference to object
- Create a client session for the EJB
- (returns remote interface)
- Call business methods defined in the remote
interface
15Locate a Remote Session EJB
try InitialContext initialContext new
InitialContext() // get jndi context String
jndiName BusinessRulesBean.RemoteJNDIName //
get jndi name // lookup and get remote
interface for session bean
BusinessRulesRemote businessRulesRemote
(BusinessRulesRemote)
initialContext.lookup(jndiName )
catch (NamingException ne) throw new
MyAppException(ne.getMessage())
16Calling business methods
try InitialContext initialContext new
InitialContext() // get jndi context String
jndiName BusinessRulesBean.RemoteJNDIName //
get jndi name // lookup and get remote
interface for session bean
BusinessRulesRemote businessRulesRemote
(BusinessRulesRemote)
initialContext.lookup(jndiName ) // call
any business methods defined in the interface
Person person businessRulesRemote.login(usernam
e, password) catch (Exception ne)
throw new MyAppException(ne.getMessage())