Dvdstore example migration for JBoss AS 7.x
Posted by manarh in Marek Novotny's Blog on Dec 16, 2011 3:48:03 PMToday I will guide you through migration path for Dvdstore example from Seam 2.2.2.Final distribution.
What is interesting on Dvdstore example? It presents quite complex set of Seam features including jbpm-jpdl and Hibernate Search integration.
Data source preparation
If you don't want to use default ExampleDS and change JNDI in persistence.xml, you have to create the new data source under JNDI java:/dvdstoreDatasource.
Simply run:
bin/jboss-admin.sh -c \ --command="add-data-source --jndi-name=java:/dvdstoreDatasource --connection-url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 --username=sa --password=sa --pool-name=DefaultDS_pool --driver-name=h2"
In comparing to JBoss AS 7.0.2.Final, if you use 7.1.0.x you need to do additional step to enable data source, because it is created disabled:
bin/jboss-admin.sh -c --command="/subsystem=datasources/data-source="java\:\/dvdstoreDatasource":enable"
You can check the creation of datasource in server log or on console:
INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) Bound data source [java:/dvdstoreDatasource]
Enterprise package migration - dvdstore EAR
Main target of EAR configuration is to set up correctly supported runtime libraries - these are JSF 1.2, Hibernate JPA. Seam 2.2.2.Final has got integration with JSF 1.2, JPA 1.0. On the other side JBoss AS7 has got JSF 2.1 and JPA 2.0 by default. These specifications are not drop in replacements and therefore dvdstore configurations require some changes to disable defaults and enable supported Seam application. This type of configurations can be managed in more than one way.
I will show you the powerful way - which are placed in jboss-deployment-structure.xml file. This is not new, you already could see it in my previous blogs about Booking nad JPA examples migrations. But now jboss-deployment-structure.xml file have one extra sub-deployment element section in comparison to Booking jboss-deployment-structure.xml file.
Insert the following content to jboss-seam-dvdstore.ear/META-INF/jboss-deployment-structure.xml:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0"> <deployment> <exclusions> <module name="org.hibernate" slot="main"/> </exclusions> <dependencies> <module name="org.apache.log4j" export="true"/> <module name="org.dom4j" export="true"/> <module name="org.apache.commons.logging" export="true"/> <module name="org.apache.commons.collections" export="true"/> <module name="javax.faces.api" slot="1.2" export="true"/> <module name="com.sun.jsf-impl" slot="1.2" export="true"/> <module name="org.slf4j" export="true"/> </dependencies> </deployment> <sub-deployment name="jboss-seam-dvdstore.war"> <exclusions> <module name="javax.faces.api" slot="main"/> <module name="com.sun.jsf-impl" slot="main"/> <module name="org.hibernate" slot="main"/> </exclusions> <dependencies> <module name="javax.faces.api" slot="1.2"/> <module name="com.sun.jsf-impl" slot="1.2"/> </dependencies> </sub-deployment> <sub-deployment name="jboss-seam-dvdstore.jar"> <exclusions> <module name="org.hibernate" slot="main"/> </exclusions> </sub-deployment> </jboss-deployment-structure>
Little explanation to all lines above:
- Deployment element disables default JPA 2 provider in main slot (see exclusion of org.hibernate), because we won't use default Hibernate from AS7, but bundled Hibernate 3.3.1 in application lib directory.
- Deployment element for EAR also turns on all necessary dependencies and JSF 1.2 support. Notice additional attribute export, which says that a module be available also for WAR(s) and JAR(s).
- Sub-deployment for WAR jboss-seam-dvdstore.war disables default JSF 2 and choosed JSF 1.2 alternative and again disabling Hibernate 4 slot.
- Sub-deployment for EJB jar jboss-seam-dvdstore.jar disables default main Hibernate 4 slot too.
Final step is to add all required additional jars for using known supported integraitons with Seam 2.2 It means bundling hibernate jars from Seam 2.2.2.Final distribution and its direct runtime dependencies:
- hibernate-core.jar
- hibernate-validator.jar
- hibernate-entitymanager.jar
- bsh.jar
- hibernate-annotations.jar
- antlr.jar
EJB archive migration - JAR
Main point of the following steps are to be properly bundled Hibernate 3 libraries and usage of dependencies which are originally integrated with Seam.
Main points of using JPA provider in AS7 are covered in https://docs.jboss.org/author/display/AS7/JPA+Reference+Guide in section "Packaging the Hibernate 3.5 or greater 3.x JPA persistence provider with your application". But there are some limitations of hibernate integration older than 3.5 version.
For instance import/export database schema with bundled Hibernate 3.3.x doesn't work out of the box - it requires additional class mapping. Next change is caused by switching of default database driver for data source from HSQLDB to H2 database and little incompatibility changes in Varchar support. This requires a small change in com.jboss.dvd.seam.Product class to extends length of description field to e.g 9000. Because otherwise you will see exceptions like this:
Caused by: org.h2.jdbc.JdbcSQLException: Value too long for column "DESCRIPTION VARCHAR(1024)"
Changes in jboss-seam-dvdstore.jar/META-INF/persistence.xml are following:
- We need to choose proper JPA provider for bundled Hibernate 3.3 library:
<property name=
"jboss.as.jpa.providerModule"
value=
"hibernate3-bundled"
/>
- Adding mapping for entity classes due not possibility to automatically entities scan for bundled Hibernate 3.3:
<class>com.jboss.dvd.seam.Actor</class>
<class>com.jboss.dvd.seam.Admin</class>
<class>com.jboss.dvd.seam.Category</class>
<class>com.jboss.dvd.seam.Customer</class>
<class>com.jboss.dvd.seam.Inventory</class>
<class>com.jboss.dvd.seam.Order</class>
<class>com.jboss.dvd.seam.OrderLine</class>
<class>com.jboss.dvd.seam.Product</class>
<class>com.jboss.dvd.seam.Product</class>
<class>com.jboss.dvd.seam.User</class>
- We also need to change value of transaction.manager_lookup_class property to org.jboss.as.jpa.hibernate3.JBossAppServerJtaPlatform in hibernate.cfg.xml. This file is required for JBPM-JPDL database configuration for business process mapping.
<property name="transaction.manager_lookup_class" value="org.jboss.as.jpa.hibernate3.JBossAppServerJtaPlatform" />
- Last step in EJB jar migration is, we need all xml file originally placed at EAR root to be moved into EJB jar root:
- checkout.jpdl.xml,
- jbpm.cfg.xml,
- hibernate.cfg.xml,
- newuser.jpdl.xml,
- ordermanagement1.jpdl.xml,
- ordermanagement2.jpdl.xml,
- ordermanagement3.jpdl.xml.
Reason for that moving files is that EAR root directory is not in classloader path anymore n JBoss AS7.
Web application packaging in WAR
Configuration in jboss-seam-dvdstore.war should be changed in way of customizing new format of JNDI to EJBs, first adding two lines for Seam EJBs EjbSynchronizations and TimerServiceDispatcher, second set all other dvdstore application based EJBs to JNDI pattern - this is the same as before, you need only change the JNDI pattern in jboss-seam-dvdstore.war/WEB-INF/components.xml from
<core:init debug="true" jndi-pattern="jboss-seam-dvdstore/#{ejbName}/local"/>
to
<core:init debug="true" jndi-pattern="java:app/jboss-seam-dvdstore.jar/#{ejbName}"/>
I will use this way, because it is proper for dvdstore application with many application session beans. So at the end of this change, you will have jboss-seam-dvdstore.war/WEB-INF/components.xml with these additional 2 lines under already existing <core:init/> line:
<core:init debug="true" jndi-pattern="java:app/jboss-seam-dvdstore.jar/#{ejbName}"/> <component class="org.jboss.seam.transaction.EjbSynchronizations" jndi-name="java:app/jboss-seam/EjbSynchronizations"/> <component class="org.jboss.seam.async.TimerServiceDispatcher" jndi-name="java:app/jboss-seam/TimerServiceDispatcher"/>
And that is all! Enjoy shopping of DVDs in JBoss AS7.
Comments