Looking up an EJB from a Web Service under JBoss 4.x
EJB injection in Web Services does not work with JBoss (yet), so when you want to use an EJB from your @WebService annotated POJO you have no choice but to look it up yourself.
This can get a little tricky, because each J2EE container can use its own JNDI naming convention when registering the EJB in the global naming context. Here I document how I configured JBoss to register my EJB with a name that I choose, and how I mapped a proper EJB name to that JNDI name.
Naming the EJB
Give your EJB a reasonably unique name in its annotation:
@Stateless(name="MyServiceBean")
@Local(SomeInterface.class)
public class ServiceBean implements SomeInterface {
...
}
Tell JBoss what JNDI name to register it under
Include the following in your jboss.xml file:
<jboss>
<enterprise-beans>
<session>
<ejb-name>MyServiceBean</ejb-name>
<local-jndi-name>myapp/MyJNDIName</local-jndi-name>
</session>
</enterprise-beans>
</jboss>
Tell JBoss to map an EJB name to the JNDI name
In your WAR, configure your jboss-web.xml file to include this:
<jboss-web>
<ejb-local-ref>
<ejb-ref-name>ejb/MyEJBName</ejb-ref-name>
<local-jndi-name>myapp/MyJNDIName</local-jndi-name>
</ejb-local-ref>
</jboss-web>
Lookup the EJB from your Web Service
You can now lookup the EJB directly from your Web Service:
@WebService
public class MyWebService {
private SomeInterface getMyBean() {
try {
return (SomeInterface) new InitialContext().lookup("java:comp/env/ejb/MyEJBName");
} catch (NamingException e) {
throw new EJBException(e);
}
}
}
No related posts.