Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

...

This example shows how to use OpenEJB3's remoting capabilities in an embedded scenario. By remoting we mean that you wish to allow clients in other vms access your ejbs. Note, you do not need to go to this extreme to unit test ejbs with remote interfaces.

The basic recipe is the same for a standard embedded scenario but with these added ingreditents:

  • openejb.embedded.remotable property
  • openejb-ejbd jar

While creating the InitialContext, pass in the openejb.embedded.remotable property with the value of "true". When this is seen by the LocalInitialContextFactory, it will boot up the Server ServiceManager in the VM which will in turn look for ServerServices in the classpath.

Provided you have the openejb-ejbd jar in your classpath along with it's dependencies (openejb-server, openejb-client, openejb-core), then those services will be brought online and remote clients will be able to connect into your vm and invoke beans.

If you want to add more ServerServices such as the http version of the ejbd protocol you'd simply add the openejb-httpejbd jar to your classpath. A number of ServerServices are available currently:

  • openejb-ejbd
  • openejb-http
  • openejb-telnet
  • openejb-derbynet
  • openejb-hsql
  • openejb-activemq

To run the example simply type:

$ mvn clean install

...

Note

If your goal is simply to unit test beans with remote interfaces, this is not the right example for you. The LocalInitialContextFactory completely supports remote interfaces and all spec required pass-by-value (serialization) semantics without the need for network sockets. This example shows the use of OpenEJB in an embedded environment where connection outside the
vm is required.

...

For this example we have a simple Stateful bean called TelephoneBean as defined below. As a simple way of demonstrating the state we have to methods: speak and listen. You call speak and pass in some text, then you call listen to get your answer.

bean

Wiki Markup
{snippet:id=code|url=openejb3/examples/telephone-stateful/src/main/java/org/superbiz/telephone/TelephoneBean.java|lang=java}

business interface

Wiki Markup
{snippet:id=code|url=openejb3/examples/telephone-stateful/src/main/java/org/superbiz/telephone/Telephone.java|lang=java}

...

  • one for invoking the bean's remote interface via the LocalInitialContextFactory which goes straight against the embedded container system
  • one for invoking the bean's remote interface via the RemoteInitialContextFactory which connects to a Socket and communicates to the embedded container system over the ejbd protocol.

setUp

Wiki Markup
{snippet:id=setup|url=openejb3/examples/telephone-stateful/src/test/java/org/superbiz/telephone/TelephoneTest.java|lang=java}

LocalInitialContextFactory: making in-vm calls to a remote business interface

Wiki Markup
{snippet:id=localcontext|url=openejb3/examples/telephone-stateful/src/test/java/org/superbiz/telephone/TelephoneTest.java|lang=java}

RemoteInitialContextFactory: making networked calls to a remote business interface

This is the part you would want to do in apps that are running a different VM than the one in which the ejb container is embedded. These "client" VMs need only have the the openejb-client jar in their classpath and connect to OpenEJB via the RemoteInitialContextFactory like any other remote EJB client.

...

Which should create output like the following.

No Format
[OPENEJB:INFO] Scanning for projects...
[OPENEJB:INFO] ----------------------------------------------------------------------------
[OPENEJB:INFO] BuildingT OpenEJBE ::S Examples :: Telephone Stateful Pojo
[OPENEJB:INFO]    task-segment: [OPENEJB:clean, install]
[OPENEJB:INFO] T S
-----------------------------------------------------------------------------
[OPENEJB:INFO] [clean:clean]
[OPENEJB:INFO] Deleting directory /Users/dblevins/work/openejb3/examples/telephone-stateful/target
[OPENEJB:INFO] Deleting directory
Running org.superbiz.telephone.TelephoneTest
Apache OpenEJB 3.0    build: 20080408-04:13
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/classes
[OPENEJB:INFO] Deleting directory - openejb.base = /Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/test-classes
[OPENEJB:INFO] [resources:resources]
[OPENEJB:INFO] Using default encoding to copy filtered resources.
[OPENEJB:INFO] [compiler:compile]
Compiling 2 source files to /Users/dblevins/work/openejb3/examples/telephone-stateful/target/classes
[OPENEJB:INFO] [resources:testResources]
[OPENEJB:INFO] Using default encoding to copy filtered resources.
[OPENEJB:INFO] [compiler:testCompile]
Compiling 1 source file to - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default JDK 1.3 ProxyFactory)
INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/test-classes
[OPENEJB:INFO] [surefire:test]
[OPENEJB:INFO] Surefire report directory - Configuring app: /Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.apache.openejb.examples.telephone.TelephoneTest
log4j:WARN No appenders could be found for logger (OpenEJB).
log4j:WARN Please initialize the log4j system properly.
Apache OpenEJB 3.0-incubating-SNAPSHOT    build: 20061228-01:59
http://incubator.apache.org/openejb
23:04:38,699 INFO  [Startup] Found EjbModule in classpathclasses
INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
INFO - Auto-creating a container for bean TelephoneBean: Container(type=STATEFUL, id=Default Stateful Container)
INFO - Loaded Module: /Users/dblevins/work/openejb-3.0/examples/telephone-stateful/target/classes
INFO - Assembling app: /Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/classes
23:04:39,625 WARN  [Startup] No ejb-jar.xml found assuming annotated beans present
23:04:39,658 WARN  [OPENEJB:OpenEJB] Auto-deploying ejb TelephoneBean: EjbDeploymentINFO - Jndi(name=TelephoneBeanRemote) --> Ejb(deployment-id=TelephoneBean)
INFO - Created Ejb(deployment-id=TelephoneBean, containerejb-id=Default Stateful Container)
23:04:39,659 WARN  [OPENEJB:OpenEJB] Auto-creating a container for bean TelephoneBean: Container(type=STATEFUL, idname=TelephoneBean, container=Default Stateful Container)
23:04:39,678 INFO - [Startup] Loaded Module: Deployed Application(path=/Users/dblevins/work/openejb3openejb-3.0/examples/telephone-stateful/target/classes)
23:04:40,140 INFO  [Startup] OpenEJB ready.
OpenEJB ready.
  ** Starting Services **
  NAME                 IP              PORT  
  ejbd                 0127.0.0.0  1       4201  
  admin thread         0127.0.0.0  1       4200  
-------
Ready!
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.89989 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[OPENEJB:INFO] [jar:jar]
[OPENEJB:INFO] Building jar: /Users/dblevins/work/openejb3/examples/telephone-stateful/target/telephone-stateful-1.0-SNAPSHOT.jar
[OPENEJB:INFO] [install:install]
[OPENEJB:INFO] Installing /Users/dblevins/work/openejb3/examples/telephone-stateful/target/telephone-stateful-1.0-SNAPSHOT.jar to ...
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] BUILD SUCCESSFUL
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] Total time: 11 seconds
[OPENEJB:INFO] Finished at: Thu Dec 28 23:04:41 PST 2006
[OPENEJB:INFO] Final Memory: 19M/254M
[OPENEJB:INFO] ------------------------------------------------------------------------