home / blog

Waiting for EBSs/WARs in JBoss to deploy using gradle / twiddle

Some home development for an issue at work… Often integration tests fail because the ESBs/WARs fail to deploy, we’d like early warning of this.

The ESBs/WARs are deployed into a JBoss instance using gradle. The deployment gradle script can be enhanced with the following to use twiddle.sh – a built in JBoss JMX client to wait for all ESBs/WARs to be marked as deployed, or timeout after some configurable period. The JMX username/password are loaded directly from the soa-users.properties file.

Example: happy path

gradle -PdeployEsbs=Foo,Bar -PdeployWars=Baz -PdeployTimeoutSecs=10
Monitoring deployment status of ESBs [Foo, Bar] and WARs [Baz]
Foo OK  | Bar x   | Baz x  
Foo OK  | Bar x   | Baz x  
Foo OK  | Bar x   | Baz x  

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/adam/development/gradle/build.gradle' line: 44

* What went wrong:
A problem occurred evaluating root project 'gradle'.
> Timed out waiting for [Bar, Baz] to deploy

Example: sad path

 gradle -PdeployEsbs=Foo,Bar -PdeployWars=Baz -PdeployTimeoutSecs=10
Monitoring deployment status of ESBs [Foo, Bar] and WARs [Baz]
Foo OK  | Bar OK  | Baz x  
All ESBs/WARs are deployed. Go go go

The code:

def deployTimeoutSecs = hasProperty('deployTimeoutSecs') ? property('deployTimeoutSecs').toInteger() : 45
def deployEsbs = hasProperty('deployEsbs') ? property('deployEsbs').tokenize(',') : []
def deployWars = hasProperty('deployWars') ? property('deployWars').tokenize(',') : []
import java.util.concurrent.TimeUnit

println "Monitoring deployment status of ESBs ${deployEsbs} and WARs ${deployWars}"
def timeStart = System.currentTimeMillis()
while (true) {
      def props = new Properties()
      props.load(new FileInputStream('soa-users.properties'))
      def jmxUser = props.stringPropertyNames().toList()[0]
      def jmxPassword = props.getProperty(jmxUser)

      def captureOutputStream = new ByteArrayOutputStream()
      exec {
         ignoreExitValue = true
         standardOutput = captureOutputStream
         executable = './twiddle.sh'
         args = ['-u', jmxPassword, '-p', jmxUser, '-h', 'localhsot:1099', 'query', '*:*']
      }
      def capture = captureOutputStream.toString().tokenize('\n').collect { it.trim() }
      def states = new LinkedHashMap()
      deployEsbs.each {
         states.put(it, capture.contains(new String("jboss.esb.deployment=${it}.esb")))
      }
      deployWars.each {
         states.put(it, capture.contains(new String("jboss.esb.deployment:war=${it}.war")))
      }
      if (states.every { it.value }) {
          println "All ESBs/WARs are deployed. Go go go"
          break
      }

      def report = states.collect {
          return it.key+ ' ' + (it.value ? ':)' : 'x').padRight(3)
      }
      println report.join(' | ')

      if (System.currentTimeMillis() - timeStart > TimeUnit.SECONDS.toMillis(deployTimeoutSecs)) {
          def notDeployed = states.findResults { !it.value ? it.key : null }
          throw new GradleException("Timed out waiting for ${notDeployed} to deploy")
      }
      Thread.sleep(TimeUnit.SECONDS.toMillis(5))
}
This entry was posted in geek and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.