EJB architecture

Sharpen your pencil
1

Label the three parts in the diagram.

Client
biz interface

B Container services

server

C li ent object

A EJB Object

C

the bean
DB

2

Describe (briefly) what each of the three things are responsible for, or how they behave.

A

The EJB object is the bodyguard for the bean... it intercepts the calls coming from the client and passes the request to the Container. The Container services are the things you’re paying for... the reason you’re using EJB in the first place - transactions, security, persistence, etc. The bean has the real business logic. It has the actual functionality (method code) to do whatever it is this remote service is supposed to do (track your shopping cart, do a big calculate, update a customer’s address, etc.

B

C

10

Chapter 1

m o s S E O ean D A session b Look at the problem description on the left.intro to EJB �� ���������� � Sharpen your pencil Know your bean types. and put a check mark for the bean type that would best fit the problem. or both) stateful stateless Booking a ticket for a rock concert probably stateful because it might have to ask you several questions in order to complete the process stateful stateless A bank account Searching a product database stateful stateless Dating service match-ups stateful stateless Receiving submitted expense reports. Entity Message-driven Session bean (circle stateless.. but another bean will work if you solve the problem in a different way. and sending them out for approval Online expert diagnosis—you describe a symptom and the system helps you determine the cause The books in a library stateful stateless stateful stateless stateful stateless you are here 4 13 . stateful. S I n a e b An entity ething. you might decide that one bean type will work if you approach it one way. There isn’t one perfect right answer for these. something..

for (Th isn’t always true for RMI and stub object to the client initially. or both (you can reuse a class). is we’ll pretend that it is. because it (client won’t even the Dog class has to be on the Ser client and server need the interface inerImpl (the Tra when it gets to the server. The picture is simplified.EJB architecture Sharpen your pencil This sharpen gets you on the one most common mistake EJB developers make. because it’s the serv EJB. Both the only the server needs the actual Dog s. clas compile without the interface). but stub the server). Dog Dog Pe train t(myDog ) Remote DogTrainer client stub 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 client class Dog DogTrainerImpl 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 101101 101101 10101000010 1010 10 0 01010 1 1010101 10101010 1001010101 DogTrainerStub DogTrainer interface DogTrainer interface Dog DogTrainerStub must be deserialized ver as well as the client. The client needs the that delivers the thing that does the remote work on er plain old RMI. obviously. So don’t skip it! Based on this scenario.) 80 Chapter 2 . so you aren’t seeing all of the players involved. but for now but the server usually will too. draw the classes below into the appropriate slot for whether they must be on the client. server.

.EJB architecture Sharpen your pencil EJB Lifecycle: Client has only a Home stub but wants to invoke a business method on a bean. Some arrows are missing. 3. 2. 8. assume the client has previously done a JNDI lookup and gotten back a stub to the Remote Home object. you can add them if you want. 9.. If you get stuck. Relax and take your time. flip back through the previous pages and study the diagrams. Number the arrows (using the boxes over the arrows) in the order in which they occur. but rather arrows pointing to the next THING that happens.. The stub is returned to the client Client calls a business method on the EJB object stub Stub contacts the EJB object with information about the method call (including any args) Container services kick in. Everything in the picture is what happens AFTER the client has the Home stub and now wants to get a reference to an EJBObject and ultimately call a business method on the bean.) Chapter 2 6. or. 7. In the scenario below. 10. Home returns the EJB object stub to the Home stub. 11. just assume some things are happening that you don’t have arrows for. Tell a story for what happens at each arrow.. 4. Method call goes to the bean 96 . 5 client calls create() on the Home stub The stub tells the Home that the client wants to “create” a bean Home object interacts with the Container services (or you can think of the Home AS the Container) EJB object is created for the bean The bean is created (note: steps 4 and 5 can be in a different order depending on the bean type. stub EJB object services 9 10 11 5 bean 8 4 client 7 1 stub 3 Home object 6 2 1. depending on how you tell the story. These arrows aren’t necessarily direct method calls (although they might be). There might be more than one right answer.

Yes No. although it’s easy for the client to re-establish one that’s identical to the one they had before the crash. They’re a lot like stateless session beans. Not applicable. [cue theme song from “Rocky”] Stateless Session Stateful Session Beans Beans Entity Beans Message-driven Beans Uses a pool Multiple clients can have a reference to the same bean Guaranteed to survive a container crash Has a client view Yes. Since they don’t keep any client-specific data. Stateful beans never have a pool. Yes Represents a process Yes Yes No. No. No. message-driven beans don’t have actual *clients* No..bean table Organize your beans Exercise Finish the table by putting in a checkmark (even better if you add notes) in the boxes corresponding to the labels that apply to that bean type. provides this.. Yes Nope.. go back through the previous two chapters. No. You can do it. You *might* Yes! The bean have a vendor that instance itself doesn’t survive. No. We believe in you. Any bean of a particular type is the same as any other. Yes Represents a “thing” in an underlying persistent store (like a database) No. but only one at a time.. When an entity bean is not *being* a specific entity (row from DB). although one bean can service multiple clients. 108 Chapter 2 .. That’s OK— you’ll have it all worked out way before the end of the book.. you don’t need one per each client. No.. Yes. We’ve done one of the boxes for you. You might have to make your best guess on a few things. No. Yes Yes. If you get stuck. it stays in the pool Yes -. entity does. Allows asynchronous communication No. but but the underlying don’t count on it.there can Never! Would you want someone putting be multiple clients their shoes in YOUR accessing the same entity (like Fred shopping cart? #24) No.

rts.the client view Let’s take another look at the complete client code import import import import import javax. make your best guess.rmi javax. but wked .printStackTrace().*.rmi headfirst javax. } } } The point of everything! To call a business method on the bean (via the EJBObject stub) to handle (or ns Not a good way le) exceptio me nd rather. not just a plain old ca ic.go(). } catch (Exception ex) { ex.printStackTrace(). we’ll look A bunch of impoivi dually at the at each one ind pa bottom of the ge public class AdviceClient { public static void main(String[] args) { new AdviceClient(). } catch (CreateException cex) { cex. headfirst. javax. Call create on the home to get us what we REALLY want — the component int erface. AdviceHome.printStackTrace().rmi.class).*.ejb Class Name InitialContext AdviceHome PortableRemoteObject RemoteException Advice CreateException you are here 4 119 . javax.ejb.naming...*. You can use the same package name more than once.naming java. st? AdviceHome home = (AdviceHome) PortableRemoteObject. Advice advisor = home..out. where we the loo ice rv se ng mi na I D JN r” { on the name “Adviso What is THIS??? Why = new InitialContext(). NOTeha so want to show here. java. If you’re not sure..getAdvice()).create().println(advisor. javax. } InitialContext public void go() try { Context ic Object o = into the is our entry point kup do .rmi.*. } catch (RemoteException rex) { rex.*. System.narrow(o. ns io pt ce ex of the chec Sharpen your pencil Package Name Match the class name with the package it’s from.lookup(“Advisor”).

.the Home interface Sharpen your pencil Based on the rules for session bean home interfaces. interfaces. import java. look at The exam expects you to e. ❏ eate() . Cart must be component interface! ❏ Cart must be the interface that extends EJBObject. and cod n bea or e. Theeaob pe (C alr dy knows its ty ed. it can be either stateful or stateless ion ❏ The interface is missing an import statement. you gs thin make inferences about of all w kno ST don’t see.rmi. re. RemoteException. public interface CartHome extends EJBHome { public Cart create() throws CreateException.ejb.EJBHome. cast or narrow is need no The object returned from create() does not need a cast.RemoteException. You MU ent pon com and e hom for the rules . rfac inte client. so ❏ The object returned from create() must be narrowed. } ❏ CartHome must not be the home of a stateful session bean. You MUST be able to look at code and infer a lot about it. ject coming from crart). which statements are true about this interface: import javax.CreateExcept ❏ The create method is missing an exception. javax. And there’s mo 132 Chapter 3 . the Remote ❏ Cart must be the class type of the bean.ejb.

❏ There are two methods in the EJBLocalHome interface you are here 4 155 . this must be a local bean ❏ If you see a getHandle() call. the ❏ If bean’s home interface must extend EJBLocalHome the client is not handling a RemoteException on a business ❏ If method. you do a JNDI lookup on a local home. You’ll have to make some inferences and smart guesses for some of them. but not narrow it. RemoteException is REQUIRED for remote interface methods. the client is catching a RemoteException on a home method. EJBMetaData is just for Remote clients Narrowing is just for Remote stubs. Select all that are true: only way to remove a local session bean is through the ❏ The component interface ❏ Entity beans can be removed through a local home interface ❏ If you see an isIdentical() call... you must narrow the You’ll have to cast it. you see a call to getEJBMetaData(). the bean’s component ❏ If interface must extend EJBLocalObject. decide if the following statements are true or false... ❏ If object returned from JNDI ❏ There are three methods in the EJBLocalObject interface Turn back to page 154 to see the interfaces.the client view Sharpen your pencil Based on what you now know about the difference between local and Remote client interfaces. the bean’s component interface must extend EJBObject. this must be a Remote bean because session beans don’t have a primary key. but you must NOT use them with local interfaces. and the only remove in a local home takes a primary key Handles always means Remote!! They aren’t needed for local clients.

Sign up to vote on this title
UsefulNot useful