Enterprise JavaBeans 3.1, 6th Edition

Book description

Learn how to code, package, deploy, and test functional Enterprise JavaBeans with the latest edition of this bestselling guide. Written by the developers of JBoss EJB 3.1, this book not only brings you up to speed on each component type and container service in this implementation, it also provides a workbook with several hands-on examples to help you gain immediate experience with these components.

With version 3.1, EJB's server-side component model for building distributed business applications is simpler than ever. But it's still a complex technology that requires study and lots of practice to master. Enterprise JavaBeans 3.1 is the most complete reference on this specification. You'll find a straightforward, no-nonsense explanation of the underlying technology, including Java classes and interfaces, the component model, and the runtime behavior of EJB.

  • Develop your first EJBs with a hands-on walkthrough of EJB 3.1 concepts
  • Learn how to encapsulate business logic with Session Beans and Message-Driven Beans
  • Discover how to handle persistence through Entity Beans, the EntityManager, and the Java Persistence API
  • Understand EJB's container services such as dependency injection, concurrency, and interceptors
  • Integrate EJB with other technologies in the Java Enterprise Edition platform
  • Use examples with either the JBossAS, OpenEJB, or GlassFish v3 EJB Containers

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Author’s Note
    2. Who Should Read This Book
    3. How This Book Is Organized
      1. Part I, Why Enterprise JavaBeans?
      2. Part II, Server-Side Component Models
      3. Part III, EJB and Persistence
      4. Part IV, Container Services
      5. Part V, Examples
    4. Software and Versions
    5. Conventions Used in This Book
    6. Using Code Examples
    7. Safari® Books Online
    8. Comments and Questions
    9. Acknowledgments
  2. I. Why Enterprise JavaBeans?
    1. 1. Introduction
      1. The Problem Domain
        1. Breaking Up Responsibilities
          1. Core concerns
          2. Cross-cutting concerns
          3. Plumbing
        2. Code Smart, Not Hard
          1. Do less
          2. The Container
        3. The Enterprise JavaBeans™ 3.1 Specification
          1. EJB defined
        4. Review
    2. 2. Component Types
      1. Server-Side Component Types
        1. Session Beans
          1. Stateless session beans (SLSBs)
          2. Stateful session beans (SFSBs)
          3. Singleton beans
        2. Message-Driven Beans (MDBs)
      2. Entity Beans
        1. The Java Persistence Model
      3. The Model Isn’t Everything
    3. 3. Container Services
      1. Dependency Injection (DI)
      2. Concurrency
      3. Instance Pooling/Caching
      4. Transactions
      5. Security
      6. Timers
      7. Naming and Object Stores
      8. Interoperability
      9. Lifecycle Callbacks
      10. Interceptors
      11. Platform Integration
      12. Bringing It Together
    4. 4. Developing Your First EJBs
      1. Step 1: Preparation
        1. Definitions
          1. Bean implementation class (session and message-driven beans)
          2. Bean instance (session and message-driven beans)
          3. Client view (session and message-driven beans)
          4. EJB Proxy (session beans)
          5. Local versus remote (session beans)
          6. Business interface (session beans)
          7. Component interface (session beans)
          8. Home interface (session beans)
          9. Endpoint interface (session beans)
          10. Message interface (MDBs)
        2. Naming Conventions
          1. Common business name
        3. Conventions for the Examples
      2. Step 2: Coding the EJB
        1. The Contract
        2. The Bean Implementation Class
        3. Out-of-Container Testing
        4. Integration Testing
          1. Packaging
          2. Deployment into the Container
          3. The client
      3. Summary
  3. II. Server-Side Component Models
    1. 5. The Stateless Session Bean
      1. The XML Deployment Descriptor
      2. SessionContext
        1. EJBContext
      3. The Lifecycle of a Stateless Session Bean
        1. The Does Not Exist State
        2. The Method-Ready Pool
          1. Transitioning to the Method-Ready Pool
          2. Life in the Method-Ready Pool
          3. Transitioning out of the Method-Ready Pool: The death of a stateless bean instance
      4. Example: The EncryptionEJB
        1. The Contract: Business Interfaces
        2. Application Exceptions
        3. Bean Implementation Class
        4. Accessing Environment Properties (Injection and Lookup)
      5. Asynchronous Methods
    2. 6. The Stateful Session Bean
      1. The Lifecycle of a Stateful Session Bean
        1. The Does Not Exist State
        2. The Method-Ready State
          1. Transitioning into the Method-Ready state
          2. Life in the Method-Ready state
          3. Transitioning out of the Method-Ready state
        3. The Passivated State
          1. System exceptions
      2. Example: The FileTransferEJB
        1. The Contract: Business Interfaces
        2. Exceptions
        3. Bean Implementation Class
        4. POJO Testing Outside the Container
        5. Integration Testing
    3. 7. The Singleton Session Bean
      1. Concurrency
        1. Shared Mutable Access
        2. Container-Managed Concurrency
        3. Bean-Managed Concurrency
      2. Lifecycle
        1. Explicit Startup
      3. Example: The RSSCacheEJB
        1. Value Objects
        2. The Contract: Business Interfaces
        3. Bean Implementation Class
    4. 8. Message-Driven Beans
      1. JMS and Message-Driven Beans
        1. JMS as a Resource
        2. JMS Is Asynchronous
        3. JMS Messaging Models
          1. Publish-and-subscribe
          2. Point-to-point
          3. Which messaging model should you use?
        4. Learning More About JMS
      2. JMS-Based Message-Driven Beans
        1. @MessageDriven
          1. @ActivationConfigProperty
          2. Message selector
          3. Acknowledge mode
          4. Subscription durability
          5. MessageDrivenContext
          6. MessageListener interface
          7. Taskflow and integration for B2B: onMessage()
          8. Sending messages from a message-driven bean
      3. The Lifecycle of a Message-Driven Bean
        1. The Does Not Exist State
        2. The Method-Ready Pool
          1. Transitioning to the Method-Ready Pool
          2. Life in the Method-Ready Pool
          3. Transitioning out of the Method-Ready Pool: The death of an MDB instance
      4. Connector-Based Message-Driven Beans
      5. Message Linking
        1. Session Beans Should Not Receive Messages
        2. The JMS APIs
          1. TopicConnectionFactory and Topic
          2. Connection and Session
          3. MessageProducer
          4. Message types
        3. Example: The StatusUpdateEJBs
          1. JMS application client: Message producer
          2. Create a base listener
          3. Further testing
  4. III. EJB and Persistence
    1. 9. Persistence: EntityManager
      1. Entities Are POJOs
      2. Managed Versus Unmanaged Entities
        1. Persistence Context
          1. Transaction-scoped persistence context
          2. Extended persistence context
          3. Detached entities
      3. Packaging a Persistence Unit
        1. The Persistence Unit Class Set
      4. Obtaining an EntityManager
        1. EntityManagerFactory
          1. Getting an EntityManagerFactory in Java EE
        2. Obtaining a Persistence Context
      5. Interacting with an EntityManager
      6. Example: A Persistent Employee Registry
        1. A Transactional Abstraction
        2. Persisting Entities
        3. Finding and Updating Entities
          1. find() and getReference()
          2. merge()
          3. Queries
        4. Removing Entities
        5. refresh()
        6. contains() and clear()
        7. flush() and FlushModeType
        8. Locking
        9. unwrap() and getDelegate()
    2. 10. Mapping Persistent Objects
      1. The Programming Model
        1. The Employee Entity
        2. The Bean Class
        3. XML Mapping File
      2. Basic Relational Mapping
        1. Elementary Schema Mappings
          1. @Table
          2. @Column
      3. Primary Keys
        1. @Id
        2. Table Generators
        3. Sequence Generators
        4. Primary-Key Classes and Composite Keys
          1. @IdClass
          2. @EmbeddedId
      4. Property Mappings
        1. @Transient
        2. @Basic and FetchType
        3. @Lob
        4. @Temporal
        5. @Enumerated
      5. @Embedded Objects
    3. 11. Entity Relationships
      1. The Seven Relationship Types
        1. One-to-One Unidirectional Relationship
          1. Relational database schema
          2. Programming model
          3. Primary-key join columns
          4. Default relationship mapping
        2. One-to-One Bidirectional Relationship
          1. Relational database schema
        3. One-to-Many Unidirectional Relationship
          1. Relational database schema
          2. Programming model
        4. Many-to-One Unidirectional Relationship
          1. Relational database schema
          2. Programming model
        5. One-to-Many Bidirectional Relationship
          1. Relational database schema
          2. Programming model
          3. Usage
        6. Many-to-Many Bidirectional Relationship
          1. Relational database schema
          2. Programming model
        7. Many-to-Many Unidirectional Relationship
          1. Relational database schema
          2. Programming model
      2. Mapping Collection-Based Relationships
        1. Ordered List-Based Relationship
        2. Map-Based Relationship
      3. Detached Entities and FetchType
      4. Cascading
        1. PERSIST
        2. MERGE
        3. REMOVE
        4. REFRESH
        5. ALL
        6. When to Use Cascading
    4. 12. Entity Inheritance
      1. Single Table per Class Hierarchy
        1. Advantages
        2. Disadvantages
      2. Table per Concrete Class
        1. Advantages
        2. Disadvantages
      3. Table per Subclass
        1. Advantages
        2. Disadvantages
      4. Mixing Strategies
      5. Nonentity Base Classes
    5. 13. Queries, the Criteria API, and JPA QL
      1. Query API
        1. Parameters
        2. Date Parameters
        3. Paging Results
        4. Hints
        5. FlushMode
      2. JPA QL
        1. Abstract Schema Names
        2. Simple Queries
        3. Selecting Entity and Relationship Properties
        4. Constructor Expressions
        5. The IN Operator and INNER JOIN
        6. LEFT JOIN
        7. Fetch Joins
        8. Using DISTINCT
        9. The WHERE Clause and Literals
        10. The WHERE Clause and Operator Precedence
        11. The WHERE Clause and Arithmetic Operators
        12. The WHERE Clause and Logical Operators
        13. The WHERE Clause and Comparison Symbols
        14. The WHERE Clause and Equality Semantics
        15. The WHERE Clause and BETWEEN
        16. The WHERE Clause and IN
        17. The WHERE Clause and IS NULL
        18. The WHERE Clause and IS EMPTY
        19. The WHERE Clause and MEMBER OF
        20. The WHERE Clause and LIKE
        21. Functional Expressions
          1. Functional expressions in the WHERE clause
          2. Functions returning dates and times
          3. Aggregate functions in the SELECT clause
            1. COUNT (identifier or path expression)
            2. MAX( path expression), MIN( path expression)
            3. AVG( numeric ), SUM( numeric)
            4. DISTINCT, nulls, and empty arguments
        22. The ORDER BY Clause
        23. Bulk UPDATE and DELETE
      3. Native Queries
        1. Scalar Native Queries
        2. Simple Entity Native Queries
        3. Complex Native Queries
          1. Native queries with multiple entities
      4. Named Queries
        1. Named Native Queries
    6. 14. Entity Callbacks and Listeners
      1. Callback Events
      2. Callbacks on Entity Classes
      3. Entity Listeners
        1. Default Entity Listeners
        2. Inheritance and Listeners
  5. IV. Container Services
    1. 15. Security
      1. Authentication and Identity
      2. Authorization
      3. Example: A Secured School
        1. The Business Interface
        2. Assigning Method Permissions
        3. Programmatic Security
        4. The RunAs Security Identity
    2. 16. JNDI, the ENC, and Injection
      1. Global JNDI
      2. The JNDI ENC
        1. What Can Be Registered in the JNDI ENC?
        2. How Is the JNDI ENC Populated?
          1. XML population
          2. Annotation population
        3. How Are Things Referenced from the ENC?
          1. Using EJBContext
          2. Annotation injection
          3. Default ENC name
          4. XML injection
          5. XML overrides
          6. Injection and inheritance
      3. Reference and Injection Types
        1. EJB References
          1. @javax.ejb.EJB
          2. Ambiguous and overloaded EJB names
          3. Resolving EJB references
        2. EntityManagerFactory References
          1. @javax.persistence.PersistenceUnit
          2. XML-based EntityManagerFactory references
          3. Scoped and overloaded unit names
        3. EntityManager References
          1. @javax.persistence.PersistenceContext
        4. Resource References
          1. @javax.annotation.Resource
          2. Shareable resources
        5. Resource Environment and Administered Objects
        6. Environment Entries
        7. Message Destination References
          1. XML-based resource references
          2. Using @Resource
    3. 17. Transactions
      1. ACID Transactions
        1. Example: The BlackjackEJB
        2. Helper EJBs for Testing Transactions
        3. Is the BlackjackEJB Atomic?
        4. Is the BlackjackEJB Consistent?
        5. Is the BlackjackEJB Isolated?
        6. Is the BlackjackEJB Durable?
      2. Declarative Transaction Management
        1. Transaction Scope
        2. Transaction Attributes
          1. Using the @TransactionAttribute annotation
          2. Transaction attributes defined
          3. EJB 3.0 persistence and transaction attributes
          4. Message-driven beans and transaction attributes
          5. EJB endpoints and transaction attributes
        3. Transaction Propagation
          1. Transactions and persistence context propagation
      3. Isolation and Database Locking
        1. Dirty, Repeatable, and Phantom Reads
          1. Dirty reads
          2. Repeatable reads
          3. Phantom reads
        2. Database Locks
        3. Transaction Isolation Levels
        4. Balancing Performance Against Consistency
          1. Controlling isolation levels
        5. Optimistic Locking
        6. Programmatic Locking
      4. Nontransactional EJBs
      5. Explicit Transaction Management
        1. Transaction Propagation in Bean-Managed Transactions
          1. Message-driven beans and bean-managed transactions
        2. Heuristic Decisions
        3. UserTransaction
        4. Status
        5. EJBContext Rollback Methods
      6. Exceptions and Transactions
        1. Application Exceptions Versus System Exceptions
          1. System exceptions
          2. Application exceptions
      7. Transactional Stateful Session Beans
        1. The Transactional Method-Ready State
          1. Transitioning into the Transactional Method-Ready state
          2. Life in the Transactional Method-Ready state
      8. Conversational Persistence Contexts
    4. 18. Interceptors
      1. Intercepting Methods
        1. Interceptor Class
        2. Applying Interceptors
          1. Annotated methods and classes
          2. Applying interceptors through XML
          3. Default interceptors
          4. Disabling interceptors
      2. Interceptors and Injection
      3. Intercepting Lifecycle Events
        1. Custom Injection Annotations
      4. Exception Handling
        1. Aborting a Method Invocation
        2. Catch and Rethrow Exceptions
      5. Interceptor Lifecycle
      6. Bean Class @AroundInvoke Methods
    5. 19. Timer Service
      1. Example: A Batch Credit Card Processing System
        1. The Business Interface
        2. javax.ejb.ScheduleExpression and @javax.ejb.Schedule
        3. The Bean Implementation Class
        4. The TimerService
        5. The Timer
          1. Canceling timers
          2. Identifying timers
          3. Retrieving other information from timers
          4. The TimerHandle object
          5. Exceptions
      2. Transactions
      3. Stateless Session Bean Timers
      4. Message-Driven Bean Timers
    6. 20. EJB 3.1: Web Services Standards
      1. Web Services Overview
      2. XML Schema and XML Namespaces
        1. XML Schema
        2. XML Namespaces
      3. SOAP 1.1
        1. Web Services Styles
        2. Exchanging SOAP Messages with HTTP
        3. Now You See It, Now You Don’t
      4. WSDL 1.1
        1. The <definitions> Element
        2. The <portType> and <message> Elements
        3. The <types> Element
        4. The <binding> and <service> Elements
      5. UDDI 2.0
      6. From Standards to Implementation
    7. 21. EJB 3.1 and Web Services
      1. Accessing Web Services with JAX-RPC
        1. Generating JAX-RPC Artifacts from WSDL
        2. Calling a Service from an EJB
        3. The <service-ref> Deployment Element
        4. The JAX-RPC Mapping File
      2. Defining a Web Service with JAX-RPC
        1. The WSDL Document
        2. The Service Endpoint Interface
        3. The Stateless Bean Class
        4. The Deployment Files
          1. The WSDL file
          2. The JAX-RPC mapping file
          3. The webservices.xml file
      3. Using JAX-WS
        1. The @WebService Annotation
        2. The @WebMethod Annotation
        3. The @SOAPBinding Annotation
        4. The @WebParam Annotation
        5. The @WebResult Annotation
        6. The @OneWay Annotation
        7. Separating the Web Services Contract
        8. The Service Class
        9. The Service Endpoint Interface
        10. The @WebServiceRef Annotation
      4. Other Annotations and APIs
      5. JAXB
        1. Taking JAXB Further
      6. Conclusion
  6. V. Examples
    1. A. FirstEJB Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. CalculatorBeanBase.java
          2. CalculatorCommonBusiness.java
          3. CalculatorLocal.java
          4. CalculatorLocalBusiness.java
          5. CalculatorLocalHome.java
          6. CalculatorRemote.java
          7. CalculatorRemoteBusiness.java
          8. CalculatorRemoteHome.java
          9. ManyViewCalculatorBean.java
          10. NoInterfaceViewCalculatorBean.java
          11. SimpleCalculatorBean.java
        2. Test Resources
          1. CalculatorAssertionDelegate.java
          2. CalculatorIntegrationTestCase.java
          3. CalculatorUnitTestCase.java
          4. MultiViewCalculatorIntegrationTestCase.java
          5. jndi.properties
    2. B. Stateless Session EJB: Encryption Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. EncryptionBean.java
          2. EncryptionCommonBusiness.java
          3. EncryptionException.java
          4. EncryptionLocalBusiness.java
          5. EncryptionRemoteBusiness.java
          6. META-INF/ejb-jar.xml
        2. Test Resources
          1. EncryptionIntegrationTestCase.java
          2. EncryptionTestCaseSupport.java
          3. EncryptionUnitTestCase.java
    3. C. Stateful Session EJB: FTP Client Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. FileTransferBean.java
          2. EncryptionBean.java
          3. EncryptionCommonBusiness.java
          4. EncryptionException.java
          5. EncryptionLocalBusiness.java
          6. EncryptionRemoteBusiness.java
          7. META-INF/ejb-jar.xml
        2. Test Resources
          1. EncryptionIntegrationTestCase.java
          2. EncryptionTestCaseSupport.java
          3. EncryptionUnitTestCase.java
          4. FileTransferCommonBusiness.java
          5. FileTransferException.java
          6. FileTransferRemoteBusiness.java
        3. Test Resources
          1. FileTransferIntegrationTestCase.java
          2. FileTransferTestCaseBase.java
          3. FileTransferUnitTestCase.java
          4. FtpServerPojo.java
          5. ftpusers.properties
    4. D. Singleton Session EJB: RSS Cache Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. ProtectExportUtil.java
          2. RomeRssEntry.java
          3. RssCacheBean.java
          4. RssCacheCommonBusiness.java
          5. RssEntry.java
        2. Test Resources
          1. RssCacheTestCaseBase.java
          2. RssCacheUnitTestCase.java
          3. TestRssCacheBean.java
          4. jndi.properties
    5. E. Message-Driven EJB: Status Update Listeners Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. StatusUpdate.java
          2. StatusUpdateConstants.java
          3. EnvironmentSpecificTwitterUtil.java
          4. LoggingStatusUpdateMdb.java
          5. SecurityActions.java
          6. StatusUpdateBeanBase.java
          7. TwitterUpdateMdb.java
          8. hornetq-jms.xml
        2. Test Resources
          1. MockObjectMessage.java
          2. StatusUpdateIntegrationTest.java
          3. StatusUpdateTestBase.java
          4. StatusUpdateUnitTestCase.java
          5. TwitterUpdateBlockingTestMdb.java
    6. F. Java Persistence APIs: Employee Registry Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. SimpleEmployee.java
          2. EmbeddedEmployeePK.java
          3. EmployeeType.java
          4. EmployeeWithEmbeddedPK.java
          5. EmployeeWithExternalCompositePK.java
          6. EmployeeWithMappedSuperClassId.java
          7. EmployeeWithProperties.java
          8. ExternalEmployeePK.java
          9. Address.java
          10. Computer.java
          11. Customer.java
          12. Employee.java
          13. Phone.java
          14. PhoneType.java
          15. Task.java
          16. Team.java
          17. Customer.java
          18. Employee.java
          19. Person.java
          20. Customer.java
          21. Employee.java
          22. Person.java
          23. Customer.java
          24. Employee.java
          25. Person.java
          26. EntityListenerEmployee.java
          27. EventTracker.java
          28. persistence.xml
        2. Test Resources
          1. EmployeeIntegrationTest.java
    7. G. Security: Secured School Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. FireDepartmentLocalBusiness.java
          2. SchoolClosedException.java
          3. SecureSchoolLocalBusiness.java
          4. FileDepartmentBean.java
          5. Roles.java
          6. SecureSchoolBean.java
        2. Test Resources
          1. SecureSchoolIntegrationTest.java
          2. groups.properties
          3. users.properties
    8. H. Transactions: Blackjack Game Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. BankLocalBusiness.java
          2. BlackjackGameLocalBusiness.java
          3. InsufficientBalanceException.java
          4. Account.java
          5. User.java
          6. BankBean.java
          7. BlackjackGameBean.java
          8. BlackjackServiceConstants.java
          9. persistence.xml
        2. Test Resources
          1. TransactionalBlackjackGameIntegrationTest.java
          2. DbInitializerBean.java
          3. ExampleUserData.java
    9. I. Interceptors: TV Channel Service Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. AuditedInvocation.java
          2. CachingAuditor.java
          3. Channel2AccessPolicy.java
          4. Channel2ClosedException.java
          5. Channel2Restrictor.java
          6. TunerBean.java
          7. TunerLocalBusiness.java
        2. Test Resources
          1. CachingInterceptorUnitTestCase.java
          2. Channel2RestrictorUnitTestCase.java
          3. InterceptorIntegrationTest.java
          4. MockInvocationContext.java
          5. SecurityActions.java
    10. J. Timer Service: Credit Card Processor Example
      1. Description
      2. Online Companion Information
      3. Source Listing
        1. Implementation Resources
          1. CreditCardTransaction.java
          2. CreditCardTransactionProcessingLocalBusiness.java
          3. CreditCardTransactionProcessingBean.java
  7. Index
  8. About the Authors
  9. Colophon
  10. Copyright

Product information

  • Title: Enterprise JavaBeans 3.1, 6th Edition
  • Author(s): Andrew Lee Rubinger, Bill Burke
  • Release date: September 2010
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596158026