05 March 2008

Starting OC4J from Ant

Good question today from a colleague in Dublin, who basically wanted to know how to start an OC4J instance from Ant using the oc4j:restart.

On the surface that looks like a reasonable thing to do. The problem though is that the OC4J Ant tasks actually require a connection to be made to the server in order to execute the operation -- so if the OC4J instance is down, you can't issue a restart since you can't connect to it.

For situations where the OC4J instance is being managed by OPMN, then one possible solution to this is to call out to the opmnctl executable and ask it to start the OC4J instance.

To do this, you can make use of the ant:exec task as shown below:

<project name="opmn" basedir="." default="status">
<property name="oracle.home" value="d:/oracleas/10131/ohs_j2ee"/>
<property name="opmn.cmd" value="${oracle.home}/opmn/bin/opmnctl.exe"/>

<target name="base" description="Verify callout to opmnctl">
<exec executable="${opmn.cmd}"/>

<target name="status" description="Show the current status">
<exec executable="${opmn.cmd}">
<arg line="status"/>

<!-- Start the oc4j.name instance-->
<target name="start-oc4j" description="Start the oc4j.name instance" depends="get-oc4j-name">
<echo message="${opmn.cmd} startproc process-type=${oc4j.name}"/>
<exec executable="${opmn.cmd}">
<arg line="startproc process-type=${oc4j.name}"/>
<antcall target="status"/>

<target name="get-oc4j-name" unless="oc4j.name">
<input message="Specify OC4J instance: " addproperty="oc4j.name"/>

When this executes, it prompts for the OC4J instance name, calls opmnctl to execute a startproc on it and then displays the status.

>ant -f opmn.xml start-oc4j
Buildfile: opmn.xml

[input] Specify OC4J instance:

[echo] d:/oracleas/10131/ohs_j2ee/opmn/bin/opmnctl.exe startproc process-type=fred
[exec] opmnctl: starting opmn managed processes...


[exec] Processes in Instance: ohs_j2ee_changed
[exec] ---------------------------------+--------------------+---------+---------
[exec] ias-component | process-type | pid | status
[exec] ---------------------------------+--------------------+---------+---------
[exec] OC4JGroup:default_group | OC4J:foo | 2128 | Alive
[exec] OC4JGroup:default_group | OC4J:fred | 6012 | Alive
[exec] OC4JGroup:default_group | OC4J:home | N/A | Down