13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

22.2 EJBs: You D<strong>on</strong>’t Know Beans?495right, the EJB c<strong>on</strong>tainer (server) makes code that uses your interfaces, al<strong>on</strong>gwith its own code, to do the infrastructure work of EJBs.Talking about all these pieces of an EJB can be c<strong>on</strong>fusing, too. Sometimesit is helpful to think of an EJB as a single class; sometimes it’s better to thinkof it as a family of classes that act together pretending to be a single bean thatis distributed across several hosts. This can make it a bit c<strong>on</strong>fusing when talkingabout an EJB—do we mean the family of interacting classes or do we mean thesingle class that provides the applicati<strong>on</strong> functi<strong>on</strong>ality that we want?The names of EJB classes and EJB interfaces (which we will extend andimplement) d<strong>on</strong>’t help much either—they can be c<strong>on</strong>fusing, too. For example,we will extend EJBObject, but not to write an EJB sessi<strong>on</strong> bean; no, we extendSessi<strong>on</strong>Bean for that, but EJBObject is the name for the remote interface.Go figure.A bit of perspective may help here. The names Remote, Local, and Homeare used as modifiers <strong>on</strong> these classes. Local means “<strong>on</strong> the same host as thebean.” But Home and Remote d<strong>on</strong>’t offer much of a clue. The home interfaceis what we get from a lookup; it produces remote objects (objects which implementthe remote interface). A remote object is what our applicati<strong>on</strong> uses as ifit were a Java object doing what we need, even though its applicati<strong>on</strong>-specificactivity will happen <strong>on</strong> a bean somewhere else <strong>on</strong> the network.Let’s look at a very very simple example, to see the pieces in acti<strong>on</strong>.22.2.1 Sessi<strong>on</strong>BeanLet’s write a stateless sessi<strong>on</strong> bean that will compute the time value of m<strong>on</strong>ey.Why that? Well, two reas<strong>on</strong>s. First, we already have an SAM<strong>on</strong>ey class with asave() method for computing some values; and sec<strong>on</strong>d, we need some simple,stateless, but somewhat computati<strong>on</strong>ally intensive task to make for a halfwayreas<strong>on</strong>able example.The real guts of an EJB, the core of the applicati<strong>on</strong> functi<strong>on</strong>ality—in ourexample, the computati<strong>on</strong> of the time value of m<strong>on</strong>ey—is the sessi<strong>on</strong> (or entity)bean. For our sessi<strong>on</strong> bean we begin by implementing the Sessi<strong>on</strong>Beaninterface, which means that we need to define these methods:public void setSessi<strong>on</strong>C<strong>on</strong>text(Sessi<strong>on</strong>C<strong>on</strong>text c<strong>on</strong>text) { }public void ejbCreate() { }public void ejbRemove() { }public void ejbActivate() { }public void ejbPassivate() { }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!