Oddjob as a Windows Service

I  previously created an email reminder for my Mum’s birthday. For Oddjob to send the email it has to be running, and as I don’t want to start it every time I switch on my computer it’s really got to run in the back ground. On Windows this means running it as a Windows Service. Here’s how I set it up.

First I had to choose a wrapper which provides the executable that Windows needs. I found three choices for this:

I chose the JavaService mainly becuase I’ve used it before and I remembered it’s configuration as being quite simple.

Note – there is a 32bit and 64bit version – make sure you use the correct one for your target platform!

The JavaService.exe is the thing that windows runs, and what appears in the name column of Task Manager, so it’s best to name it something you’ll recognise. I copied it and renamed it Oddjob.exe.

Now here’s where I could list the commands to install and start an Oddjob service, but Oddjob itself can be used as an easy way of keeping a collection of commands together for re-use at a later date. So I created a new Oddjob for the commands. It looks like this:

Oddjob Service Commands

Oddjob Service Commands

Here’s the Oddjob XML configuration for this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<oddjob id="this">
    <job>
        <sequential>
            <jobs>
                <properties>
                    <values>
                        <file file="c:\rob\java\JavaService-2.0.10" key="javaservice.home"/>
                        <file file="${javaservice.home}\OddjobService.exe" key="javaservice.exe"/>
                        <file file="c:\rob\java\oddjob" key="use.oddjob.home"/>
                        <file file="${use.oddjob.home}\run-oddjob.jar" key="use.oddjob.runjar"/>
                        <file file="${this.dir}" key="ojobs.dir"/>
                        <file file="${ojobs.dir}" key="use.cwd"/>
                        <file file="${java.home}\bin\server\jvm.dll" key="java.dll"/>
                        <value key="use.service.name" value="OddjobService"/>
                        <buffer key="install.service.command"><![CDATA["${javaservice.exe}"
-install ${use.service.name}
"${java.dll}" 
"-Djava.class.path=${use.oddjob.runjar}"
-Dcom.sun.management.jmxremote.port=3333
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-start org.oddjob.launch.Launcher 
-params -f service-config.xml -l log4j.properties
-out oddjob-service.log
-err oddjob-service.log
-current "${use.cwd}"
-description "Oddjob Scheduler Service"]]></buffer>
                        <buffer key="uninstall.service.command"><![CDATA["${javaservice.exe}"
-uninstall ${use.service.name}
]]></buffer>
                    </values>
                </properties>
                <exists file="${javaservice.exe}" name="Check JavaService exe"/>
                <folder>
                    <jobs>
                        <exec name="Install"><![CDATA[cmd /c ${install.service.command}]]></exec>
                        <exec name="Start Oddjob Service"><![CDATA[cmd /c net start ${use.service.name}]]></exec>
                        <exec name="Uninstall"><![CDATA[cmd /c ${uninstall.service.command}]]></exec>
                        <exec name="Stop Oddjob Service"><![CDATA[cmd /c net stop ${use.service.name}]]></exec>
                        <exec name="Query"><![CDATA[${javaservice.exe} -queryconfig ${use.service.name}]]></exec>
                    </jobs>
                </folder>
            </jobs>
        </sequential>
    </job>
</oddjob>

Oddjob needs to run with administrator privileges for these commands. This can either be done either by right-clicking the oddjob.bat file and selecting ‘Run as Administrator’ or creating a shortcut and setting the Run as Administer property in the  Advanced section.

Creating an Oddjob Shortcut with Admin Privs

Creating an Oddjob Shortcut with Admin Privs

When the service is running I’d like to see what it’s doing without needing to check the log file oddjob-service.log. This is easily achieved by wrapping my birthday Oddjob in another Oddjob that provides a JMX server. For more information on this please se the User Guide Sharing Jobs on a Network but for brevity I’m just going to give you the XML I used.

 

<oddjob id="oddjob">
    <job>
        <sequential id="root"
                    name="main">
            <jobs>
                <jmx:server xmlns:jmx="http://rgordon.co.uk/oddjob/jmx"
                        id="server"
                        name="Oddjob Server"
                        root="${server-jobs}"/>
                <oddjob id="server-jobs"
                        name="Server Jobs"
                        file="${oddjob.dir}/mums-birthday.xml"/>
            </jobs>
        </sequential>
    </job>
</oddjob>

And then I can start the service by running my Oddjob ‘Start Oddjob Service’ task and connect to it using a new Oddjob Explorer window and an Oddjob client job.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<oddjob>
    <job>
        <jmx:client id="client1" name="Client1" url="service:jmx:rmi:///jndi/rmi://localhost:3333/jmxrmi" xmlns:jmx="http://rgordon.co.uk/oddjob/jmx"/>
    </job>
</oddjob>

And here is my birthday reminder running as a service:

Oddjob Service And Client

Oddjob Service And Client

Comments are closed.