JavaEJBJumping Into JBoss

Jumping Into JBoss

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

JBoss is a free, open source, application server that implements the complete
Java 2 Enterprise Edition (J2EE) stack, including Java Server Pages (JSP),
servlets, and Enterprise JavaBeans (EJB). For J2EE developers that are getting
started with JBoss, this article presents the basics, including downloading,
installation, application deployment, and data source configuration.

Tools

To run JBoss, you will need the following tools:

  • Java 2 Standard Edition SDK 1.3.1 or higher

Additionally, to develop applications for JBoss, you may want the following
tools:

  • Ant 1.5 – Java build tool
  • XDoclet 1.1.2 – Javadoc plug-in for code generation
  • JBoss Project Template

Downloading

To get JBoss, go to www.jboss.org. JBoss
is available there in two flavors: the standard bundle, which includes Jetty for
web pages and servlets, and the Tomcat bundle, which includes (you guessed it)
Tomcat instead of Jetty.

As of this writing, JBoss 3.2.1 is the latest stable release.

Installation

To install JBoss, first unzip the distribution file to a directory of your
choice. A directory named JBoss-version should be created, and it should
contain the following directories:

  • bin — scripts
  • client — client jars
  • docs — documentation
  • lib — server jars
  • server — server configurations

Second, set the following environment variables:

  • JBOSS_HOME – Set to JBoss installation directory.
  • JAVA_HOME – Set to Java 2 SDK home directory.

Server Configurations

JBoss comes with three server configurations:

  • minimal – This configuration includes only logging, the JNDI service,
    and the URL deployment scanner. You would want to use this configuration
    for starting services that don’t require J2EE or as the base for a custom
    configuration.

  • default – This configuration is the default. It includes all of the
    J2EE services exception RMI/IIOP and clustering.

  • all – This configuration includes all JBoss services.

Startup

To start JBoss:

  • Windows:

    1. Open an MS-DOS Prompt or Command Prompt window.
    2. cd %JBOSS_HOME%bin
    3. run [-c default|minimal|all]

  • Unix:

    1. Open a shell.
    2. cd $JBOSS_HOME/bin
    3. ./run [-c default|minimal|all]

If JBoss starts successfully, you should see something like this:


21:11:16,637 INFO [Server] JBoss (MX MicroKernel) [3.2.1 (build: CVSTag=JBoss_3
_2_1 date=200305041533)] Started in 26s:919ms

Once the server is started successfully, you can verify that all of the J2EE
services are up and running by opening http://localhost:8080/jmx-console/ in
your browser. The JBoss Management Console will start. It shows all of the
services included in the currently running JBoss configuration and various
details about each service.

JBoss can also run as an NT Service or a Unix service. For more information
on how to do either, see the “JBoss 3.0 Quick Start Guide”.

Shutdown

If you started JBoss in the foreground from an MS-DOS Prompt window, Command
Prompt window, or Unix shell, simply press Ctrl-C in the window where JBoss
is running.

To shutdown JBoss in the window or shell where it is running, press Ctrl-C.

To shutdown JBoss from another window or shell:

  • Windows:

    • Open an MS-DOS Prompt or Command Prompt window.
    • cd %JBOSS_HOME%bin
    • shutdown -S or shutdown --server=url

  • Unix:

    • Open a shell.
    • cd $JBOSS_HOME/bin
    • ./shutdown -S or ./shutdown --server=url

Management

Jboss is managed with a web-based console. To access it, start your browser
and go to “http://localhost:8080/jmx-console”. To view or change the
configuration for a particular service, simply click on the link to it.

Additionally, with the management console, you can invoke public methods
of services. For example, to shutdown the server:

  • Open the management console.
  • Scroll down the page to the “jboss.system” section.
  • Click on the “type=Server” link.
  • Scroll down to “void shutdown()” section.
  • Click the “Invoke” button.

Deploying Applications

Deploying applications to JBoss could not be easier. Simply copy your
application archive file — .jar, .war, or .ear — to the deploy directory of
the server configuration that is running. JBoss will detect the file, extract
the contents, and load the application.

Here is an example of deploying an application to the default server
configuration:

  • Windows:

    copy rotmachine.ear %JBOSS_HOME%serverdefaultdeploy

  • Unix:

    cp rotmachine.ear $JBOSS_HOME/server/default/deploy

During deployment, JBoss logs messages to the console and log/server.log,
which is located in the server configuration directory. If deployment is
successful, you should see a message like this:

22:21:44,730 INFO [MainDeployer] Deployed package:
file:/C:/jboss-3.2.1/server/default/deploy/rotmachine.ear

Additionally, if you open the JBoss Management Console, you should see your
application listed in the “jboss.j2ee” section.

Undeploying Applications

Undeploying applications is even easier than deploying them. To undeploy a
application, delete the application archive file from the deploy directory.
JBoss will notice that the file has been deleted and undeploy the application.

When undeploying an application, JBoss logs messages to the console and
server.log. If undeployment is successful, you should see a message like this:

22:59:41,500 INFO [MainDeployer] Undeployed
file:/C:/jboss-3.2.1/server/default/deploy/rotmachine.ear

Data Sources

If your application is going to connect to a database using JBoss, you will
need to configure a data source for the database. Here is how to configure a
basic data source:

  1. If the JDBC driver library for your database is not in lib directory of
    the server configuration you are using, copy it there, then restart JBoss.

  2. Create a data source descriptor for your database, e.g.
    oracle-ds.xml.

  3. Deploy the data source descriptor as you would deploy an
    application.

Here is an example of a descriptor for an Oracle data source:

   <?xml version="1.0" encoding="UTF-8"?>
   <datasources>
      <local-tx-datasource>
         <jndi-name>OracleDS</jndi-name>
         <connection-url>jdbc:oracle:thin:@oracle.somewhere.com:1521:DB</connection-url>
         <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
         <user-name>dev</user-name>
         <password>secret</password>
      </local-tx-datasource>
   </datasources>

Additional examples of data source descriptors can be found in
JBOSS_HOME/docs/examples/jca.

Enterprise JavaBeans (EJBs)

EJBs are constructed for JBoss like they would be constructed for any other
J2EE application server, with the addition of a JBoss EJB deployment descriptor,
jboss.xml. Here is an example:

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN"
      "http://www.jboss.org/j2ee/dtd/jboss.dtd">

   <jboss>
      <enterprise-beans>
         <session>
            <ejb-name>Processor</ejb-name>
            <jndi-name>ejb/Processor</jndi-name>
         </session>
         <entity>
            <ejb-name>Data</ejb-name>
            <jndi-name>ejb/Data</jndi-name>
         </entity>
      </enterprise-beans>
   </jboss>

Note that, unlike other J2EE servers, JBoss does not require generation of
stubs and skeletons. It handles EJB usage dynamically.

Clients

As mentioned above, JBoss does not require client stubs. You will need to
provide only the following to clients:

  • The EJB home and remote.
  • Any classes that are passed between the client and the EJB.
  • A copy of JBOSS_HOME/client/jbossall-client.jar.

Here is an example of connecting to an EJB deployed in JBoss:

   // Create env.
   Hashtable env = new Hashtable();
   env.put("java.naming.factory.initial",
      "org.jnp.interfaces.NamingContextFactory");
   env.put("java.naming.factory.url.pkgs",
      "org.jboss.naming:org.jnp.interfaces");
   env.put("java.naming.provider.url",
      "localhost");

   // Create naming context.
   InitialContext context = new InitialContext(env);

   // Find home.
   CipherHome home = (CipherHome) context.lookup("ejb/org/rotmachine/Cipher");

   // Create remote.
   Cipher cipher = home.create();

Example Application

To help you further investigate JBoss, an example application called
RotMachine is included with this article. See the Resources
section below for a link.

RotMachine is a simple J2EE application that includes an EJB (stateless
session bean), a web client, and a command-line client. It was built using the
JBoss project template, so you will want to get the additional tools listed in
the Tools section above.

To install the RotMachine source, unzip the archive file to a directory of
your choice.

To build and deploy RotMachine:

  • Windows:

    1. Open an MS-DOS Prompt window or Command Prompt windows.
    2. cd (RotMachine install dir.)
    3. ant deploy-server

  • Unix:

    1. Open a shell.
    2. cd (RotMachine install dir.)
    3. ant deploy-server

To test RotMachine using the command-line client:

  • Windows:

    1. cd buildbin
    2. run-client

  • Unix:

    1. cd build/bin
    2. ./run-client.sh

To test RotMachine using the web client:

  1. Open your browser and go to “http://localhost:8080/rotmachine/index.jsp”.
  2. Enter some text in the form.
  3. Click the “Go!” button.

Resources

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories