Lookup of other EJBs ExamplePage added by David BlevinsOverviewThis example shows how to configure JNDI to lookup other EJBs using either the @EJB annotation or the ejb-jar.xml deployment descriptor. There are a couple interesting aspects in this example intended to flush out some of the more confusing, and perhaps frustrating, aspects of referring to EJBs.
This is the most frustrating and hard to accept. Java EE 5 does not have a global namespace and therefore there is no singular name for your EJB. It does not matter what you do to your EJB, there is no standard way to "give" the bean a name that can be used by the application globally.
You read this right. If you have 10 EJBs and all want to refer to bean B, then you must declare bean B as a reference 10 times (once for each of the 10 beans). There is no standard way in Java EE 5 to do this just once for all beans. In Java EE 6 there is a "java:global" namespace, a "java:app" namespace, and a "java:module" namespace where names can be defined with the desired scope. There are two things which make this even more confusing:
As well this example shows some other interesting aspects of referring to EJBs:
To illustrate all of this, we have two simple @Stateless beans, RedBean and BlueBean. Both implement the same business local interface, Friend. Both RedBean and BlueBean define java:comp/env/myFriend differently which is allowed as java:comp is a namespace that is private to each bean and not visible to other beans – so the names do not have to match. The source for this example is the "lookup-of-ejbs" directory located in the openejb-examples.zip available on the download page. The CodeHere we show the code for RedBean and BlueBean and their shared business local interface Friend. @Stateless @EJB(beanInterface = Friend.class, beanName = "BlueBean", name = "myFriend") public class RedBean implements Friend {
public String sayHello() {
return "Red says, Hello!";
}
public String helloFromFriend() {
try {
Friend friend = (Friend) new InitialContext().lookup("java:comp/env/myFriend");
return "My friend " + friend.sayHello();
} catch (NamingException e) {
throw new EJBException(e);
}
}
}
@Stateless @EJB(beanInterface = Friend.class, beanName = "RedBean", name = "myFriend") public class BlueBean implements Friend { public String sayHello() { return "Blue says, Hello!"; } public String helloFromFriend() { try { Friend friend = (Friend) new InitialContext().lookup("java:comp/env/myFriend"); return "My friend " + friend.sayHello(); } catch (NamingException e) { throw new EJBException(e); } } } @Local public interface Friend { public String sayHello(); public String helloFromFriend(); } The key items in the above are the following:
Alternative to annotationsIf there is a desire to not use annotations, the above annotation usage is equivalent to the following ejb-jar.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"> <enterprise-beans> <session> <ejb-name>BlueBean</ejb-name> <business-local>org.superbiz.ejblookup.Friend</business-local> <ejb-class>org.superbiz.ejblookup.BlueBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> <ejb-local-ref> <ejb-ref-name>myFriend</ejb-ref-name> <local>org.superbiz.ejblookup.Friend</local> <ejb-link>RedBean</ejb-link> </ejb-local-ref> </session> <session> <ejb-name>RedBean</ejb-name> <business-local>org.superbiz.ejblookup.Friend</business-local> <ejb-class>org.superbiz.ejblookup.RedBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> <ejb-local-ref> <ejb-ref-name>myFriend</ejb-ref-name> <local>org.superbiz.ejblookup.Friend</local> <ejb-link>BlueBean</ejb-link> </ejb-local-ref> </session> </enterprise-beans> </ejb-jar> Writing a unit test for the exampleWriting an unit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods public class EjbDependencyTest extends TestCase { private InitialContext initialContext; protected void setUp() throws Exception { Properties properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); initialContext = new InitialContext(properties); } public void testRed() throws Exception { Friend red = (Friend) initialContext.lookup("RedBeanLocal"); assertNotNull(red); assertEquals("Red says, Hello!", red.sayHello()); assertEquals("My friend Blue says, Hello!", red.helloFromFriend()); } public void testBlue() throws Exception { Friend blue = (Friend) initialContext.lookup("BlueBeanLocal"); assertNotNull(blue); assertEquals("Blue says, Hello!", blue.sayHello()); assertEquals("My friend Red says, Hello!", blue.helloFromFriend()); } } RunningRunning the example is fairly simple. In the "lookup-of-ejbs" directory of the examples zip, just run:
Which should create output like the following. ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.superbiz.ejblookup.EjbDependencyTest Apache OpenEJB 3.1.5-SNAPSHOT build: 20101129-09:51 http://openejb.apache.org/ INFO - openejb.home = /Users/dblevins/work/openejb-3.1.x/examples/lookup-of-ejbs INFO - openejb.base = /Users/dblevins/work/openejb-3.1.x/examples/lookup-of-ejbs INFO - 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 - Found EjbModule in classpath: /Users/dblevins/work/openejb-3.1.x/examples/lookup-of-ejbs/target/classes INFO - Beginning load: /Users/dblevins/work/openejb-3.1.x/examples/lookup-of-ejbs/target/classes INFO - Configuring enterprise application: classpath.ear INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) INFO - Auto-creating a container for bean BlueBean: Container(type=STATELESS, id=Default Stateless Container) INFO - Enterprise application "classpath.ear" loaded. INFO - Assembling app: classpath.ear INFO - Jndi(name=BlueBeanLocal) --> Ejb(deployment-id=BlueBean) INFO - Jndi(name=RedBeanLocal) --> Ejb(deployment-id=RedBean) INFO - Created Ejb(deployment-id=RedBean, ejb-name=RedBean, container=Default Stateless Container) INFO - Created Ejb(deployment-id=BlueBean, ejb-name=BlueBean, container=Default Stateless Container) INFO - Deployed Application(path=classpath.ear) Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.244 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
Change Notification Preferences
View Online
|
Add Comment
|
