开发者

shared ejb 3 for multiple web project with glassfish

开发者 https://www.devze.com 2023-01-22 18:16 出处:网络
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 sa

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开发者_JS百科, 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


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

  • Java EE 6 Tutorial
    • Packaging Applications
  • EJB3 in Action
    • Packaging EJB 3 Applications


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号