|
Page Created :
OPENEJB :
Local Server
Local Server has been created by David Blevins (Sep 02, 2005). Content:Accessing EJBs LocallyWhen OpenEJB embedded in your app, server, IDE, or JUnit, you can use what we call the Local Server and avoid the network overhead and enjoy an easy way to embedd OpenEJB. Say what?! A local server?Yes, you read correctly. OpenEJB can be embedded and treated as your very own personal EJB container. If they can have Local and Remote EJB's, why not Local and Remote EJB Servers too? Haven't you ever wanted EJBs without the heavy? I mean you need the "heavy" eventually, but not while you're developing. Well, there's the advantage of an EJB implementation that was designed with a very clean and well defined server-container contract, you can cut the server part out completely! So, if you wish to access ejbs locally and not in client/server mode, you can do so by embedding OpenEJB as a library and accessing ejbs through OpenEJB's built-in IntraVM (Local) Server. Why would someone want to do this?
In this case, your application, test suite, IDE, or client accesses beans as you would from any other EJB Server. The EJB Server just happens to be running in the same virtual machine as your application. This EJB Server is thusly called the IntraVM Server, and, for all intense purposes, your application an IntraVM Client. There are some interesting differences though. The IntraVM Server isn't a heavyweight server as one normally associates with EJB. It doesn't open connections, launch threads for processing requests, introduce complex classloading heirarchies, or any of those "heavy" kind of things. All it does is dish out proxies to your app that can be used to shoot calls right into the EJB Container. Very light, very fast, very easy for testing, debugging, developing, etc. This excellent series of articles on TheServerSide.com is just one example of the neat things that can be done with an embeddible EJB Container: http://www.theserverside.com/articles/article.tss?l=ContainerDrivenTestingSeries A Basic ClientTry something like this for a simple IntraVM (Local) Client: MyEjbApplication.java import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import FooHome; public class MyEjbApplication { public static void main( String args[]) { try{ Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory"); InitialContext ctx = new InitialContext(properties); Object obj = ctx.lookup("my/bean/Foo"); FooHome ejbHome = (FooHome) PortableRemoteObject.narrow(obj, FooHome.class); } catch (Exception e){ e.printStackTRace(); } } } That would be the simplest spec compliant client you could create. If you don't care about spec compliance and just want to "cheat", you can do this: MyEjbApplication.java import javax.naming.InitialContext; import FooHome; public class MyEjbApplication { public static void main( String args[]) { try{ FooHome ejbHome = (FooHome)new InitialContext().lookup( "java:openejb/ejb/my/bean/Foo"); } catch (Exception e){ e.printStackTRace(); } } } Now keep in mind, that the name "java:openejb/ejb/my/bean/Foo" is not spec compliant. Also keep in mind that we provide it as a convenience, so if there is something you don't like or think should be changed, send code. Embedding OpenEJB DynamicallyRead the Embedding |
Unsubscribe or edit your notifications preferences
