You are previewing EJB 3.1 Cookbook.
O'Reilly logo
EJB 3.1 Cookbook

Book Description

Enterprise JavaBean 3.1 - Build real world EJB solutions with a collection of simple but incredibly effective recipes with this book and eBook

  • Build real world solutions and address many common tasks found in the development of EJB applications

  • Manage transactions and secure your EJB applications

  • Master EJB Web Services

  • Part of Packt's Cookbook series: Comprehensive step-by-step recipes illustrate the use of Java to incorporate EJB 3.1 technologies

In Detail

Enterprise Java Beans enable rapid and simplified development of secure and portable applications based on Java technology.Creating and using EJBs can be challenging and rewarding. Among the challenges are learning the EJB technology itself, learning how to use the development environment you have chosen for EJB development, and the testing of the EJBs.

This EJB 3.1 Cookbook addresses all these challenges and covers new 3.1 features, along with explanations of useful retained features from earlier versions. It brings the reader quickly up to speed on how to use EJB 3.1 techniques through the use of step-by-step examples without the need to use multiple incompatible resources. The coverage is concise and to the point, and is organized to allow you to quickly find and learn those features of interest to you.

The book starts with coverage of EJB clients. The reader can choose the chapters and recipes which best address his or her specific needs. The newer EJB technologies presented include singleton beans which support application wide needs and interceptors to permit processing before and after a target method is invoked. Asynchronous invocation of methods and enhancements to the timer service are also covered.

The EJB 3.1 CookBook is a very straightforward and rewarding source of techniques supporting Java EE applications.

Table of Contents

  1. EJB 3.1 Cookbook
    1. EJB 3.1 Cookbook
    2. Credits
    3. About the Author
    4. About the Reviewers
    5. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why subscribe?
        2. Free access for Packt account holders
        3. Instant updates on new Packt books
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Getting Started With EJBs
      1. Introduction
      2. Creating a simple session EJB
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Accessing a session bean using dependency injection
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Accessing the session bean using JNDI
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Portable JNDI naming syntax
          2. EJBs supporting multiple interfaces
      5. Creating a simple message-driven bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Sending a message to a message-driven bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Accessing an EJB from a web service (JAX-WS)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Accessing an EJB from a web service (JAX-RS)
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      9. Accessing an EJB from an Applet
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Accessing an EJB from JSP
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Calling an EJB from JSF
        1. Getting ready
        2. How to do it...
        3. How it works...
      12. Accessing an EJB from a Java Application using JNDI
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      13. Accessing an EJB from a Java Application using an embeddable container
        1. How to do it...
        2. How it works...
        3. See also
      14. Accessing the EJB container
        1. How to do it...
        2. How it works...
        3. There's more...
    8. 2. Session Beans
      1. Introduction
      2. Creating a stateless session bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Use of Instance variables
          2. Stateless Bean Life Cycle
      3. Creating a stateful session bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Creating a singleton bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Using multiple singleton beans
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      6. Using container managed concurrency
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Using bean managed concurrency
        1. Getting ready
        2. How to do it...
        3. How to do it...
        4. There's more...
        5. See also
      8. Controlling the initialization process
        1. How to do it...
        2. How it works...
        3. See also
      9. Using session beans with more than one business interface
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Understanding parameter behavior and granularity
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Using an asynchronous method to create a background process
        1. How to do it...
        2. How it works...
        3. There's more...
    9. 3. Message-Driven Beans
      1. Introduction
      2. Handling a string-based message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Handling a byte-based message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Handling a stream-based message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Handling a map-based message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Handling an object-based message
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using an MDB in a point-to-point application
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Using MDB in a publish-and-subscribe application
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Specifying which types of message to receive using the message selector
        1. Getting ready
        2. How to do it...
        3. How it works...
      10. Browsing messages in a message queue
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    10. 4. EJB Persistence
      1. Introduction
      2. Creating an entity
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Creating an entity facade
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Using the EntityManager
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      5. Controlling the Object-Relationship Mapping (ORM) process
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using embeddable classes in entities
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using application-managed persistence
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Validating persistent fields and properties
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Validating null fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. Validating string fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Validating temporal fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      12. Validating using regular expressions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      13. Validating Boolean fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      14. Validating Integer fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      15. Using the Validator class
        1. Getting ready
        2. How to do it...
        3. How it works...
    11. 5. Querying Entities using JPQL and the Criteria API
      1. Introduction
      2. Populating the Patient and Medication tables
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Using the Select query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Eliminating duplicate entities
          2. Using the Order By clause
        5. See also
      4. Using the Where clause
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Comparison operators
          2. Between operator
          3. Like operator
          4. IN and IS Operators
        5. See also
      5. Controlling the number of entities returned by a Select query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using the Delete query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Using the Update query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      8. Using parameters in a query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Using a Named query
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Using the Criteria API
        1. Getting ready
        2. How to do it...
        3. How it works...
    12. 6. Transaction Processing
      1. Introduction
      2. Creating the Demonstration classes
        1. Getting ready
        2. How to do it...
        3. How it works...
      3. Handling transactions the easy way
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Using the SessionSynchronization interface with session beans
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Understanding how the TransactionAttributeType affects transactions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Handling transactions manually
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. General transaction restrictions
          2. Using the getStatus Method
        5. See also
      7. Rolling back a transaction
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Handling errors in a transaction
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Using timeouts with transactions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    13. 7. EJB Security
      1. Introduction
      2. Creating the SecurityApplication
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Configuring the server to handle security
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Understanding and declaring roles
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Controlling security using declarations
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Propagating identity
        1. Getting ready
        2. How to do it...
        3. How it works...
      7. Controlling security programmatically
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    14. 8. Interceptors
      1. Introduction
      2. Creating the Registration Application
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Defining and using interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Creating an Interceptor for all EJBs in an EJB module
          2. Creating an interceptor for all methods of a class
          3. Creating an interceptor for a specific method
          4. Declaring an interceptor in the target class
      4. Using the InvocationContext to verify parameters
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using getTarget to return information about the target
          2. Using getMethod to return information about the target's method
      5. Using interceptors to enforce security
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Using interceptors to handle transactions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Using interceptors to handle application statistics
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using the getContextData method
          2. Understanding interceptor chaining
          3. Excluding interceptors
      8. Using lifecycle methods in interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
    15. 9. Timer Services
      1. Introduction
      2. Setting up the ReportsApplication
        1. Getting ready
        2. How to do it...
        3. How it works...
      3. Creating and using declarative timers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using @Schedule with multiple methods
          2. Using @Schedules with a single method
        5. See also
      4. Creating and using programmatic timers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Single event timers
          2. Interval event timers
          3. Calendar event timers
          4. Getting a collection of scheduled timers
      5. Understanding calendar-based scheduling
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using lists in schedule expressions
          2. Using ranges in schedule expressions
          3. Using increments in schedule expressions
        5. See also
      6. Using the timer interface
        1. Getting ready
        2. How to do it....
        3. How it works...
        4. There's more...
          1. Using the TimerConfig object to pass information
          2. Using the Serializable object to pass information
      7. Using persistent and non-persistent timers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      8. Creating timers upon application deployment
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Using interceptors with timers
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    16. 10. Web Services
      1. Introduction
      2. Creating an EJB-based web service using JAX-WS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Variations of the @WebService annotation
          2. Using the @WebMethod annotation
          3. Using the @WebParam annotation
        5. See also
      3. Creating an EJB-based web service using JAX-RS
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Understanding the Application class
          2. Using the GET command with parameters
          3. Using the POST command with form data
      4. Using an MDB as part of a web service
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    17. 11. Packaging the EJB
      1. Introduction
      2. Understanding an application's JAR files using the jar command
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Understanding class loading
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Using deployment descriptors for interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using method-level interceptor descriptors
          2. Excluding interceptors
          3. Controlling the execution order of interceptors
        5. See also
      5. Using deployment descriptors for timer interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Using deployment descriptor for default interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using deployment descriptors for callback interceptors
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Using a deployment descriptors for transactions
        1. Getting ready
        2. How to do it...
        3. How it works...
      9. Using deployment descriptors for security
        1. Getting ready
        2. How to do it...
        3. How it works...
    18. 12. EJB Techniques
      1. Introduction
      2. Exception handling and EJBs
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Logging exceptions to the console
          2. Incomplete exception handling
          3. Exceptions that are ignored
        5. See also
      3. Using logging within an EJB
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Using an interceptor for logging and exception handling
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Creating your own interceptor
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Using time within an EJB
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Inadvertent Use of the Default TimeZone
          2. Thread issues with the DateFormat class
      7. How to support currency
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Understanding the implications of immutable BigDecimal objects
          2. Comparison of BigDecimal numbers
          3. When to perform rounding
      8. Efficient manipulation of strings
        1. Getting ready
        2. How to do it...
        3. How it works...