Skip to main content

Elvis, Meet Portability

Posted by bleonard on April 14, 2006 at 12:59 PM PDT

Last month I took a Cay S. Hortsmann article, Elvis Meets GlassFish, and demonstrated how you could use the NetBeans 5.5 Preview to make Elvis even happier. I was curious what it would take to make that same application run on JBoss. Elvis continues to smile.

Getting Started
  • Download and install the NetBeans 5.5 Preview. This will install both NetBeans and if needed, the GlassFish Application Server.
  • Build the Quiz application as documented in my previous entry, Elvis, Meet NetBeans.
  • Download and install JBoss 4.0.4 (CR2 at the time of the blog entry). During installation, be sure to select the ejb3 profile.
Step 1: Add the JBoss Server to NetBeans
  1. Switch to the Runtime tab and right-click the Servers node to add a server.






  2. Select JBoss Application Server 4.0.3 from the Server drop down list. Change the name to JBoss Application Server 4.0.4 CR2:






  3. Complete the Add Server wizard.
  4. Stop the Sun Java System Application Server if it is running (both servers use http port 8080 as their default).
  5. Right-click the new JBoss server node and choose Start:




Step 2: Update and Deploy the Quiz Application to JBoss

We need to make a couple of changes to the Quiz application in order for it to successfully deploy to JBoss.

  1. If not already open, open the Quiz project
  2. Open the Quiz project properties, select the Run category, and change the Server from Sun Java System Application Server to JBoss Application Server 4.0.4 CR2:






  3. Delete the ejb-jar.xml Configuration File from the Quiz-EJBModule. The JBoss Server does not yet recognize all of the namespaces declared in this file which will cause deployment to fail. Also, because we're using EJB3 annotations, this file is now empty and no longer necessary:






  4. Open persistence.xml and replace the persistence-unit element with the following in order to use the embedded HSQL database in JBoss:



      <persistence-unit name="default">
        <jta-data-source>java:/DefaultDS</jta-data-source>
        <properties>
          <property name="hibernate.hbm2ddl.auto"
                    value="create-drop"/>
        </properties>
      </persistence-unit>


  5. Right-click the Quiz project and choose Clean & Build Project. This is necessary in order to remove the ejb-jar.xml from the previously build.
  6. Right-click the Quiz project and select Deploy Project
Step 3: Update the Client

Actually, our client is so simple that it's easier to just re-create it.

  1. Create a new General Java Application project, JBossQuizClient. Set the Main Class to elvis.client.Client.
  2. QuizClient needs access to several libraries:
    • The JBoss Client Libraries. Right-click the Libraries node and choose Add Jar/Folder. Browse to your /client folder and select the following jars:
      • jbossall-client.jar
      • jboss-ejb3-client.jar
      • jboss-aop-jdk50-client.jar
      • jboss-aspect-jdk50-client.jar..
    • The Quiz session interface. Right-click the Libraries node and choose Add Jar/Folder. Browse to your /Quiz/Quiz-ejb/build directory and select ear-module.
  3. Add the following to main, including the throws clause:



        public static void main(String[] args) throws Exception {
            Hashtable env = new Hashtable();
            env.put("java.naming.factory.initial",
                    "org.jnp.interfaces.NamingContextFactory");
            env.put("java.naming.provider.url",
                    "jnp://localhost:1099");
            env.put("java.naming.factory.url.pkgs",
                    "org.jboss.naming:org.jnp.interfaces");
            InitialContext ctx = new InitialContext(env);

            TestRemote test = (TestRemote) ctx.lookup("quiz/TestBean/remote");
            long id = test.makeQuiz();
            System.out.println(test.getQuiz(id));
        }

       
  4. Press Alt+Shift+F to fix the imports.
  5. Press F6 to run the JBossQuizClient. You should see the following in the Output window:




Step 5: Spying on the Database

By default, the HSQL database runs in embedded (in-process) mode. In order to use NetBeans' database explorer tools, we need to switch the database to server mode.

  1. Outside of NetBeans, browse to your /server/default/deploy directory and open the HSQL configuration file, hsqldb-ds.xml.
  2. Uncomment the connection-url element to used for server mode:



    And comment the connection-url used for in-process persistent db mode.


  3. Comment the depends element used for in-process mode and uncomment the depends element used for server mode:




  4. Uncomment the mbean element used for server mode and comment the mbean element used for in-process mode:


  5. Switch to the NetBeans Runtime tab and restart JBoss.
  6. While still in the Runtime tab, expand the Databases node and right-click the Drivers node and choose Add Driver:






  7. In the Add JDBC Driver dialog, click Add and browse to your /server/default/lib directory and select hsqldb.jar. NetBeans will locate the driver class, org.hsqldb.jdbcDriver and suggest a name, HSQLDB. Click OK:






  8. Right-click the new HSQLDB driver node and select Connect Using:






  9. Set the Database URL to jdbc:hsqldb:hsql://localhost:1701 and the User Name to sa. Leave Password blank:






  10. Click OK and select the PUBLIC schema when prompted.
  11. Expand the hsqldb connection > Tables node. Right-click the QUESTION table and select View Data.




    Related Topics >>