Oddjob With Spring

Spring is great for creating flexible applications by assembling loosely coupled components using XML. Here’s a simple Spring configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="hello" class="example.HelloWorldBean" />

</beans>

Except that now you have to go back to your context assisted IDE and write some Java to launch it!

public static void main(String... args) {

	ConfigurableApplicationContext ctx = 
			new ClassPathXmlApplicationContext(
					"classpath:config/spring/Simple.xml");

	Runnable hello = ctx.getBean("hello", Runnable.class);

	hello.run();

	ctx.close();
}

Wouldn’t it be great if you could have some more XML that launched your Spring App instead of this pesky Java code! And how cool would it be if that XML were in a completely different format from that which Spring uses, such as this:

<oddjob id="this">
  <job>
    <sequential>
      <jobs>
        <spring:beans id="spring" xmlns:spring="oddjob:spring">
          <classLoader>
            <url-class-loader>
              <files>
                <file file="${this.dir}/classes" />
              </files>
              <parent>
                <value value="${spring.class.classLoader}" />
              </parent>
            </url-class-loader>
          </classLoader>
          <resources>
            <value value="classpath:config/spring/Simple.xml" />
          </resources>
        </spring:beans>
        <run job="${spring/hello}" />
      </jobs>
    </sequential>
  </job>
</oddjob>

Not convinced? Really? OK – I agree, more XML is the last thing you want. It might, however, be worth it if your Spring App is on a remote server. Launching it from Oddjob can give you a quick (but crude) view into it, like this:

Remote Spring Running In OddjobHere we see our HelloWorldBean provided by Spring but run by Oddjob. We can see that it has run and we can see the value of its only property, the runCount, which is how many times it has run.

Oddjob uses JMX to provide the client/server bit, so it was simply a case of launching Oddjob with remote JMX enabled, like this:

java -Dcom.sun.management.jmxremote.port=13013
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-jar c:\Users\rob\java\oddjob\run-oddjob.jar -f Server.oj.xml

And then running the client Oddjob with this configuration:

<oddjob>
  <job>
    <jmx:client connection="localhost:13013" xmlns:jmx="http://rgordon.co.uk/oddjob/jmx" />
  </job>
</oddjob>

The Spring Oddball (An Oddball is an Oddjob plugin) is fairly experimental at the moment. The latest snapshot can be downloaded from SourceForge here. The source code is on GitHub here.

All that remains now is for me to ask – Does Oddjob look like a good enough reason for more XML with Spring?

Comments are closed.