views:

24

answers:

2

Hi all,

I currently have a J2EE project containing an EJB and a WAR. Everything goes fine while I have only one WAR and one EJB module bundled in the final EAR. I now need to develop an other WAR using the same EJB module.

So, I added the EJB module in the library of the new WAR. The problem is when I try to deploy the EAR bundled with the two WAR and the shared EJB, I get the following exception :

Caused by: java.lang.RuntimeException: Error while binding JNDI name com.alex.ejb.MyBeanRemote__3_x_Internal_RemoteBusinessHome__ for EJB : MyBean

As I remove the EJB from the library of the second WAR, everything work again.

Is there something I missed ?

Finally, the real question is "how to share an EJB within multiple web applications ?"

Thanks for help

A: 

You do not need to include the EJB in the new WAR, you can simply look it up using JNDI or by injecting it. In terms of compiling against it, you can create a JAR that simply contains the interface(s) you need to use, and that to the classpath of your new project.

William
A: 

The problem is when I try to deploy the EAR bundled with the two WAR and the shared EJB, I get the following exception (...)

That's because the beans get deployed twice, causing a naming collision. Actually, that's just not how you should package your application when using an EAR packaging.

Finally, the real question is "how to share an EJB within multiple web applications ?"

Don't put your EJB module(s) in the WEB-INF/lib folder of your Web modules, your EAR should have the following structure:

myear.ear
|-- META-INF
|   |-- application.xml
|   `-- sun-application.xml (optional)
|-- myejbmodule1-ejb.jar
|-- ...
|-- myejbmoduleN-ejb.jar
|-- mywebapp1-war.war
`-- mywebapp2-war.war

Resources

Pascal Thivent