Jetty 6 and XBean

As you probably know, Jetty is a web server (and servlet container) written purely in Java. Because it is small, fast and has a well designed configuration API it is often used as a embedded server in various Java applications (Continuum, FishEye, …).

Of course, when you use Jetty in a Spring-based application, you will want to configure it as you configure the rest of your application (as a Spring bean). Since it has a well defined configuration API, this is not a problem to achieve.

Here’s an example of a standard Spring bean that, if included in the configuration, would start Jetty on port 8181 and load a web application from the webapp subfolder:

<bean id="Server"
      class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
    <property name="connectors">

      <list>
        <bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
          <property name="port" value="8181"/>
        </bean>
      </list>
    </property>

    <property name="handler">
      <bean id="handlers"
           class="org.mortbay.jetty.handler.HandlerCollection">
        <property name="handlers">
          <list>
             <bean id="contexts"
              class="org.mortbay.jetty.handler.ContextHandlerCollection">
               <property name="handlers">

                 <list>
                   <bean class="org.mortbay.jetty.webapp.WebAppContext">
                     <property name="contextPath" value="/"/>
                     <property name="resourceBase" value="webapp"/>
                     <property name="parentLoaderPriority" value="false"/>
                   </bean>

                 </list>
               </property>
             </bean>
          </list>
        </property>
      </bean>

    </property>
</bean>

Now, although this configuration is valid it is hard to read and maintain. But starting with Jetty 6, there is cure for this too. You have probably heard of XBean project. Among other things XBean, supports custom XML schemas that could be used inside Spring configuration files and Jetty 6 supports XBean configuration.

To demonstrate this we will create a XBean configuration snippet, which is equivalent to the above Spring bean:

<jetty xmlns="http://mortbay.com/schemas/jetty/1.0">
    <connectors>

      <nioConnector port="8181" />
    </connectors>

    <handlers>
      <webAppContext contextPath="/" resourceBase="webapp"
           parentLoaderPriority="false" />
    </handlers>
 </jetty>

All that is left to be done is to include jetty-xbean-6.x.x.jar (x.x denotes a Jetty version) in your application’s classpath. You can find this JAR in the lib/xbean folder of the Jetty 6 distribution.

This XBean configuration is much more easier to write and maintain (and will certainly make your configuration files smaller). It’s good to see that development team puts a great effort in making Jetty play well with other Java technologies. Jetty 6 brings much more features then this simplified configuration. Some of these are NIO connectors and Servlet 2.5 specification, but they deserve a post of their own.

Leave a comment

Your email address will not be published. Required fields are marked *