Enterprise Java Development@TOPIC@
Prior to JavaEE 6
EJB beans could only be deployed in EJB modules
EJB modules integrated with WARs using common EAR
Starting with JavaEE 6
EJB beans can be embedded within WAR
EJB modules can be deployed within WAR
|-- index.html `-- WEB-INF ... |-- jboss-web.xml |-- lib `-- web.xml
index.html - example static web resource
WEB-INF/jboss-web.xml - container-specific configuration. Used here to control name of WAR
WEB-INF/lib - directory to place jars for classpath
WEB-INF/web.xml - standard WAR deployment descriptor
EJB beans developed external to WAR
Integrated using WEB-INF/lib
WEB-INF/lib/ `-- webejbCustomerEJB-3.0.2012.2-SNAPSHOT.jar
example EJB module contents
$ jar tf WEB-INF/lib/webejbCustomerEJB-3.0.2012.2-SNAPSHOT.jar | sort ejava/examples/ejbwar/customer/bo/Customer.class ejava/examples/ejbwar/customer/bo/CustomerRepresentation.class ejava/examples/ejbwar/customer/bo/Customers.class ejava/examples/ejbwar/customer/client/ ejava/examples/ejbwar/customer/client/CustomerClient.class ejava/examples/ejbwar/customer/client/CustomerClientImpl.class ejava/examples/ejbwar/customer/CustomerResources.class ejava/examples/ejbwar/customer/Customers.class ejava/examples/ejbwar/customer/dao/ ejava/examples/ejbwar/customer/dao/CustomerDAO.class ejava/examples/ejbwar/customer/dao/CustomerDAOImpl.class ejava/examples/ejbwar/customer/ejb/ ejava/examples/ejbwar/customer/ejb/CustomerMgmt.class ejava/examples/ejbwar/customer/ejb/CustomerMgmtEJB.class ejava/examples/ejbwar/customer/ejb/CustomerMgmtLocal.class ejava/examples/ejbwar/customer/ejb/CustomerMgmtRemote.class ejava/examples/ejbwar/customer/rs/ ejava/examples/ejbwar/customer/rs/CustomersResource.class META-INF/beans.xml META-INF/MANIFEST.MF META-INF/persistence.xml
JNDI name uses WAR name (minus.war)
java:global/jaxrsInventoryWAR/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtRemote java:app/jaxrsInventoryWAR/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtRemote java:module/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtRemote java:jboss/exported/jaxrsInventoryWAR/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtRemote java:global/jaxrsInventoryWAR/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtLocal java:app/jaxrsInventoryWAR/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtLocal java:module/CustomerMgmtEJB!ejava.examples.ejbwar.customer.ejb.CustomerMgmtLocal
jboss-web.xml used to express WAR name without version#
$ cat WEB-INF/jboss-web.xml <jboss-web> <!-- needed to always assure that version# does not get into JNDI name --> <context-root>jaxrsInventoryWAR</context-root> </jboss-web>
EJB module contains persistence.xml and CDI beans.xml
META-INF/beans.xml META-INF/persistence.xml
EJB beans developed as part of WAR
No need to declare new EJB module just to get EJB bean functionality
WEB-INF |-- beans.xml |-- classes | |-- ejava | | `-- examples | | `-- ejbwar | | `-- inventory | | |-- bo | | | |-- Categories.class | | | |-- Category.class | | | |-- InventoryRepresentation.class | | | |-- Product.class | | | `-- Products.class | | |-- client | | | |-- InventoryClient.class | | | `-- InventoryClientImpl.class | | |-- dao | | | |-- InventoryDAO.class | | | `-- InventoryDAOImpl.class | | |-- ejb | | | |-- InventoryMgmtEJB.class | | | `-- InventoryResources.class | | |-- rmi | | | |-- InventoryMgmtRemote.class | | | `-- InventoryMgmtRMIEJB.class | | `-- rs | | |-- Application.class | | |-- CategoriesResource.class | | |-- PrettyPrinter.class | | |-- ProductsResource.class | | |-- ResourceHelper.class | | `-- TxFilter.class | `-- META-INF | `-- persistence.xml ...
CDI descriptor hosted in WEB-INF/beans.xml
persistence unit hosted in WEB-INF/classes/META-INF/persistence.xml
JNDI name uses WAR name (minus.war)
java:global/jaxrsInventoryWAR/InventoryMgmtRMIEJB!ejava.examples.ejbwar.inventory.rmi.InventoryMgmtRemote java:app/jaxrsInventoryWAR/InventoryMgmtRMIEJB!ejava.examples.ejbwar.inventory.rmi.InventoryMgmtRemote java:module/InventoryMgmtRMIEJB!ejava.examples.ejbwar.inventory.rmi.InventoryMgmtRemote java:jboss/exported/jaxrsInventoryWAR/InventoryMgmtRMIEJB!ejava.examples.ejbwar.inventory.rmi.InventoryMgmtRemote java:global/jaxrsInventoryWAR/InventoryMgmtRMIEJB java:app/jaxrsInventoryWAR/InventoryMgmtRMIEJB java:module/InventoryMgmtRMIEJB
Alternative: Use traditional RMI interface (specific to Java)
Alternative: Use Web-oriented HTTP interface (open to all platforms)
JAX-RS 1.x only addresses server side
Many vendor-specific client libraries (e.g., RESTEasy, Jersey, HttpClient)
HTTP/REST Client API will be part of JAX-RS 2.0
Class examples implements with Apache HttpClient
Notional example accessing /index.html
HttpClient client = new DefaultHttpClient();
URI appURI = "http://127.0.0.1:8080/jaxrsInventoryWAR";
URI uri = UriBuilder.fromUri(appURI).path("index.html").build();
//build the overall request
HttpGet get = new HttpGet(uri);
get.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML);
HttpResponse response = client.execute(get);
if (Response.Status.OK.getStatusCode() == response.getStatusLine().getStatusCode()) {
return ...
}