December 20, 2014
Hot Topics:

Creating and Deploying Stateless Session Beans and CMP Entity Beans

  • September 9, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

From the very beginning, I would like to notify you that this article is unusual. I believe we should call it a "manual," not an "article." In this document, we will provide the instructions needed to create a very simple (but still working) stateless session bean, a simple CMP (Container Managed Persistence) entity bean, then deploy everything on a JBoss application server. Because we are creating an entity bean, we will need a persistence store—a database. In our case, it will be MySQL database server. This article will explain how to "teach" JBoss to support CMP on MySQL.

On this initial point, I am eager to warn you that you have to be familiar with J2EE programming, you need to know the basics of EJB programming, you need at least to understand that JBoss is an application server, and that MySQL is a database server. If you have some troubles with these nuances, I suggest that you to skip reading this article and come back only when you feel yourself ready. In general, this article is created to explain how to apply your knowledge of EJB into "real life," and how to create some working stuff. But, this article will not teach you all these technologies.

And at last, to finish with the preparatory part, I will list all the software that is required for us, so we will be able to check up on material given on practice in our article. We will require JBoss 3.2.3 application server (it comes with Tomcat 4.1, so we do not need separated a Web container), MySQL 4.0.16 database server, Apache Ant 1.6.2—a Java-based build tool, and of course any simpliest text editor to copy and paste source code from this article. All software is free; you can download it easily from the companies' official Web sites. Before we will start, make sure you have downloaded everything and properly installed and configured all paths (PATH and CLASSPATH variables). This process is beyond the scope of our article, so we are leaving all these requirements to you.

First, our task will be creating Ant's build.xml, a so-called "make file," which will have all rules and instructions on how to compile, pack, and deploy all our stuff. Here it is; put it in the root directory, where you will be placing all code:

<project name="teste" default="ear">
   <!-- define our environment -->
   <property name="jboss.home"   value="C:\jboss-3.2.3"/>
   <property name="jboss.server.dir"
             value="${jboss.home}\server\default"/>
   <property name="jboss.deploy.dir"
             value="${jboss.server.dir}\deploy"/>
   <property name="source.dir"   value="src"/>
   <property name="classes.dir"  value="classes"/>
   <property name="app.ear"
             value="${ant.project.name}.ear"/>
   <property name="app.war"
             value="${ant.project.name}.war"/>
   <property name="app.ejb.jar"
             value="${ant.project.name}-ejb.jar"/>
   <!-- ---------------------- -->
   <path id="compile.class.path">
      <pathelement location="${jboss.server.dir}\lib\jboss-j2ee.jar"/>
   </path>
   <target name="all" depends="clean,ear,deploy"/>
   <target name="compile">
      <mkdir dir="${classes.dir}"/>
      <javac srcdir="${source.dir}" destdir="${classes.dir}"
             classpathref="compile.class.path" debug="yes"/>
      <copy todir="${classes.dir}">
         <fileset dir="${source.dir}">
            <include name="META-INF/**"/>
         </fileset>
      </copy>
   </target>
   <target name="jar" depends="compile">
      <jar jarfile="${app.ejb.jar}">
         <fileset dir="${classes.dir}">
            <include name="META-INF/jbosscmp-jdbc.xml"/>
            <include name="META-INF/ejb-jar.xml"/>
            <include name="META-INF/jboss.xml"/>
            <include name="META-INF/mysql-ds.xml"/>
            <include name="**/*.class"/>
         </fileset>
      </jar>
   </target>
<target name="war">
      <jar jarfile="${app.war}">
         <fileset dir="${source.dir}">
            <include name="WEB-INF/web.xml"/>
            <include name="*.jsp"/>
         </fileset>
      </jar>
   </target>
   <target name="ear" depends="jar,war">
      <jar jarfile="${app.ear}">
         <fileset dir="${classes.dir}">
            <include name="META-INF/application.xml"/>
            <include name="${app.ejb.jar}"/>
         </fileset>
         <fileset dir=".">
            <include name="${app.ejb.jar}"/>
            <include name="${app.war}"/>
         </fileset>
      </jar>
   </target>
   <target name="deploy">
      <copy file="${app.ear}" todir="${jboss.deploy.dir}"/>
   </target>
   <target name="undeploy">
      <delete file="${jboss.deploy.dir}/${app.ear}" quiet="true"/>
   </target>
   <target name="clean">
      <delete includeEmptyDirs="true" quiet="true">
         <fileset dir=".">
            <include name="*.jar"/>
            <include name="*.war"/>
            <include name="*.ear"/>
         </fileset>
         <fileset dir="${classes.dir}"/>
      </delete>
   </target>
</project>

Just create this file, save it, and forget about it for now. Honestly speaking, you can forget about it at all. As rough as it sounds, you do not necessarily need to be an Ant guru and keep all the structures of an XML configuration file in your head to use all the advantages of the Ant build tool. I do not remember them because I already have skeleton of build.xml and I use it almost all the time. Of course, when I require some other features, I just open Ant's documentation, look for the functions that I need, and use them. That's all. I suggest that you do the same if you do not have the time or desire to learn everything from the very beginning.





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel