EAR vs separate EJB + WAR

2019-01-13 04:37发布

问题:

What's the difference in deploying application as EAR (with 1 EJB and 1 WAR module) vs separate modules? I want to use GlassFish 3 web profile but it does not support EAR archive. Can I simply use EJB and WAR as separate modules? Any other options?

回答1:

There seems to be some confusion between 3 variants of deployment:

  1. An EAR that includes an EJB and WEB module
  2. Deploying a separate EJB module and a separate WEB module
  3. Deploying a WEB module that includes EJB classes or an EJB jar.

In the first situation, you have logically one application, but one that is divided in two tiers. The WEB module is isolated from the EJB module in the sense that it can consume classes from EJB module, but the EJB module can not consume classes from the WEB module. Since it's a single application local access to EJB beans can be used and injection of EJB beans works as expected.

In the second situation (which you seem to be referring to in your question) there isn't a logical single application, but really two separate modules. They do run in the same JVM, but officially Java EE does not allow to use local access and remote access has to be used (although practically local access often works anyway). Also, injection of EJB beans in beans in the web module does not work directly with a simple @EJB annotation, but instead the lookup attribute has to be used that specifies the global JNDI name.

Finally, the third situation (which you don't seem to mention, but 'home' mentions) is a bit similar to the first one, but there are no tiers and isolation in this case. EJB beans can access all classes from the rest of the web module directly.

The web profile only supports this last deployment situation. Both EAR and standalone EJB deployments are not supported.



回答2:

Whats the difference in deploying application as ear (with 1 ejb and 1 war module) vs separate modules?

Not a complete list: In an EAR you can define Utility JARs as well, they are located in e.g. EAR/lib and can be reused by WARs and EJB JARs. Often EAR files provide prorietary deployment features, e.g. in WebSphere you can specify DataSource details, so that you do not have to define a DataSource (and JDBC driver) using management utilitiers.

Since I want use Glassfish 3 web profile, but it does not support ear archive. Can I simply use ejb and war as separate modules? Any other options?

Yes, the web profile spec explicitly allows you to deploy a lightweight EJB version as part of the WAR. Simply place the EJB JAR inside the WARs WEB-INF/lib. This link provides a comparison of features (Web profile vs full-blown): http://glassfish.java.net/downloads/v3-final.html