Monday, September 20, 2010

jpa-osgi example in FUSE ESB 4

Since FuseSource[1] ESB 4.2.0-01-00, we introduce a new example jpa-osgi to demostrate how jpa works in OSGi based ESB container. We choose to use hibernate as JPA implementation in this demo, as hibernate is popularly used in existing system. Package hibernate jars as bundle isn't a easy job, but thanks for Spring Source's great work, we can simply reuse those Spring Source hibernate bundles in our demo.

We create a jpa-hibernate feature which put all necessary bundles together,
    <feature name="jpa-hibernate" version="4.3.0-fuse-01-00">
<bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.java-persistence-api-1.1.1/1.5.0</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.2.2_5</bundle>
<bundle>mvn:org.springframework/spring-jdbc/3.0.3.RELEASE</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_2</bundle>
<bundle>mvn:org.antlr/com.springsource.antlr/2.7.7</bundle>
<bundle>mvn:org.objectweb.asm/com.springsource.org.objectweb.asm/1.5.3</bundle>
<bundle>mvn:net.sourceforge.cglib/com.springsource.net.sf.cglib/2.2.0</bundle>
<bundle>mvn:org.jboss.javassist/com.springsource.javassist/3.9.0.GA</bundle>
<bundle>mvn:org.hibernate/com.springsource.org.hibernate.annotations.common/3.3.0.ga</bundle>
<bundle>mvn:org.hibernate/com.springsource.org.hibernate.annotations/3.4.0.GA</bundle>
<bundle>mvn:org.hibernate/com.springsource.org.hibernate.ejb/3.4.0.GA</bundle>
<bundle>mvn:org.hibernate/com.springsource.org.hibernate/3.3.2.GA</bundle>
<bundle>mvn:org.springframework/spring-orm/3.0.3.RELEASE</bundle>
</feature>

This demo is based on the cxf-wsdl-first scenario, which you can find it from both Fuse ESB 3.x and 4.x. The workflow is
cxfbc consumer====>cxfse endpoint.
cxfbc consumer handle external request, and cxfse endpoint retrieve PersonEntity according to ID from underlying hsqldb. We can put all complicated configuration in xbean used for cxf se endpoint.

<cxfse:endpoint>
<cxfse:pojo>
<bean class="org.apache.servicemix.samples.wsdl_first.PersonImpl">
<constructor-arg>
<ref bean="entityManagerFactory"/>
</constructor-arg>
</bean>
</cxfse:pojo>
</cxfse:endpoint>
<bean class="org.apache.servicemix.common.osgi.EndpointExporter" />
also configure JPA entityManagerFacotry in this file
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</property>
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="smx4"/>
<property name="jpaVendorAdapter" ref="jpaAdapter"/>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
<!-- DataSource Definition -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:smx4_jpa" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>

And there's a JPA required persistence.xml in src/main/resources/META-INF/ folder
  <persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<persistence-unit name="smx4" transaction-type="RESOURCE_LOCAL">
<class>org.apache.servicemix.samples.wsdl_first.PersonEntity</class>
<!-- Hibernate -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>

After installing jpa-hibernate feature, we can see related bundles set
[ 179] [Active     ] [            ] [       ] [   60] Apache ServiceMix Specs :: JAVA PERSISTENCE API 1.4 (1.5.0)
[ 180] [Active     ] [            ] [       ] [   60] Apache ServiceMix Bundles: commons-dbcp-1.2.2 (1.2.2.5)
[ 181] [Active     ] [            ] [       ] [   60] Spring JDBC (3.0.3.RELEASE)
[ 182] [Active     ] [            ] [       ] [   60] Apache ServiceMix Bundles: dom4j-1.6.1 (1.6.1.2)
[ 183] [Active     ] [            ] [       ] [   60] ANTLR (2.7.7)
[ 184] [Active     ] [            ] [       ] [   60] ObjectWeb ASM (1.5.3)
[ 185] [Active     ] [            ] [       ] [   60] CGLIB Code Generation Library (2.2.0)
[ 186] [Active     ] [            ] [       ] [   60] Javassist Java Programming Assistant (3.9.0.GA)
[ 187] [Active     ] [            ] [       ] [   60] JBoss Hibernate Common Annotations (3.3.0.ga)
[ 188] [Resolved   ] [            ] [       ] [   60] JBoss Hibernate Annotations (3.4.0.GA)
                                       Hosts: 190
[ 189] [Resolved   ] [            ] [       ] [   60] JBoss Hibernate Entity Manager (3.4.0.GA)
                                       Hosts: 190
[ 190] [Active     ] [            ] [       ] [   60] JBoss Hibernate Object-Relational Mapper (3.3.2.GA)
                                       Fragments: 188,189
[ 191] [Active     ] [            ] [       ] [   60] Spring ORM (3.0.3.RELEASE)
We can see bundle 188,189 are fragment bundle, at the moment we add this demo, Apache Felix not fully support fragment bundle yet, one outstanding issue is FELIX-1919[2], so this demo only works with equinox at that time. But now it works with both felix and equinox.

As hibernate is LGPL license, we can't add this demo into Apache Servicemix[3], but we do have an issue[4] on Apache Servicemix to track it, we can use OpenJPA to replace hibernate for this demo an Apache Side later on.

[1]http://fusesource.com
[2]https://issues.apache.org/jira/browse/FELIX-1919
[3]http://servicemix.apache.org/home.html
[4]https://issues.apache.org/activemq/browse/SMX4-472

No comments:

Post a Comment