Versions Compared

Key

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

Overview

Wiki Markup
{snippet:url=openejb3/examples/telephone-stateful/README.txt}

The Code

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/apache/openejb/examples/telephone/TelephoneBean.java|lang=java}
business interface
Wiki Markup
{snippet:id=code|url=openejb3/examples/telephone-stateful/src/main/java/org/apache/openejb/examples/telephone/Telephone.java|lang=java}
Tip
titleEJB3 Notes

The bean class uses the annotation @Remote but does not specify a list of interfaces as is normally required. Per EJB3 rules, if the bean implements exactly one business interface it may use @Remote with no other values and that business interface is then implied to be a remote business interface. The same rule applies to identical usage of @Local.

The critical thing to know is that if you add another interface the rules change and require that you specify both interfaces in the @Remote annotation as in @Remote({Telephone.class, SecondInterface.class}).

Embedding

We're going to embed OpenEJB3 into a plain JUnit TestCase as a simple means of demonstrating the remote capabilities. We'll do the embedding in our test setUp method, then will make two test methods:

  • 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/apache/openejb/examples/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/apache/openejb/examples/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.

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

Maven setup

Wiki Markup
{snippet:id=desc|url=openejb3/examples/telephone-stateful/pom.xml}
Wiki Markup
{snippet:id=openejbdep|url=openejb3/examples/telephone-stateful/pom.xml|lang=xml}

Running

Running the example is fairly simple, just run:

...