Helper scripts for Glassfish/OpenESB and SunMQ

22nd December 2009, 1302

A couple of weeks ago I finally spent the time to set up my cygwin environment at work to just the way I want it. As part of the process I threw together two bash scripts to help me when interacting with Glassfish server/OpenESB, making short aliases for common commands and providing some helpful colourisation. I thought I would share them in case they prove useful to anyone else.

Here's the first script for starting and stopping glassfish and colourising the default log file, I have also tested this on OSX but you need to use GNU Sed from MacPorts for the colourisation of the log file to work.

#!/bin/bash

#
# Variables
#

GLASSFISH_HOME="/cygdrive/c/Sun/AppServer"
SED_COMMAND="sed" #May be different on OS X for example, you need Gnu SED
PASSFILE="${HOME}/.caps_passfile"

#
# Statics
#
ASADMIN="${GLASSFISH_HOME}/bin/asadmin.bat"

#
# Functions
#

start-domain() {
  local domain="$1"
  if [ ${#domain} -eq 0 ]; then
    domain="domain1"
  fi
  ${ASADMIN} start-domain $domain
}

stop-domain() {
  local domain="$1"
  if [ ${#domain} -eq 0 ]; then
    domain="domain1"
  fi
  ${ASADMIN} stop-domain $domain
}

less-log() {
  local DOMAIN="$1"
  if [ ${#DOMAIN} -eq 0 ]; then
    DOMAIN="domain1"
  fi
  tail -F -n20000 ${GLASSFISH_HOME}/domains/${DOMAIN}/logs/server.log | \
    ${SED_COMMAND} -u \
      -e 's/^.*SEVERE.*$/&/' \
      -e 's/^.*WARNING.*$/&/' \
      > /tmp/colors.log | \
    less -fR /tmp/colors.log

}

#
# main()
#

command=$1
shift

case "$command" in
  'start')
    start-domain
    ;;
  'stop')
    stop-domain
    ;;
  'restart')
    stop-domain
    start-domain
    ;;
  'log')
    less-log
    ;;
  *)
    echo "Usage: $SELF start|stop|restart|log"
    exit 1
    ;;
esac

I named the script "gf" and placed it on my path, so I can use it as follows

$ gf start
$ gf log
$ gf stop

The Sun MQ script, this has colourisation of the imqcmd list dst command so that you can quickly see which queues have messages and which have un-ack'd messages.

#!/bin/bash

#
# Variables
#

MQ_HOME="/cygdrive/c/Program Files/Sun/MessageQueue/mq"
SED_COMMAND="sed" #May be different on OS X for example, you need Gnu SED
PASSFILE="${HOME}/.caps_passfile"

#
# Statics
#
IMQ_BROKERD="${MQ_HOME}/bin/imqbrokerd.exe"
IMQ_CMD="${MQ_HOME}/bin/imqcmd.exe"

#
# Functions
#

start-mq() {
  "${IMQ_BROKERD}"
}

do-with-imqcmd() {
  "${IMQ_CMD}" -u admin -passfile `cygpath -dm ${PASSFILE}` $@
}

list-destinations() {
  do-with-imqcmd list dst | awk '{print (NF == 8 && $7 != 0) ? "" $0 "" : (NF == 8 && $6 != 0) ? "" $0 "" : $0}'
}

purge-destination() {
  do-with-imqcmd purge dst -n "$1" -t q
}

#
# main()
#

command=$1
shift

case "$command" in
  'start')
    start-mq
    ;;
  'list')
    list-destinations
    ;;
  'purge')
    purge-destination "$1"
    ;;
  *)
    echo "Usage: $SELF start|list|purge"
    exit 1
    ;;
esac

I named this script q and can therefore use it as follows;

$ q start &
$ q list
$ q purge my_broken_queue

This Blog represents my personal views and experiences which are not necessarily the same as those of my employer.