EJB 3 in Action

Free Download

Authors:

Edition: 1

ISBN: 9781933988344, 1933988347

Size: 15 MB (15289057 bytes)

Pages: 708/708

File format:

Language:

Publishing Year:

Category:

Debu Panda, Reza Rahman, Derek Lane9781933988344, 1933988347

EJB 2 is widely used but it comes at a cost – procedural, redundant code. EJB 3 is a different animal. By adopting a POJO programming model and Java 5 annotations, it dramatically simplifies enterprise development. A cool new feature, its Java Persistence API, creates a standard for object-relational mapping. You can use it for any Java application, whether inside or outside the EJB container. With EJB 3 you will create true object-oriented applications that are easy to write, maintain and extend. EJB 3 in Action is a fast-paced tutorial for both novice and experienced Java developers. It will help you learn EJB 3 and the JPA quickly and easily. This comprehensive, entirely new EJB 3 book starts with a tour of the EJB 3 landscape. It then moves quickly into core topics like building business logic with session and message-driven beans. You’ll find four full chapters on the JPA along with practical code samples, design patterns, performance tuning tips, and best practices for building and deploying scalable applications.

Table of contents :
EJB 3 in Action……Page 1
brief contents……Page 8
contents……Page 10
preface……Page 20
acknowledgments……Page 22
about this book……Page 25
Part 1 – Overview of the EJB landscape……Page 32
What’s what in EJB 3……Page 34
1.1.1 EJB as a component……Page 36
1.1.2 EJB as a framework……Page 38
1.1.3 Layered architectures and EJB……Page 40
1.1.4 Why choose EJB 3?……Page 43
1.2 Understanding EJB types……Page 45
1.2.2 Message-driven beans……Page 47
1.2.3 Entities and the Java Persistence API……Page 48
1.3 Getting inside EJB……Page 50
1.3.2 Accessing JPA services: the persistence provider……Page 51
1.3.3 Gaining functionality with EJB services……Page 52
1.4 Renaissance of EJB……Page 54
1.4.1 HelloUser Example……Page 55
1.4.2 Simplified programming model……Page 56
1.4.3 Annotations instead of deployment descriptors……Page 57
1.4.5 Simplified persistence API……Page 58
1.4.6 Unit-testable POJO components……Page 60
1.4.7 EJB 3 and Spring……Page 61
1.5 Summary……Page 63
A first taste of EJB……Page 64
2.1.1 Replacing deployment descriptors with annotations……Page 66
2.1.2 Introducing dependency injection……Page 71
2.2 Introducing the ActionBazaar application……Page 72
2.2.1 Starting with the architecture……Page 73
2.2.2 An EJB 3-based solution……Page 74
2.3.1 Using stateless beans……Page 76
2.3.2 The stateless bean client……Page 78
2.3.3 Using stateful beans……Page 81
2.3.4 A stateful bean client……Page 85
2.4 Messaging with message-driven beans……Page 87
2.4.1 Producing a billing message……Page 88
2.4.2 Using the order billing message processor MDB……Page 90
2.5 Persisting data with EJB 3 JPA……Page 92
2.5.1 Working with the Java Persistence API……Page 93
2.5.2 Using the EntityManager……Page 96
2.6 Summary……Page 98
Part 2 – Building business logic with EJB 3……Page 100
Building business logic with session beans……Page 102
3.1 Getting to know session beans……Page 103
3.1.1 Why use session beans?……Page 104
3.1.2 Session beans: the basics……Page 106
3.1.3 Understanding the programming rules……Page 109
3.1.5 Bean lifecycle callbacks……Page 110
3.2 Stateless session beans……Page 114
3.2.1 The BidManagerBean example……Page 115
3.2.2 Using the @Stateless annotation……Page 117
3.2.3 Specifying bean business interfaces……Page 118
3.2.4 Using bean lifecycle callbacks……Page 121
3.3 Stateful session beans……Page 124
3.3.2 The BidderAccountCreatorBean example……Page 125
3.3.4 Stateful bean lifecycle callbacks……Page 129
3.4 Session bean clients……Page 133
3.4.2 Injection and stateful session beans……Page 135
3.5.1 Using stateful session beans effectively……Page 136
3.5.2 Stateful session bean alternatives……Page 138
3.6 Session bean best practices……Page 139
3.7 Summary……Page 140
Messaging and developing MDBs……Page 141
4.1 Messaging concepts……Page 142
4.1.2 Messaging in ActionBazaar……Page 143
4.1.3 Messaging models……Page 145
4.2.1 Developing the JMS message producer……Page 148
4.2.2 The JMS message interface……Page 150
4.3 Working with message-driven beans……Page 152
4.3.1 Why use MDBs?……Page 153
4.3.3 Developing a message consumer with MDB……Page 155
4.3.5 Implementing the MessageListener……Page 158
4.3.6 Using ActivationConfigProperty……Page 159
4.3.7 Using bean lifecycle callbacks……Page 163
4.3.8 Sending JMS messages from MDBs……Page 166
4.3.9 Managing MDB transactions……Page 167
4.4 MDB best practices……Page 168
4.5 Summary……Page 170
Learning advanced EJB concepts……Page 171
5.1.1 EJB behind the scenes……Page 172
5.1.2 EJB context: accessing the runtime environment……Page 174
5.2.1 Resource injection using @Resource……Page 177
5.2.2 The @Resource annotation in action……Page 183
5.2.3 Looking up resources and EJBs……Page 186
5.3.1 What is AOP?……Page 188
5.3.2 What are interceptors?……Page 189
5.3.3 Specifying interceptors……Page 191
5.3.4 Implementing business interceptors……Page 193
5.3.5 Lifecycle callback methods in the interceptor class……Page 196
5.4 Scheduling: the EJB 3 timer service……Page 198
5.4.1 What are timers?……Page 199
5.4.2 Using the timer service……Page 200
5.4.3 When to use EJB timers……Page 205
5.5 Summary……Page 206
Transactions and security……Page 207
6.1 Understanding transactions……Page 208
6.1.2 ACID properties……Page 209
6.1.3 Transaction management internals……Page 212
6.1.4 Two-phase commit……Page 214
6.1.5 Transaction management in EJB……Page 215
6.2.1 Snag-It ordering using CMT……Page 216
6.2.2 The @TransactionManagement annotation……Page 217
6.2.3 The @TransactionAttribute annotation……Page 218
6.2.4 Marking a CMT for rollback……Page 222
6.2.5 Transaction and exception handling……Page 224
6.3.1 Snag-It ordering using BMT……Page 227
6.3.2 Getting a UserTransaction……Page 229
6.3.3 Using UserTransaction……Page 230
6.3.4 The pros and cons of BMT……Page 232
6.4.1 Authentication vs. authorization……Page 233
6.4.3 A security problem in ActionBazaar……Page 234
6.4.4 EJB 3 and Java EE security……Page 236
6.4.5 Declarative security……Page 239
6.4.6 Using EJB programmatic security……Page 241
6.5 Summary……Page 244
Part 3 – Diving into the Java Persistence API (JPA)……Page 246
Implementing domain models……Page 248
7.1 Domain modeling and the JPA……Page 249
7.1.2 The ActionBazaar problem domain……Page 250
7.1.3 Domain model actors……Page 253
7.1.4 The EJB 3 Java Persistence API……Page 255
7.1.5 Domain objects as Java classes……Page 256
7.2 Implementing domain objects with JPA……Page 258
7.2.1 The @Entity annotation……Page 259
7.2.2 Persisting entity data……Page 260
7.2.3 Specifying entity identity……Page 264
7.2.4 The @Embeddable annotation……Page 269
7.3 Entity relationships……Page 270
7.3.1 @OneToOne……Page 271
7.3.2 @OneToMany and @ManyToOne……Page 275
7.3.3 @ManyToMany……Page 278
7.4 Summary……Page 280
Object-relational mapping……Page 281
8.1 The impedance mismatch……Page 282
8.1.1 Mapping objects to databases……Page 283
8.1.2 Introducing O/R mapping……Page 288
8.2 Mapping entities……Page 289
8.2.1 Specifying the table……Page 292
8.2.2 Mapping the columns……Page 293
8.2.3 Using @Enumerated……Page 295
8.2.5 Mapping temporal types……Page 296
8.2.6 Mapping an entity to multiple tables……Page 297
8.2.7 Generating primary keys……Page 298
8.2.8 Mapping embeddable classes……Page 301
8.3.1 Mapping one-to-one relationships……Page 304
8.3.2 One-to-many and many-to-one……Page 309
8.3.3 Many-to-many……Page 312
8.4 Mapping inheritance……Page 315
8.4.1 Single-table strategy……Page 316
8.4.2 Joined-tables strategy……Page 318
8.4.3 Table-per-class strategy……Page 319
8.4.4 Mapping polymorphic relationships……Page 322
8.5 Summary……Page 323
Manipulating entities with EntityManager……Page 324
9.1.1 The EntityManager interface……Page 325
9.1.2 The lifecycle of an entity……Page 328
9.1.3 Persistence contexts, scope, and the EntityManager……Page 332
9.1.4 Using the EntityManager in ActionBazaar……Page 334
9.2 Creating EntityManager instances……Page 335
9.2.1 Container-managed EntityManagers……Page 336
9.2.2 Application-managed EntityManager……Page 339
9.3 Managing persistence operations……Page 344
9.3.1 Persisting entities……Page 345
9.3.2 Retrieving entities by primary key……Page 350
9.3.3 Updating entities……Page 356
9.3.4 Deleting entities……Page 359
9.3.5 Controlling updates with flush……Page 361
9.3.6 Refreshing entities……Page 362
9.4 Entity lifecycle listeners……Page 364
9.4.1 Using an entity listener……Page 365
9.4.2 Default listener classes……Page 367
9.4.3 Listener class execution order and exclusion……Page 368
9.6 Summary……Page 369
Using the query API and JPQL to retrieve entities……Page 371
10.1.1 The big picture……Page 372
10.1.2 Anatomy of a query……Page 374
10.1.3 Defining named queries……Page 375
10.2 Executing the queries……Page 376
10.2.1 Creating a query instance……Page 377
10.2.2 Working with the Query interface……Page 378
10.2.3 Specifying query hints……Page 384
10.3 Introducing JPQL……Page 385
10.3.1 Defining statement types……Page 387
10.3.2 Using the FROM clause……Page 389
10.3.3 Conditional expressions and operators……Page 393
10.3.4 Working with JPQL functions……Page 398
10.3.5 Using a SELECT clause……Page 401
10.3.6 Using aggregations……Page 403
10.3.8 Using subqueries……Page 405
10.3.9 Joining entities……Page 407
10.3.10 Bulk updates and deletes……Page 410
10.4 Native SQL queries……Page 411
10.4.1 Using dynamic queries with native SQL……Page 412
10.4.2 Using a named native SQL query……Page 413
10.5 Summary……Page 414
Part 4 – Putting EJB 3 into action……Page 416
Packaging EJB 3 applications……Page 418
11.1 Packaging your applications……Page 420
11.1.1 Dissecting the EAR file……Page 422
11.1.2 Loading the EAR module……Page 423
11.2.1 Class-loading basics……Page 425
11.2.2 Exposing the classic parent delegation model……Page 427
11.2.4 Dependencies between Java EE modules……Page 429
11.3.1 Packaging EJB-JAR……Page 431
11.3.2 Deployment descriptors vs. annotations……Page 433
11.3.3 Overriding annotations with deployment descriptors……Page 437
11.3.4 Specifying default interceptor settings……Page 438
11.3.5 Using vendor-specific annotations and descriptors……Page 439
11.4.1 Exposing the persistence module……Page 441
11.4.2 Describing the persistence module with persistence.xml……Page 442
11.4.3 Performing O/R mapping with orm.xml……Page 449
11.5.1 Packaging and deployment best practices……Page 453
11.5.2 Troubleshooting common deployment problems……Page 454
11.6 Summary……Page 455
Effectively integrating EJB 3 across your application tiers……Page 456
12.1 Design patterns and web technologies……Page 457
12.1.1 Presentation tier……Page 458
12.1.2 Using the Entity Access Object pattern……Page 460
12.1.3 Visiting the Session Façade pattern……Page 465
12.2 Accessing session beans from the web tier……Page 470
12.2.1 Accessing session beans using dependency injection……Page 472
12.2.2 Referencing session beans from helper classes……Page 473
12.2.3 Dealing with transactions……Page 476
12.2.4 Working with stateful session beans……Page 477
12.3 Using JPA from the web tier……Page 478
12.3.1 Using a container-managed entity manager……Page 479
12.3.2 Using an application-managed EntityManager with JTA transactions……Page 481
12.3.3 Accessing an application-managed EntityManager outside the container……Page 482
12.4 Summary……Page 485
Taming wild EJBs: performance and scalability……Page 486
13.1 Handling entity locking issues……Page 488
13.1.1 Understanding locking types……Page 489
13.1.2 Optimistic locking and entity versioning……Page 492
13.1.3 EntityManager and lock modes……Page 493
13.2.1 Remodeling and schema redesign……Page 495
13.2.2 Tuning the JDBC layer……Page 499
13.2.3 Reducing database operations……Page 501
13.2.4 Improving query performance……Page 505
13.2.5 Caching……Page 509
13.3 Improving performance of EJB 3 components……Page 514
13.3.1 Session bean performance……Page 515
13.3.2 Improving MDB performance……Page 518
13.4 Clustering EJB applications……Page 519
13.4.1 Collocated architecture……Page 520
13.4.2 Load-balancing stateless session beans……Page 521
13.4.3 Clustering stateful session beans……Page 524
13.4.4 Entities and clustered cache……Page 526
13.5 Summary……Page 527
Part 5 – Migration and interoperability issues……Page 528
Migrating to EJB 3……Page 530
14.1 Backward compatibility and interoperability with EJB 2……Page 531
14.1.1 Packaging EJB 2 and EJB 3 together……Page 532
14.1.2 Invoking EJB 2 from EJB 3……Page 533
14.1.3 Using EJB 3 from EJB 2……Page 534
14.2 Migrating session beans……Page 536
14.2.1 Converting interface and bean classes……Page 537
14.2.2 Resource usage……Page 541
14.2.4 Client applications……Page 542
14.4 Migrating CMP 2 entity beans to the EJB 3 JPA……Page 544
14.4.1 Redesign your domain model……Page 545
14.4.2 Using DTOs as entities……Page 548
14.4.3 Entity bean classes and interfaces……Page 550
14.4.4 Client applications……Page 557
14.5.1 Identifying entities……Page 560
14.5.2 Reworking a DAO implementation class to use the EntityManager API……Page 561
14.5.3 Swapping SQL queries for JPQL……Page 562
14.6 Helping O/R frameworks to use the EJB 3 JPA……Page 564
14.7.1 Strategies……Page 566
14.8 Summary……Page 567
Exposing EJBs as web services……Page 568
15.1 What is a web service?……Page 570
15.1.1 Identifying web service components……Page 572
15.1.2 Web services styles……Page 578
15.1.3 Approaches to developing web services……Page 579
15.2.1 Introducing the web services platform……Page 580
15.2.2 Why choose EJB over a POJO for a web service?……Page 581
15.3 Developing EJB web services with JAX-WS 2.0……Page 582
15.3.1 Using the @WebService annotation……Page 584
15.3.2 Specifying the web service style with @SOAPBinding……Page 586
15.3.3 Using @WebMethod……Page 588
15.3.4 Using the @WebParam annotation……Page 589
15.3.5 Using the @WebResult annotation……Page 591
15.3.6 Using @OneWay and @HandlerChain……Page 592
15.4.1 Accessing the PlaceBid web service……Page 593
15.4.2 EJB as a web service client……Page 595
15.5 Best practices for web service development……Page 596
15.6 Summary……Page 598
EJB 3 and Spring……Page 599
16.1 Introducing the Spring framework……Page 601
16.1.2 The inversion of control principle……Page 602
16.1.3 The separation of concerns principle……Page 603
16.2 Using JPA with Spring……Page 604
16.2.1 Building JPA EAOs for Spring……Page 605
16.2.2 Configuring Spring to use the JPA……Page 608
16.3.1 Developing Spring-enabled EJBs……Page 612
16.3.2 Using session beans from Spring beans……Page 614
16.4 Summary……Page 616
RMI and JNDI……Page 618
A.1 Distributed communication with RMI……Page 619
A.2 JNDI as a component registry……Page 622
Reviewing relational databases……Page 627
B.1 Database tables, columns, rows, and schema……Page 628
B.2.2 Foreign key……Page 630
B.2.3 NOT NULL……Page 631
B.3 Structured Query Language (SQL)……Page 632
Annotations reference……Page 634
C.1.1 Session beans……Page 635
C.1.3 Dependency injection……Page 637
C.1.4 Transaction management……Page 638
C.1.5 Security management……Page 639
C.1.6 EJB lifecycle management……Page 640
C.1.8 Timers……Page 641
C.2.1 Defining domain objects……Page 642
C.2.2 Defining domain object data……Page 643
C.2.3 Mapping entity data……Page 644
C.2.4 Defining domain relationships……Page 646
C.2.5 Mapping domain relationships……Page 647
C.2.6 Mapping object-oriented inheritance……Page 649
C.2.7 Java Persistence Query Language annotations……Page 650
C.2.8 Entity lifecycle annotations……Page 651
C.2.9 JPA dependency injection……Page 653
Deployment descriptors reference……Page 654
D.1.1 enterprise-beans……Page 655
D.1.2 assembly-descriptor……Page 661
D.2 persistence.xml reference……Page 663
D.3 orm.xml (O/R mapping metadata)reference……Page 665
Installing and configuring the Java EE 5 SDK……Page 677
E.1 Installing the Java EE 5 SDK……Page 678
E.2 Running the application server and database……Page 684
E.3 Installing and running ActionBazaar……Page 687
Online resources……Page 691
Specifications and Sun websites for Java technologies……Page 692
A……Page 694
C……Page 695
E……Page 696
I……Page 698
J……Page 699
K……Page 701
M……Page 702
O……Page 703
P……Page 704
S……Page 705
U……Page 707
X……Page 708

Reviews

There are no reviews yet.

Be the first to review “EJB 3 in Action”
Shopping Cart
Scroll to Top