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.

494Chapter 22Building an EJBclasses, then compile them. Then, in the next chapter, we’ll package them,al<strong>on</strong>g with other supporting informati<strong>on</strong>, into an EAR file. But let’s begin bywriting some Java classes.It’s not that we just write a single EJB class, say a sessi<strong>on</strong> bean, and we’red<strong>on</strong>e. Keep in mind that we’re going to be using these beans in a distributedenvir<strong>on</strong>ment, so we need a way to have an applicati<strong>on</strong> running <strong>on</strong> <strong>on</strong>e systemfind, create, look up, or otherwise access the bean running <strong>on</strong> another machinesomewhere in our enterprise network. The job of EJBs is to simplify (up to apoint) the efforts of the applicati<strong>on</strong> programmer doing all this, and make itseem as if the bean is quite local or at least independent of locati<strong>on</strong>.Here’s how it works. Any applicati<strong>on</strong> that wants to use the functi<strong>on</strong>s providedby an EJB must first locate the bean. It uses a naming service(Chapter 21) for this. What it gets from the lookup is something called a homeinterface. The home interface object is in effect a factory for producing remoteinterfaces, which are the proxies for the actual service(s) that our applicati<strong>on</strong>wants to use. A remote interface has the method signatures that give the functi<strong>on</strong>alitythat the applicati<strong>on</strong> is after, but it doesn’t do the actual work of thebean. Rather, it is a proxy for the bean. The remote interface’s job is to do allthe work behind the scenes to marshal the arguments and send them off to thebean, and to unmarshal the results returned from the bean.So it’s a three step process:1. Do the lookup.2. Use the home interface to produce a remote interface.3. Use the remote interface to call the methods <strong>on</strong> the bean.What’s all this talk about interfaces? They provide a way to define themethods you want to use, but without having to write all the code to do it. Forexample, with the remote interface you may define a method to do somethingwith several arguments, say Blah(a, b, c). Now the remote object doesn’treally do the Blah work; its job is to marshal the arguments (serialized a, b,and c) and send them off to the EJB to do whatever Blah is, and then unmarshalthe results. So you as an applicati<strong>on</strong> programmer will write the guts ofBlah in the EJB object, but for the remote object, its proxy, you <strong>on</strong>ly need todeclare the method signature. Then the job of the EJB c<strong>on</strong>tainer (e.g., JBossor Ger<strong>on</strong>imo) is to provide the smarts of the proxy—that is, to generate a Javaclass that implements your interface, al<strong>on</strong>g with the code that knows how toc<strong>on</strong>tact your EJB and marshal and unmarshal the arguments and results. That’s

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

Saved successfully!

Ooh no, something went wrong!