Using the Tibco Hawk Console API to deploy Rulebases with Groovy/Ant.
Facts - Tibco
Wednesday, 30 July 2008 22:00

I use groovy to call java code from Ant and Ant is called from the Hudson or Jenkins web interface.

Recently I wrote some java classes deploying so-called rulebases on Tibco Hawk agents. With these java classes I can deploy and undeploy Hawk rulebases from Hudson. Businessworks processes (ear-files) can be deployed with Hudson as well. In fact before each redeployment of a businessworks ear file its Hawk rulebase is undeployed and after the deployment the Hawk Rulebase is generated from a template for the specific component and environment and deployed again.

This java program has a main thread from which the Tibco Hawk Console API is invoked. The API creates one or more threads to communicate with the Hawk Agents. The main thread schedules methods that deploy rulebases on Hawk agents. Whenever the objects from the Tibco Hawk Console API discover a Hawk Agent and its rulebase methods a callback is executed deploying the Hawk rulebases.

It takes some time for the Tibco API to discover the Hawk Agents and their so-called microagent methods. therefore the main thread of the java program finishes almost instantly, well before the thread in the Tibco Hawk Console API. When executing the code directly from java this was no problem, and neither when executing the code directly from the Groovy console.

However when calling the java code via a groovy task from an Ant built script I encountered a problem. The groovy/ant task did not wait for the last thread to finish, instead it finished immediately after the main thread finished. I tried to prevent this by installing the latest groovy release (1.6beta) and using groovy ant task attributes such as force="true", but that did not help.

The solution was setting a property in the Hawk thread indication whether all scheduled rulebase microagent methods had been executed. The main thread is able to use this property after scheduling the rulebase methods by waiting as follows:

 
....
HawkConsoleManager hc = new HawkConsole(...);
for (rulebase : RulebaseSet) {
 ....
 hc.addRulebase(rulebase);
}
hc.scheduleLastRulebaseOperation();
// Prevents the Groovy Ant task to exit before the rulebase microagent has been discovered 
// by theHawkConsole. 
while (hc.hasQueuedMethods()) {
   Thread.sleep(1000);
}
 

The methods HawkConsole.addRulebase and HawkConsole.scheduleLastRulebaseOperation store rulebase methods in a queue. When the HawkConsole object discovers the rulebase microagent method it executes the methods from this queue, that is, it deploys rulebases. After deploying a rulebase it will remove it from the queue. While deploying the rulebases it also checks for a special method in the queue, that has been added with the method HawkConsole.scheduleLastRulebaseOperation. If it encounters this method then it will stop deploying rulebases, remove all methods from the queue and clean up the thread for the communication with the Hawk Agent. The check HawkConsole.hasQueuedMethods is just a check on size of the queue. If this queue is empty all rulebases have been deployed and the LastRulebaseOperation has been removed from the queue as well. This mean that the hawk thread has been completed and therefore the main thread can continue (and complete) as well.