Accessing EJBs Locally

When 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. Instead of putting the app in the server, put the server in the app!

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?

  • Your application is a server or other middleware
  • You want to write an app that can be both stand alone and distributed
  • To test your EJBs with JUnit and don't want to start/stop servers and other nonsense
  • Imagine the power from being able to use your IDE debugger to step from your Client all the way into your EJB and back with no remote debugging voodoo.

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.

Embedding

The basic process for embedding OpenEJB:
  1. Add the OpenEJB libraries to your classpath
  2. Ensure your EJB modules are discoverable
  3. Use the LocalInitialContextFactory to boot OpenEJB

Important docs

Examples

Unfortunately all the examples are not documented here yet, more examples can be browsed in version control or check it out with subversion (svn co http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples).

title description APIs used
Simple EJB3 @Stateless bean with local and remote business interfaces and unit test.
  • javax.ejb.Remote
  • javax.ejb.Local
  • javax.ejb.Stateless
Simple Stateful Simple EJB3 @Stateful bean with local and remote business interfaces and unit test.
  • javax.ejb.Remote
  • javax.ejb.Stateful
EJB 3.1 Singleton Shows two EJB 3.1 @Singleton beans. One configured to load on startup via @Startup and using Bean-Managed Concurrency and synchronization. Another using Container-Managed Concurrency via @Lock(READ) and @Lock(WRITE).
  • javax.annotation.PostConstruct
  • javax.annotation.PreDestroy
  • javax.ejb.ConcurrencyManagement
  • javax.ejb.Lock
  • javax.ejb.Singleton
  • javax.ejb.Startup
  • javax.ejb.ConcurrencyManagementType.BEAN
  • javax.ejb.LockType.READ
  • javax.ejb.LockType.WRITE
EJB 2.1 Compatibility Shows an EJB 3.0 Stateful bean with Business interfaces and EJB 2.1 interfaces (now called "component" interfaces) using the @LocalHome and @RemoteHome. Four interfaces in total.
  • javax.ejb.CreateException
  • javax.ejb.EJBHome
  • javax.ejb.EJBLocalHome
  • javax.ejb.EJBLocalObject
  • javax.ejb.EJBObject
  • javax.ejb.Init
  • javax.ejb.Local
  • javax.ejb.LocalHome
  • javax.ejb.Remote
  • javax.ejb.RemoteHome
  • javax.ejb.Remove
  • javax.ejb.Stateful
Injection of env-entry Shows how @Resource can be used to inject env-entry values declared in an ejb-jar.xml file
  • javax.annotation.Resource
  • javax.ejb.Local
  • javax.ejb.Remote
  • javax.ejb.Stateful
Injection of env-entry 2 Same as the above example but shows how to use a properties file to declare the injection values and demonstrates how to get injection of more types such as java.util.Date, java.lang.Class, java.net.URI and more. OpenEJB specific feature.
  • javax.annotation.Resource
  • javax.ejb.Stateless
Injection of other EJBs Shows use of @EJB in a Stateless to have another Stateless bean injected into it.
  • javax.ejb.EJB
  • javax.ejb.Local
  • javax.ejb.Remote
  • javax.ejb.Stateless
Injection of DataSource Shows use of @Resource to have a JDBC DataSource injected into a Stateful bean. The Stateful bean does basic INSERT, SELECT and DELETE SQL operations.
  • javax.annotation.PostConstruct
  • javax.annotation.Resource
  • javax.ejb.Stateful
  • javax.sql.DataSource
Injection of EntityManager Shows use of @PersistenceContext to have an EntityManager with an EXTENDED persistence context injected into a @Stateful bean. An EJB 3 @Entity bean is used with the EntityManager to create, persist and merge data to a database.
  • javax.ejb.Stateful
  • javax.persistence.Entity
  • javax.persistence.EntityManager
  • javax.persistence.PersistenceContext
  • javax.persistence.PersistenceContextType.EXTENDED
  • javax.persistence.Query
Testing Transactions Shows use of @PersistenceContext to have an EntityManager with an TRANSACTION persistence context injected into a @Stateful bean using the @TransactionAttribute annotation and a TestCase that runs test code in a JTA Transaction. An EJB 3 @Entity bean is used with the EntityManager to create, persist and merge data to a database.
  • javax.ejb.Stateful
  • javax.ejb.Stateless
  • javax.ejb.TransactionAttribute
  • javax.ejb.TransactionAttributeType.MANDATORY
  • javax.ejb.TransactionAttributeType.REQUIRES_NEW
  • javax.persistence.Entity
  • javax.persistence.EntityManager
  • javax.persistence.PersistenceContext
  • javax.persistence.PersistenceContextType.TRANSACTION
  • javax.persistence.Query
Testing Security Builds upon the Injection of EntityManager Example but adds the use of @RolesAllowed and @PermitAll in the @Stateful bean to restrict who can perform create, persist and remove operations on the EntityManager. Shows a TestCase using the @RunAs annotation to execute and test the bean code as various users.
  • javax.annotation.security.PermitAll
  • javax.annotation.security.RolesAllowed
  • javax.annotation.security.RunAs
  • javax.ejb.EJBAccessException
  • javax.ejb.Stateful
  • javax.ejb.Stateless
  • javax.ejb.TransactionAttribute
  • javax.ejb.TransactionAttributeType.SUPPORTS
  • javax.persistence.Entity
  • javax.persistence.EntityManager
  • javax.persistence.PersistenceContext
  • javax.persistence.PersistenceContextType.EXTENDED
  • javax.persistence.Query
Embedded and Remotable Demonstrates how to use an OpenEJB feature that allows people embedding OpenEJB into their applications to support remote clients in other VMs. This is not required for unit testing.
  • javax.ejb.Remote
  • javax.ejb.Stateful
Helloworld Weblogic Demonstrates OpenEJBs ability to understand and support the WebLogic deployment descriptors so people using that platform in production can still use OpenEJB in their IDE or build to unit test their EJB applications.
  • javax.ejb.CreateException
  • javax.ejb.EJBLocalHome
  • javax.ejb.EJBLocalObject
  • javax.ejb.LocalHome
  • javax.ejb.Stateless
JSF Injection Support Demonstrates OpenEJBs ability to inject EJB's into JSF managed beans.
  • javax.ejb.Stateless
Struts with OpenEJB and Tomcat Demonstrates the usage of Struts within an OpenEJB + Tomcat environment.
  • javax.ejb.Stateless
Applets with OpenEJB Demonstrates how an applet can communicate with a remote stateless session bean. The stateless session bean is deployed in an OpenEJB + Tomcat environment
  • javax.ejb.Stateless

  • No labels