Author(s): Kathy Sierra, Bert Bates
Edition: 1
Publisher: OReilly Media
Year: 2003
Language: English
Pages: 695
51si69jkjxl......Page 1
_pdf_927_927730-901969-896963-0596005717-hfjejb-chp-1......Page 2
Welcome to EJB......Page 3
OBJECTIVES......Page 4
What is EJB all about?......Page 5
What does EJB really give me?......Page 6
No more vendor lock-in!......Page 7
There are no Dumb Questions......Page 8
How does it all work?......Page 9
Behind the scenes.........Page 10
Sharpen your pencil......Page 12
Beans come in three flavors......Page 13
There are no Dumb Question......Page 14
Sharpen your pencil......Page 15
Overheard at! THE TIKIBEAN LOUNGE......Page 16
Example: The Advice Guy bean......Page 17
Five things you do to build a bean:......Page 18
EJB Roles and Responsibilities......Page 28
Tutorial:......Page 30
Organize your project directory......Page 31
Compile the two interfaces and the bean class......Page 32
You'll see something like this......Page 33
you'll see something like this......Page 34
There are no Dumb Questions......Page 35
Name and save the new application......Page 36
What you'll see af ter you create and name the application......Page 37
Now let's make the new enterprise bean (the ejb-jar and the DD)......Page 38
Now we're in the really cool New Enterprise Bean Wizard......Page 39
Create the new ejb-jar......Page 40
Add the three class files (including their package directory) to the JAR......Page 41
Confirm that you added ONLY the package directory and the class files......Page 42
Make it a Stateless Session bean......Page 43
Tell it which of the three class files in the JAR is the actual BEAN class......Page 44
Tell it which is the Home interface, and which is the Component interface......Page 45
Verify everything on this screen!......Page 46
You're done, click Finish......Page 47
Meanwhile back on the main deploytool screen.........Page 48
Run your bean through the deploytool verifier......Page 49
Close your eyes and click OK......Page 50
Whew! No failed tests.......Page 51
Time to Deploy......Page 52
Make it Return a Client Jar......Page 53
Give it a name, so clients can look it up......Page 54
Watch the progress bars go up, then celebrate......Page 55
Now you'll see the AdviceApp inside the server......Page 56
Now all we need is a client.........Page 57
Organizing your project directory for the client......Page 58
The Client Code (AdviceClient.java)......Page 59
Run the client!......Page 60
COFFEE CRAM......Page 61
COFFEE CRAM......Page 62
_pdf_927_927729-901969-896963-0596005717-hfjejb-chp-2......Page 63
EJB Architecture......Page 64
objectives Background......Page 65
You remember this picture.........Page 66
Making a remote method call......Page 67
There's a "helper" on the server as well.........Page 68
There are no dumb questions......Page 69
What about and return values arguments?......Page 70
What really gets passed when you pass an object to a remote method?......Page 72
There are no Dumb Questions......Page 75
Passing a Remote object through a remote method call......Page 77
Brain power......Page 78
There are no Dumb Question......Page 80
What must the Remote object and the stub have in common?......Page 82
The client calls business methods on the stub through the Remote business interface......Page 83
Sharpen your pencil......Page 84
How EJB uses RMI......Page 85
The Remote object is not the bean, it's the bean's bodyguard—the EJBObject......Page 86
The Component interface......Page 87
There are no Dumb Questions......Page 89
Who writes the class that really DOES implement the component interface? In other words, who makes the EJBObject class?......Page 91
Who creates what?......Page 92
THE TIKIBEAN LOUNGE......Page 93
There are no Dumb Questions......Page 94
Sharpen your pencil......Page 100
Architectural over view: Session beans......Page 102
Architectural overview: Entity beans......Page 103
Architectural overview : Creating a Stateful Session bean......Page 104
Architectural overview: Creating a Stateless Session bean......Page 105
Who creates the stateless session bean, and when?......Page 106
Stateless session beans are more scalable......Page 107
Sharpen your pencil......Page 108
There are no Dumb Questions......Page 109
Architectural over view: Message-driven beans......Page 110
Exercise: What goes where?......Page 111
Exercise: Organize your beans......Page 112
Exercise Solutions: What goes where?......Page 113
_pdf_927_927728-901969-896963-0596005717-hfjejb-chp-3......Page 115
Exposing Yourself......Page 116
Objectives......Page 117
What the client really wants......Page 118
There are no Dumb Questions......Page 119
How a client uses a session bean: create, use, and remove......Page 120
What's JNDI?......Page 121
Getting the home interface stub......Page 122
There are no Dumb Questions......Page 123
Sharpen your pencil......Page 124
OK, I'll bite. Why can't you just do a plain old cast ?......Page 125
PortableRemoteObject.narrow()......Page 126
Think of narrowing as "Exotic Casting".......Page 127
Now that we (finally) have the home stub, let's use it to get what we REALLY want.........Page 128
There are no Dumb Questions......Page 129
Writing the Remote home interface for a session bean......Page 130
Remote home interface examples for session beans......Page 131
If you're a client, and you want to.........Page 134
Sharpen your pencil......Page 137
But enough about the home... let's talk about the EJB object. The component interface. The thing you REALLY want.......Page 138
Imagine what else you might want to do with your EJB object reference.........Page 140
Online shopping should not be rushed.........Page 143
Thankfully, we've got handles......Page 144
isIdentical ?: how to find out if two stubs refer to the same bean......Page 146
There are no Dumb Questions......Page 148
BRAIN POWER......Page 150
Sharpen your pencil......Page 152
Which methods make sense for the local client interfaces?......Page 153
Who needs EJBMetaData when you've got reflection?......Page 154
Do you need isIdentical() when there's equals() ?......Page 155
Why so many remove methods?......Page 156
There are no Dumb Questions......Page 158
Comparing Remote vs. Local interfaces......Page 159
Sharpen your pencil......Page 160
Writing the local client interfaces......Page 161
You can have both a Remote and local client view for a bean, but you probabl won't.......Page 162
Sharpen your pencil......Page 163
Exceptions in client interfaces: what the client might get......Page 164
Local client code......Page 165
BRAIN POWER: What has to change inside the bean class?......Page 166
Arguments to Remote vs. local methods......Page 168
There are no Dumb Questions......Page 169
Exercise: BE the Container......Page 170
Exercise Solutions: BE the Container......Page 172
COFFEE CRAM......Page 173
COFFEE CRAM......Page 175
_pdf_927_927731-901969-896963-0596005717-hfjejb-chp-4......Page 177
Being a Session Bean......Page 178
OBJECTIVES: Session Beans......Page 179
Exploring the session bean lifecycle......Page 181
You remember how it all works, right ? Getting and using a stateful session bean......Page 183
Stateful Session Bean, CREATION......Page 184
There's obviously more to the bean's lifecycle than just creation and business methods.........Page 185
Container Callbacks, for the special moments in a bean's life.........Page 186
Container Callbacks come from TWO places:......Page 187
Implementing the container callbacks......Page 188
Sharpen your pencil......Page 191
Sharpen your pencil......Page 192
Bean creation: when an object becomes a bean......Page 193
Bean things you can do during creation: stateful session bean)......Page 201
Bean Use: what happens AFTER creation.........Page 203
Bean things you can do within business methods:......Page 204
Passivation: a stateful bean's chance at scalability.........Page 205
There are no Dumb Questions......Page 207
Your job for passivation: make your state passivatable!......Page 208
Sharpen your pencil......Page 210
Bean things you can do in ejbActivate and ejbPassivate()......Page 212
Bean Removal: when beans die......Page 213
Complaints about bean removal......Page 218
Bean things you can do in ejbRemove()......Page 221
There are no Dumb Questions......Page 222
AdviceStatefulBean code......Page 223
AdviceStatefulBean CLIENT code......Page 224
Sharpen your pencil......Page 225
Deploying a stateful bean......Page 226
Compared to stateful beans, stateless beans have a simple life......Page 227
Bean things you can do from stateless bean methods......Page 233
Writing a Session Bean:your job as Bean Provider......Page 235
Sharpen your pencil......Page 236
Sharpen your pencil: Who does What?......Page 244
SessionContext : You need it more than it needs you......Page 245
Overheard at THE TiKiBEAN LoUNGE......Page 247
What's allowed?......Page 248
COFFEE CRAM: Mock Exam......Page 249
COFFEE CRAM: Mock Exam Answers......Page 256
_pdf_927_927734-901969-896963-0596005717-hfjejb-chp-5......Page 263
Entities Are Persistent......Page 264
OBJECTIVES: Entity Beans......Page 265
What's an entity bean?......Page 266
Entities vs. Entity Beans......Page 267
There are no Dumb Questions......Page 268
There are no Dumb Questions......Page 269
Entity beans from the client's point of view......Page 270
Entity bean overview......Page 271
Entity bean overview......Page 272
A very simple Customer entity bean......Page 273
An entity bean's client view......Page 274
Entity bean Remote component interface......Page 275
Entity bean Remote component interface......Page 276
Rules for the Remote component interface......Page 277
Entity bean Remote home interface......Page 278
What does the client really want from an entity bean home?......Page 279
Entity bean Remote component interface......Page 280
When finders have a dark side.........Page 281
There are no Dumb Questions......Page 282
Home business methods to the rescue......Page 283
Rules for the Remote home interface......Page 284
Sharpen your pencil......Page 285
Session bean create() vs. entity bean create()......Page 286
Session bean remove() vs. entity bean remove()......Page 287
Entity/bean/instance death......Page 290
Client calls remove()......Page 291
Entity bean client view......Page 292
COFFEE CRAM......Page 293
COFFEE CRAM......Page 296
_pdf_927_927741-901969-896963-0596005717-hfjejb-chp-6......Page 299
Being an Entity Bean......Page 300
OBJECTIVES......Page 301
The real power of entity beans is synchronization......Page 303
Oh no!: The entity bean and the entity it represents have different data!......Page 304
How the entity bean and the underlying entity stay synchronized......Page 306
The only question is WHO does the work when it's time to synchronize......Page 307
Container-managed vs. bean-managed persistence......Page 308
there are no Dumb Questions......Page 309
Off the path: A brief history on the evolution of CMP 2.0......Page 310
The EntityBean interface adds three new container callbacks (including two just for synchronization)......Page 315
Even the methods that are the same, don't behave the same......Page 316
But wait... there's more! Entity beans have new home container callbacks , too......Page 317
Writing a CMP entity bean: make it abstract......Page 318
You put three kinds of things in your bean class:......Page 319
PLUS... (ok, that's four things...)......Page 320
Virtual fields are NOT instance variables!......Page 321
Sharpen your penil......Page 322
Complete code for the CustomerBeanCMP class......Page 323
Sharpen your pencil......Page 324
Entity bean instance lifecycle......Page 325
Entity bean instance transitions......Page 326
there are no Dumb Questions......Page 327
Sharpen your pencil......Page 328
Sharpen your pencil......Page 329
So how DID the client get a reference to the EJB object for #28?......Page 330
Bean things you can do during entity construction:......Page 332
CMP Entity bean creation......Page 333
Creating a new entity bean: CMP entity bean......Page 334
CMP Entity bean creation......Page 335
Implement your ejbCreate() methods......Page 336
Object identity: the primary key......Page 337
Implement your ejbPostCreate() methods......Page 339
BRAIN POWER......Page 340
Bean things you can do during entity creation:......Page 343
CMP Entity finder......Page 344
Finding an existing entity......Page 345
CMP Entity bean finders......Page 346
YOU don't implement the Finder methods!......Page 347
CMP Home Business Methods......Page 350
Client invokes a Home Business Method......Page 351
Bean things you can do in home business methods......Page 352
Starting a business method (in a transaction)......Page 353
Completing a business method (and a transaction)......Page 354
Being John Entity Bean......Page 355
Activation/passivation of a CMP entity bean......Page 357
Sharpen your pencil......Page 358
Bean things you can do during activation and loading......Page 359
Bean things you can do during passivation and storing......Page 360
Commit options: what REALLY happens to a bean after a transaction commits?......Page 361
There are no Dumb Questions......Page 362
Exercise: CMP entity bean Creation......Page 364
Exercise: Entity Bean Business Method (with activation & passivation)......Page 365
Exercise......Page 366
COFFEE CRAM......Page 367
COFFEE CRAM......Page 372
_pdf_927_927737-901969-896963-0596005717-hfjejb-chp-7......Page 377
When Beans Relate......Page 380
OBJECTIVES: Entity Relationships: CMP and CMR......Page 381
Beanifying your movie database......Page 382
But we don't want to think in TABLES We want to think in CLASSES......Page 383
We need relationships between the Movie bean and the Director......Page 385
Why should the Director be a bean? Why can't it just be data?......Page 386
Relationships and multiplicity......Page 387
Multiplicity in Bean Classes......Page 388
Multiplicity affects return type !......Page 389
You need a pair of abstract getters and setters for each CMP field (column values) and each CMR field (relationship with another entity).......Page 390
The Container needs these TWO things to know you have a CMP field "title"......Page 391
there are no Dumb Questions......Page 392
Using relationships in your code......Page 393
Defining relationships in your abstract persistence schema (in the DD)......Page 394
Relationship definition for the Director-to-Movie relationship......Page 395
Director-to-Movie relationship......Page 397
Relationships can be one-way ( unidirectional)......Page 398
The dark side of a unidirectional (one-way) relationship......Page 399
Movie-to-Trailer relationship......Page 400
The abstract schema> element is in the element and the is in the MovieBean with a CMR code field......Page 403
The MovieBean's home interface......Page 405
Mapping from abstract schema to a real database......Page 407
Writing your portable queries, for select and finder methods......Page 408
EJB-QL for the MovieBean......Page 409
Using an optional WHERE clause......Page 410
Navigating to another related bean......Page 411
Selecting a value rather than the whole bean......Page 412
EJB-QL SELECT......Page 413
Sharpen your pencil......Page 414
What does it MEAN to return an abstract schema type?......Page 415
SELECT and FROM are mandatory !......Page 416
Identifiers......Page 417
The WHERE clause......Page 418
The WHERE clause......Page 419
The problem with using Collection types......Page 420
Collections don't bark() !......Page 421
The IN operator lets you say "For an individual element IN the Collection..."......Page 422
The BETWEEN expression......Page 423
The other "IN"......Page 424
The IS NOT EMPTY comparison expression......Page 425
The LIKE expression......Page 426
The NOT LIKE expression......Page 427
Relationship assignments......Page 429
Exercise: Multiplicity and Assignments......Page 430
Solution: Multiplicity and Assignments......Page 431
COFFEE CRAM......Page 432
COFFEE CRAM......Page 438
_pdf_927_927736-901969-896963-0596005717-hfjejb-chp-8......Page 444
Getting the Message......Page 445
OBJECTIVES......Page 446
Imagine this scenario.........Page 447
Too bad these guys aren't message-driven beans......Page 449
Message-driven bean class......Page 454
Sharpen your pencil......Page 455
Complete DD for a message-driven bean......Page 457
Topics and Queues......Page 458
There are no: Dumb Questions......Page 460
Only ONE bean per pool gets a copy of a topic message......Page 461
With a queue, only one bean gets the message. Period.......Page 462
MessageDrivenContext......Page 463
MessageDrivenContext......Page 464
message acknowledgement......Page 466
That's all well and good, but let's go back and see how our earlier scenario ended.........Page 467
Off the path: Think about it......Page 468
COFFEE CRAM......Page 471
COFFEE CRAM......Page 474
_pdf_927_927735-901969-896963-0596005717-hfjejb-chp-9......Page 477
The Atomic Age......Page 478
OBJECTIVES......Page 479
An EJB transaction is an atomic unit of work.......Page 480
The ACID test......Page 481
Distributed transactions: two-phase commit......Page 482
How it works in EJB......Page 483
Some transactions don't propagate......Page 484
How do I make (or get) a transaction?......Page 485
BRAIN POWER......Page 486
Making a BMT transaction......Page 487
Call stack of the checkOut() method......Page 488
Sharpen your pencil......Page 489
Things you must NOT do with BMT......Page 490
BMT transactions are one way: they can propagate out to a CMT bean, but no other transaction can propagate in to a BMT bean......Page 491
What does it mean to suspend a transaction?......Page 492
The UserTransaction interface......Page 493
setRollbackOnly( ): The sound of a transaction's death......Page 494
setRollbackOnly() lives in TWO interfaces......Page 496
getRollbackOnly():......Page 497
BMT beans use getStatus() instead of getRollbackOnly()......Page 498
BMT can be a really BAD idea. BMT hurts bean reuse......Page 499
Container-managed transactions......Page 500
How attributes work......Page 501
Know your attributes......Page 502
Transaction attributes that require a transaction......Page 503
Sharpen your pencil: Know your attributes......Page 505
Sharpen your pencil......Page 506
These are the methods you MUST mark with an attribute (for a CMT bean)......Page 507
"Unspecified Transaction Context"......Page 508
Burn these in......Page 509
Overheard at: THE TiKiBEAN LoUNGE......Page 510
Marking transactions in the DD......Page 511
DD example for CMT......Page 512
More DD examples for CMT......Page 513
Sharpen your pencil: Which methods need transaction attributes?......Page 516
Summary of Bean-managed demarcation......Page 517
Entity beans have ejbLoad( ) to stay synchronized, even if the transaction rolls back.......Page 519
Session Synchronization: because session beans don't have ejbLoad and ejbStore......Page 520
SessionSynchronization "special moments"......Page 521
There are no Dumb Questions......Page 522
Pool Puzzle......Page 523
Pool Puzzle Solution......Page 524
COFFEE CRAM......Page 525
COFFEE CRAM......Page 529
_pdf_927_927738-901969-896963-0596005717-hfjejb-chp-10......Page 533
When beans go bad......Page 534
OBJECTIVES......Page 535
What can go wrong?......Page 536
Remember, Java exceptions can be checked or unchecked......Page 537
It's all about expectations.........Page 538
Exception pathways......Page 539
In EJB, exceptions come in two flavors: application and system......Page 541
With an Application Exception, the Container will.........Page 542
With a System Exception, the Container will.........Page 543
BRAIN POWER......Page 545
Warning! RemoteException is checked, but not expected!......Page 546
A local entity bean home interface declares only application exceptions......Page 547
RemoteException goes to remote clients EJBException goes to local clients......Page 548
there are no Dumb Questions......Page 549
Bean Provider's responsibilities......Page 550
Bean Provider's responsibilities......Page 551
Bean Provider's responsibilities......Page 552
Bean Provider's responsibilities......Page 553
there are no Dumb Questions......Page 554
The Container's responsibilities......Page 555
Sharpen your pencil......Page 556
The five standard EJB application exceptions......Page 557
The five standard application exceptions from the client's point of view......Page 558
there are no Dumb Questions......Page 559
Common system exceptions......Page 560
Common sysstem exceptions......Page 561
Scenarios: what do you think happens?......Page 565
Sharpen your pencil......Page 566
Scenario Summary......Page 567
Scenario Summary......Page 568
Scenario Summary......Page 569
Exercise......Page 570
Exercise......Page 571
COFFEE CRAM......Page 572
COFFEE CRAM......Page 575
_pdf_927_927739-901969-896963-0596005717-hfjejb-chp-11......Page 578
Protect Your Secrets......Page 579
OBJECTIVES......Page 580
Imagine you're writing a payroll application.........Page 581
What can you do?......Page 582
How to do security in EJB......Page 583
The Application Assembler's job: access control......Page 584
Sharpen your pencil......Page 585
Defining the roles......Page 586
Defining the roles... a better way......Page 587
Defining the method permissions......Page 588
Defining the method permissions......Page 589
Method permissions interact with one another as a union !......Page 590
Watch out for ......Page 591
The Deployer's job: mapping actual humans to abstract roles......Page 593
Principals and Roles, Users and Groups......Page 594
Class-level vs. instance-level security......Page 596
Using programmatic security to custom-tailor a method......Page 597
The problem with isCallerInRole().........Page 598
Map declarative security roles to the programmer's hard-coded (fake) roles......Page 599
The complete security mapping picture......Page 600
Use security identity to pretend someone else is calling.........Page 601
Security context propagation with ......Page 602
COFFEE CRAM......Page 603
COFFEE CRAM......Page 606
_pdf_927_927740-901969-896963-0596005717-hfjejb-chp-12......Page 609
The Joy of Deployment......Page 610
OBJECTIVES......Page 611
A bean's special place- java:comp/env......Page 613
But it's not per bean instance... It's per bean home......Page 614
Environment subcontexts......Page 615
brain power......Page 616
It's simple.........Page 617
Environment entries: deploy-time constants......Page 618
there are no Dumb Questions......Page 619
It's subcontexts all the way down......Page 620
Resource manager connection factories (think: database)......Page 621
The complete resource mapping picture......Page 623
EJB references (when a bean wants another bean)......Page 624
there are no Dumb Questions......Page 625
Using with EJB references......Page 626
Resource environment references (think: JMS destinations)......Page 627
Bean Provider and Application Assembler......Page 628
Deployer responsibility for the Deployment Descriptor......Page 629
Remembering who does what......Page 630
Now let's look at the bean's runtime environment......Page 631
Sharpen your pencil......Page 632
What MUST be in an ejb-jar?......Page 635
Programming restrictions......Page 636
ExeRciSe: Memorize THIS......Page 637
COFFEE CRAM......Page 638
COFFEE CRAM......Page 643
_pdf_927_927742-901969-896963-0596005717-hfjejb-app-a......Page 648
COFFEE CRAM......Page 649
COFFEE CRAM......Page 650
COFFEE CRAM: Master Mock Exam Answers......Page 672
This isn't goodbye......Page 694
Cover Rough Drafts for the series......Page 695