JBoss at Work. A Practical Guide
Authors: Davis S. Marrs T.
Published year: 2004
Pages: 52-54/197
Buy this book on amazon.com >>

5.3. Hibernate MBean Service Descriptor

Now that we have the HBM file in place, we must create an MBean service configuration file for Hibernate. Hibernate is a service, no different than Hypersonic or any of the others. Each MBean needs a service configuration file like Example 5-4 so that JBoss will recognize it and run it on startup.

Example 5-4. hibernate-service.xml
<server>
    <mbean code="org.jboss.hibernate.jmx.Hibernate"
           name="jboss.har:service=Hibernate">
        <attribute name="DatasourceName">java:/JBossAtWorkDS</attribute>
        <attribute name="Dialect"> org.hibernate.dialect.HSQLDialect</attribute>
        <attribute name="SessionFactoryName">
                   java:/hibernate/SessionFactory</attribute>
        <attribute name="CacheProviderClass">
           org.hibernate.cache.HashtableCacheProvider
        </attribute>
    </mbean>
</server>

Let's step through it line by line.

  • The <mbean> element names the service and specifies the implementing class.

  • The <attribute name="DatasourceName"> element is a link to your Hypersonic datasource using the global JNDI name.

  • The <attribute name="Dialect"> element tells Hibernate which type of database it talks to. As much as we'd like to believe the "s" in SQL stands for "standard," the acronym is short for "Structured Query Language." Each database vendor's implementation of SQL varies, and this setting allows Hibernate to generate well- formed SQL for the specific database in question. Other common dialects include org.hibernate.dialect.Oracle9Dialect and org.hibernate.dialect.MySQLDialect.

  • The <attribute name="SessionFactoryName"> element is the global JNDI name for this service's SessionFactory. We'll use this name in jboss-web.xml (and map it to a local ENC-style name in web.xml ).

  • Finally, the <attribute name="CacheProviderClass"> element tells Hibernate what caching strategy to use. Rather than going round trip to the database for each request, Hibernate caches the results to improve performance. (See the Hibernate documentation for a more in-depth discussion of the different CacheProviders.)



5.4. Creating a HAR

Now that we created the HBM files and the Hibernate MBean service deployment descriptor, we are ready to bundle things up and deploy it as a part of the EAR.

Hibernate applications are bundled up in a Hibernate Archive (HAR). We use the standard Ant <jar> task in Example 5-5 to create the HAR.

Example 5-5. build.xml
<!-- ====================================== -->
    <target name="har" depends="generate-hbm"
            description="Builds the Hibernate HAR file">
        <mkdir dir="${distribution.dir}" />

        <jar destfile="${distribution.dir}/jaw.har">
            <!-- include the hbm.xml files  -->
            <fileset dir="${gen.source.dir}">
                <include name="**/*.hbm.xml"/>
            </fileset>

            <!-- include hibernate-service.xml -->
            <metainf dir="${hibernate.dir}">
                <include name="hibernate-service.xml"/>
            </metainf>
        </jar>
    </target>



5.5. Adding the HAR to the EAR

Moving up a level from the common subproject to the master project, we need to make sure that the EAR file includes our newly created HAR. The master build file already handles this step, since the jaw.har file is built in the same directory as common.jar . (Look in common/build/distribution to confirm this.)

But what about our application.xml file? This is traditionally where we identify the JARs that are included in the EAR. HARs are not a standard part of a J2EE EAR file, so JBoss looks for a jboss-app.xml file to handle container-specific exceptions to the standard. src/META-INF/ stores this file, right next to your application.xml file. Example 5-6 shows what it looks like.

Example 5-6. jboss-app.xml
<!DOCTYPE jboss-app PUBLIC "-//JBoss//DTD J2EE Application 1.4//EN"
           "http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd">
<jboss-app>
  <module>
    <har>jaw.har</har>
  </module>
</jboss-app>

Our <ear> task now includes this file along with the traditional application.xml in Example 5-7.

Example 5-7. Master build.xml
<!-- ====================================== -->
    <target name="ear" depends="compile"
            description="Packages all files into an EAR file">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${distribution.dir}" />

        <echo message="##### Building EAR #####" />
        <ear destFile="${distribution.dir}/${ear.name}"
             appxml="${meta-inf.dir}/application.xml" >
            <!-- files to be included in / -->
            <fileset dir="${webapp.war.dir}" />
            <fileset dir="${common.jar.dir}" />

            <!-- include jboss-app.xml -->
            <metainf dir="${meta-inf.dir}">
                <include name="jboss-app.xml"/>
            </metainf>

        </ear>
    </target>


JBoss at Work. A Practical Guide
Authors: Davis S. Marrs T.
Published year: 2004
Pages: 52-54/197
Buy this book on amazon.com >>

Similar books on Amazon