LevSelector.com New York
home > WebLogic

Enterprise Javabeans : Developing Component-Based Distributed Applications.
by Thomas C. Valesky

My Conspectus.
Chapters
_1_ - (pp.1-16) started and finished 11/11/00
_2_ - (pp.17-48)
_3_ -
_4_ -
_5_ -
_6_ -
_7_ -
_8_ -
_9_ -
_10_ -

Preface:
http://www.awl.com/cseng/titles/0-201-60446-9/ - online supplement to this book

 
Chapter 1 home - top of the page -

Chapter 1 The Big Picture 1
Introduction 1
Transaction Processors
  - Example with money transfer between 2 accounts
  - begin transaction, commit or rollaback
  - logging mechanism to protect transaction even in case of a system failure
ACID Properties of Transactions 3
     = Atomicity (all actions will either commit or not together)
     = Consistency (database is always in reasonable state and can survive system failure)
     = Isolation (state of transaction is hidden until it is completed)
     = Durability (results are stored permanently)
OLTP Versus OLAP 3
 OLTP - Online Transaction Processing
   - used to update database, typically high volume, response time is important
     OLTP is where EJB is mostly aimed to be used.
 OLAP - Online Analytical Processing (also called DSS - Decision Support Systems, EIS - Enterprise Information Systems)
   - used to help review and analyse information in the database. Typically doesn't update database. Queries may be complicated and long.

Two-Tier, Client-Server Architecture 4
   - database server contains data
   - client contains both presentation logic and business rules

   Problem - every time business logic is changed - we have to distribute new software to all clients.

Three-Tier Architecture 7
   - database server contains data
   - middle-tier server - contains business rules
   - client contains presentation logic

3-tier can be implemented using: Sockets, RPCs, CORBA, RMI, OLE/DCOM, Message Queues
 
Sockets 8
 Sockets act as an interface  to networking services. All internet is build on top of sockets.
 Address = IP address + port.
 A socket client and server must have an agreed-upon protocol for conversation.
 The server should be able to handle many concurrent connections.

RPCs 9
  RPC (Remote Procedure Call) - a thin layer on top of sockets, allowing you to call a remote procedure as if it were any other procedure in your program. You actually call a routine called a "stub".  Stubs a typically generated by a compiler using an IDL (Interface Definition Language) to describe the procedure, arguments and return types.

CORBA 9
  CORBA - Common Object Request Broker Architecture. It is a Object-Oriented RPC mechanism, it also uses IDL, but it includes objects descriptions. The IDL compiler generates stubs and skeletons (skeleton is a server-side stub). 

Marshalling - process of taking parameters and transforming them into a format suitable for network transmission.
Unmarshalling - process of transforming data received via network into "normal" data types.

ORB - Object Request Broker - sits between client and server. Client and server can be written in different languages, but still communicate via ORB.  EJB includes a CORBA mapping allowing misc. clients to talk to EJB.

RMI 10
  RMI - Remote Method Invocation - a java-only version of CORBA.

OLE/DCOM 11
  OLE/DCOM - Microsoft version of distributed computing (doesn't mix well with EJB).
     OLE = Object Linking and Embedding, 
     DCOM = Distributed Component Object Model)
     DNA - Distributed interNetworking Architecture

Message Queues 11
  MQ is a server to receive and forward messages between client and server. If the server is busy, the message will wait in a queue. Thus communication is asynchronous, like email. In java - JMS (Java Message Service).

Distributed Transaction Processing 12
  A distributed transaction is the one involving several participants (TP = transaction processors) on the network.
The usual approach is "two-phase commit":
   1st phase - TC (Transaction Coordinator) tells each TP to prepare the transaction (write it to a log). Each TP replies with "vote commit" or "vote rollback".
   2nd phase - TC notifies each TP to commit (rollback) the transaction.

 Transactions can be nested in each other (or flat).

EJB's Role 13
  - EJB specifies a container (execution environment) to provide services to EJB, such as
    - support for transactions,
    - support for persistence,
    - management of multiple instances of a given bean
    - proxy object for each bean to communicate with the client
  - EJB exists in the middle tier (typically EJB consists of methods)
  - EJB support transactions (making programming easy)
  - EJB can maintain state accross several method invocations.
          (or, if you need, you can also have stateless beans).
  - EJBs are simple (because container takes care of everything).
    - EJBs are portable (platform-independent)
    - EJBs supported by many vendors
Conclusion 16

 
Chapter 2 home - top of the page -

Chapter 2 EJB's Architecture 17
Logical Architecture 17
  Client -- EJB Server -- Database

Overview of EJB's Software Architecture 18
  Client --- EJB Server --- EJB Container --- EJB Home and Remote Interfaces -- EJB Bean

EJB Servers 19
  EJB Server is a "container container" - it contains the EJB container.

EJB Containers 21
  EJB Containers provide:
    - transaction support: beans requirements for transaction support can be declared in the "deployment descriptor": (for example: TX_NOT_SUPPORTED, TX_BEAN_MANAGED, TX_REQUIRED, TX_SUPPORTS, TX_REQUIRES_NEW, TX_MANDATORY).  EJB transactions are not nested.
    - Support for management of multiple instances:
        - instance passivation, (temporarily swap out the bean if container needs resources)
        - instance pooling, (share stateless beans among clients)
        - database connection pooling,
        - precached instances, (to avoid unnecessary loading info from DB)
        - optimized method invocations.(A typical EJB communication is the same whether it is with another EJB or a client and uses remote invocation as follows:
            -- use a naming service to locate the EJB's home interface,
            -- make a call to this home interface to gain access to its remote interface,
            -- make calls to the business methods against the remote interface
          If both communicating parties are EJBs in the same container, it is possible to avoid overhead of this remote invocation thus making communication much faster - but it still should go through a proxy object.)
    - Support for persistence: Entity beans are persistent (stored in a database). Persistence can be container- or bean-managed.
    - Support for security: ACL (Access Control List) - list of persons or groups and their permissions. ACLs can be specified for a bean's methods or for a bean as a whole.

 ==============================================================

Enterprise Beans 26
   Session beans - Stateful or Stateless?  Session Synchronization ?
   Entity Beans - Bean Managed or Container-Managed Persistance? Re-entrant?

   EJB Home Interface:
          must extend EJBHome interface
 
          public interface javax.ejb.EJBHome extends java.rmi.Remote {
                public abstract EJBMetaData getEJBMetaData();
                public abstract void remove(Handle handle);
                public abstract void remove(Object primaryKey);
          }

 --------------------------------------------------------------

   When the client uses a naming service (JNDI - Java Naming and Direcoty Interface) to look up an EJB bean, the naming service returns a reference to an object which implements the bean's home interface.

   There are 3 methods in this interface:
       getEJBMetaData() - returns a reference to an object that implements the EJBMetaData interface:
          public interface javax.ejb.EJBMetaData {
                public abstract EJBHome getEJBHome();
                public abstract Class getHomeInterfaceClass();
                public abstract Class getPrimaryKeyClass();
                public abstract Class getRemoteInterfaceClass();
                public abstract boolean isSession();
          }

    Other 2 methods remove an EJB object using a Handle or primaryKey (onloy an Entity bean may have a primary key):
       remove(Handle handle)
       remove(Object primaryKey)

    EJBHome interface doesn't provide any methods to create new beans or look up exising ones. Instead the EJB developer must write the method signatures for these tasks in their own interfaces, and they are different for Session and Entity beans.

 --------------------------------------------------------------

The EJBObject interface:
          public interface javax.ejb.EJBObject extends java.rmi.Remote {
                public abstract EJBHome getEJBHome();  // this method returns ref. to home
                public abstract Handle getHandle();
                public abstract Object getPrimaryKey();
                public abstract boolean isIdentical(EJBObject obj);
                public abstract void remove();
          }

          getEJBHome() - returns a reference to an object implementing the EJB's object home interface

          getHandle() - returns a handle, which is a serializable reference to the object
           (The Handle interface specifies one method - getEJBObject() to recreate a remote reference from a handle)

          getPrimaryKey() - returns the primary key (for Entity bean only)
          isIdentical(EJBObject obj) - test if 2 references refer to the same EJB object
          remove() - to remove the remote EJB object

 --------------------------------------------------------------

The EnterpriseBean Interface
          public interface javax.ejb.EnterpriseBean extends java.io.Serializable {
          }

This interface serves as a common point of ancestry for SessionBean and EntityBean interfaces (and requires them to be serializable).

 --------------------------------------------------------------
Session Bean - implements SessionBean inerface:
 --------------------------------------------------------------

          public interface javax.ejb.SessionBean extends javax.ejb.EnterpriseBean {
                public abstract void ejbActivate();
                public abstract void ejbPassivate();
                public abstract void remove();
                public abstract void setSessionContext(SessionContext ctx);
          }

  All the methods in this interface are callback methods - they invoked by the container.

          ejbActivate() - is called by container right after restoring a Session bean from storage.
             (can be used to restore any resources that were closed before being passivated)
          ejbPassivate() - is called by container immediately before swapping session bean into storage
             (enabling the session bean to perform necessary cleanup, for example close files, etc.)
             passivation itself is accomplished via serialization and storing (to a database)
          remove() - invokes ejbRemove() method of the instance (to do final cleanup)
          setSessionContext(SessionContext ctx) - called at the beginning of Session bean life.
               The bean receives from the container an SessionContext object and stores it.
               Later it can use this object to interract with container-provided services
                (security and transaction management).

 --------------------------------------------------------------
 
          public interface javax.ejb.EJBContext {
              public abstract Identity     getCallerIdentity();
              public abstract EJBHome    getEJBHome();
              public abstract Properties  getEnvironment();
              public abstract boolean     getRollbackOnly();
              public abstract UserTransaction getUserTransaction();
              public abstract boolean     isCallerInRole(Identity role);
              public abstract void         setRollbackOnly();
          }

              getCallerIdentity() - used to get java.security.Identity object (identity of the client)
                 (the better way to control access is using ACLs on the EJB container)
              getEJBHome() - for a bean to get a reference to its own home interface
              getEnvironment() - returns EJB's environment properties
              getRollbackOnly() - check if transaction is marked as RollbackOnly
              getUserTransaction() - returns object which implements javax.jts.UserTransaction interface
                (this interface has 10 STATUS constants, begin(), commit(), rollback(), getStatus(), etc. methods)
              isCallerInRole(Identity role) - to check the caller's role (for example "user" or "manager")
              setRollbackOnly() - sets current transaction to "rollback only"

 --------------------------------------------------------------
 
   SessionContext interface:

   public interface javax.ejb.SessionContext extends javax.ejb.EJBContext {
      public abstract EJBObject getEJBObject();
   }

How a new Session bean is created:
    - a client calls a create() method in the bean's home interface
    - container instantiates the object
    - containers calls the object's setSessionContext() method
    - container calls the ejbCreate() method whose signature matches the create() method invoked by the client.

Each Session EJB must provide 3 classes:
   - The EJB class itself  - Session bean interface, business methods, ejbCreate() method
   - The home interface - inherit from EJBHome interface, provide method signatures for create() methods
   - The remote interface - Inherit from EJBObject interface, provide method signatures for business methods

Statefull and Stateless Session beans: - later

The SessionSynchronization Interface: - may or may not be implemented. Used to be notified by the container about the state of the transaction. Only for stateful Session bean.

   public interface javax.ejb.SessionSynchronization {
      public abstract void afterBegin();
      public abstract void afterCompletion(boolean committed);
      public abstract void beforeCompletion();
   }
 

 --------------------------------------------------------------
 Entity Bean - implements EntityBean inerface:
 --------------------------------------------------------------
??????????????????????
 
 

 ==============================================================

A High-Level View of an EJB Conversation 41
Finding the Bean 42
Getting Access to a Bean 43
Calling the Bean's Methods 43
Getting Rid of the Bean 43
RMI Clients 43
CORBA Clients 44
Building and Deploying EJBs 45
Writing the EJB 45
Deploying the EJB 46
Connecting to the EJB 46
Roles in EJB 46
Enterprise Bean Provider 46
Deployer 47
Application Assembler 47
EJB Server Provider 47
EJB Container Provider 48
System Administrator 48

 
Chapter 3 home - top of the page -

Chapter 3 Hello, EJB! 49
Requirements 49
Design 50
Implementation 51
Step 1: Create the Remote Interface for the Bean 51
Step 2: Create the Bean's Home Interface 54
Step 3: Create the Bean's Implementation Class 56
Step 4: Compile the Remote Interface, Home Interface, and Implementation Class 59
Step 5: Create a Session Descriptor 59
Step 6: Create a Manifest 62
Step 7: Create an ejb-jar File 63
Step 8: Deploy the ejb-jar File 63
Step 9: Write a Client 64
Step 10: Run the Client 67
What's Really Going on Here? 68
Conclusion 70

 
Chapter 4 home - top of the page -

Chapter 4 Writing EJB Session Beans 71
When to Use Session Beans 71
Constraints on Session Beans 71
Session Bean Life Cycle 73
Transactions and EJB 78
Stateful Session Bean Example 80
Requirements 80
Design 81
Implementation 83
Summing Up the Stateful Session Bean Example 91
Stateless Session Bean Example 91
Requirements 91
Design 92
Implementation 92
Deploying the Example 100
Conclusion 101

 
Chapter 5 home - top of the page -

Chapter 5 Writing EJB Entity Beans 103
When to Use Entity Beans 103
Concurrent Use by Several Clients 104
Long Lifetime 104
Survival of Server Crashes 104
Direct Representation of Data in an Underlying Database 105
Bean-Managed Versus Container-Managed Persistence 105
Primary Keys 106
Entity Bean Life Cycle 106
Nonexistence 107
The Pooled State 108
The Ready State 108
Reentrant Instances 109
Example: Container-Managed Persistence 109
Requirements 109
Design 109
Implementation 110
Example: Bean-Managed Persistence 116
Requirements 116
Design 116
Implementation 117
Conclusion 124

 
Chapter 6 home - top of the page -

Chapter 6 EJB Clients 125
An EJB Bean as a Client to Another Bean 125
The Home Interface 125
The Remote Interface 126
The EJB Client Bean 126
The Client 128
Serializing a Handle 129
The Client 129
Invoking the Client 134
Transactions in Clients 134
Authentication in Clients 137
Getting Metadata 139
A Servlet Client 143
HTML to Make a Call to the Servlet 146
Setting Up WebLogic Servlets 149
An Applet Client 149
The Applet Tag 152
CORBA Client Example 153
What to Look for in a CORBA-Compliant EJB
Implementation 156
HTTP Tunneling and SSL 156
Conclusion 157

 
Chapter 7 home - top of the page -

Chapter 7 Deployment 159
The DeploymentDescriptor Class 159
The AccessControlEntry Class 161
Back to the DeploymentDescriptor Class (I) 162
The ControlDescriptor Class 163
"Run-as" Modes 164
Back to the DeploymentDescriptor Class (II) 165
The SessionDescriptor Class 165
The EntityDescriptor Class 167
Example Program 168
The Home Interface 168
The Remote Interface 168
The Bean Implementation Class 169
The Client 170
Using Roles at Runtime 173
The ReadDD Class 174
The Deployment Descriptor 177
Setting Up Access Control Lists 178
Container-Managed Finder Methods 178
Other Deployment Issues 179
Caching Issues 179
Persistent Storage 180
Properties 180
Other Administrative Issues 180
Conclusion 182

 
Chapter 8 home - top of the page -

Chapter 8 Tips, Tricks, and Traps for Building Distributed and Other Systems 183
Expect Your Network Connections to Fail 183
Test Catastrophic Failure 184
Avoid Remote Method Invocations Where Possible 184
Treat Transactions and Database Connections as Precious Resources 184
Monitor the Granularity of Objects 185
Monitor the Granularity of Methods 185
Isolate Vendor-Specific Code 185
Avoid Making Entity Beans Reentrant 185
Observe Programming Restrictions on EJB Beans 186
Don't Implement the Bean's Remote Interface in Its Implementation Class 186
Use Relatively Small and Well-Defined Queries 187
Don't Keep Database Cursors Open 187
Minimize Transactions 187
Minimize Distributed Transactions 188
Avoid Indexing Your Tables 188
Remember That Memory Is Cheap 188
Build in Upward-Scalability 188
Wrap Entity Beans with Session Beans 189
Streamline Your Middleware Methods 189
Put Your Business Logic in the Middle Tier 189
Understand the Tradeoffs Between Isolation Level and Concurrency 189
Avoid Extensive Object Allocation and Deallocation 190
Prototype, Prototype, Prototype 190
Do Load Testing 190
Monitor the Size of Your User Base When Designing an Architecture 190
Separate Transaction-Processing Tables from Reporting Tables 191
If a Database Query Runs Slowly, Review Its Query Plan 191
Keep Joins Simple 191
Have a Database Administrator 192
Use Prepared Statements 192
Have Your Development Environment Mirror Your Production Environment 192
During Load Testing, Use a Sniffer to Monitor Network Traffic 192
Use the Facade Pattern for Interfacing with Legacy Systems 193
Use Patterns 193
Keep Network Topology in Mind 194
Design Security in from the Start 194
Work Closely with Network Personnel 194
Be Aware of Internal Politics 194
Be Aware of the Organizational Culture 195
Be Prepared for Requirements Changes 195
Build One Slice at a Time 196
Build the Difficult Components First 196
Talk to Your Users 196
Keep It Simple 197
Conduct Walkthroughs 197
Use Version Control 198
Use a Code Profiler 198
Establish Your Interfaces Early 198
Build Early and Often 199
Perform Regression Testing 199
Choose Appropriate Test Cases 200
Generate Test Cases While Implementing the Application 200
Automate Everything 200
Understand the Role of Testing 201

 
Chapter 9 home - top of the page -

Chapter 9 A Nontrival Example 203
Requirements 203
Design 204
Relationships in EJB 204
Relationships in General 205
Detailed Design 205
Database Design 208
Detailed Design of the TimeTracker Class 209
Detailed Design of the Employee Class 211
Designing the TimeSheetHash Class 212
Designing the Client 213
Implementation 213
Building the Database 214
Setting Up the Access Control Lists 214
Implementing the Employee Bean 215
The Final Product 218
Employee Home Interface 218
Employee Remote Interface 219
Employee Implementation 220
Employee Primary Key Class 220
EmployeeInfo Class 221
MyIdentity Class 221
TimeSheetLine Class 222
TimeSheetHash Class 222
The Deployment Descriptor 223
Implementing the TimeTracker Bean 223
The Home Interface 225
The Remote Interface 225
Exceptions 226
Notes About the Implementation Class 227
The Deployment Descriptor 227
Implementing the Real Client 227
The Client Implementation Class 228
An Applet to Run the Client 228
Deployment Issues 229
Conclusion 229

 
Chapter 10 home - top of the page -

Chapter 10 Implementations and Future Directions 231
EJB Implementations 231
WebLogic 231
EJBHome 232
Other EJB Vendors 232
Future Directions for EJB 236
Sun's EJB Roadmap 237
A Bit of Stargazing 238


 
Conclusion and Appendices  home - top of the page -

Conclusion 238
 

Appendix A Source Code for Chapter 4 241
Home Interface for Bag Example 241
Remote Interface for the Bag Example 241
InventoryItem Class 242
ItemNotFoundException Exception 242
BagBean Implementation Class 242
BagBean Client 249
Bag2 Home Interface 251
Bag2 Remote Interface 251
Bag2 Bean Implementation 252
Bag2 Client 258
 

Appendix B Source Code for Chapter 5 261
Implementation of OrderBean (Container-Managed Persistence) 261
Client for Container-Managed Entity Bean 263
Implementation Bean for Bean-Managed Persistence 264
Client for the Bean-Managed Persistence Example 270
 

Appendix C Source Code for Chapter 9 273
Implementation of the Employee Entity Bean 273
Deployment Descriptor for the Employee Entity Bean 283
Implementation Class for the TimeTracker Session Bean 287
Deployment Descriptor for the TimeTracker Session Bean 293
TimeTracker Client Implementation 298

Glossary 305
Index 311
CD-ROM Warranty 325