01 October 2016

In part 4 of this article we explored a method for transfering a password from one file to another. Now we are going to use Apache Ant to stop a Liferay server. What are the steps needed to accomplish this goal? First we need to determine if the Liferay server is running. No need to stop it if it isn’t running. Next we need to run the shutdown command. The shutdown command works most of the time. The build file will need to account for those times when it doesn’t. Because I will be using this on a Linux server we will start with the pgrep command.

command line on server



    $ pgrep -lf liferay
    18600 /home/oracle_jre/bin/java -Djava.util.logging.config.file=/home/liferay/apache-tomcat/conf/logging.properties 
    -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
    -Djdk.tls.ephemeralDHKeySize=2048 -server -d64 -Xms16g -Xmx16g 
    -XX:ParallelGCThreads=4 -XX:+TieredCompilation -XX:+UseG1GC -XX:ConcGCThreads=2 
    -XX:+UseCompressedOops -XX:ReservedCodeCacheSize=1024m -XX:+DisableExplicitGC 
    -XX:-UseBiasedLocking -XX:+BindGCTaskThreadsToCPUs -XX:+UseFastAccessorMethods 
    -Dfile.encoding=UTF8 -Dsystem.properties.load=true 
    -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false 
    -Duser.timezone=GMT -XX:HeapDumpPath=/apps01/liferay/data/heapdumps 
    -XX:+HeapDumpOnOutOfMemoryError -Djava.net.preferIPv4Stack=true 
    -Xloggc:/home/liferay/apache-tomcat/logs/gc.log -XX:+PrintClassHistogram 
    -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+DisableExplicitGC 
    -XX:MaxTenuringThreshold=15 -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 
    -XX:GCLogFileSize=100M -Djava.endorsed.dirs=/home/liferay/apache-tomcat/endorsed 
    -classpath /home/liferay/apache-tomcat/bin/bootstrap.jar:/home/liferay/apache-tomcat/bin/tomcat-juli.jar 
    -Dcatalina.base=/home/liferay/apache-tomcat -Dcatalina.home=/home/liferay/apache-tomcat 
    -Djava.io.tmpdir=/home/liferay/apache-tomcat/temp org.apache.catalina.startup.Bootstrap start

The above pgrep command shows that Liferay is running. Using the above results we can construct the following target.

target to detect running Liferay server



    <target name="checkIfLiferayIsRunning">
      <exec executable="pgrep" failonerror="false" outputproperty="result">
        <arg value="-lf"/>
        <arg value="Dcatalina.home=/home/liferay/apache-tomcat"/>
      </exec>
      <condition property="isLiferayRunning" else="false">
        <contains string="${result}" substring="Dcatalina.home=/home/liferay/apache-tomcat"/>
      </condition>
    </target>

Using the exec task we send the pgrep command to the OS and store the result in the “result” property. Then using the condition task we can verify the result contains what it should and set the isLiferayRunning property to true. Otherwise we set it to false. Now the next target can use it to stop the Liferay instance, or not.

target to stop Liferay if running



    <target name="stopLiferayIfRunning" depends="checkIfLiferayIsRunning" if="${isLiferayRunning}">
      <exec executable="/home/liferay/apache-tomcat/bin/shutdown.sh" failonerror="true">
      </exec>
    </target>

The above target depends on our previous target. Which means the checkIfLiferayIsRunning target runs first and then returns to the stopLiferayIfRunning target. However the “if” attribute of the target will prevent it from being run if the “isLiferayRunning” property is not true. This property was set inside the checkIfLiferayIsRunning target. We still need to monitor and make sure it shuts down properly. We can do this by expanding on the target to monitor a log file.

target to stop Liferay if running



    <target name="stopLiferayIfRunning" depends="checkIfLiferayIsRunning" if="${isLiferayRunning}">
      <exec executable="/home/liferay/apache-tomcat/bin/shutdown.sh" failonerror="true">
      </exec>

      <echo message="Waiting for the following log file to appear."/>
      <echo message="/home/liferay/apache-tomcat/logs/catalina.out"/>
      <waitfor maxwait="10" maxwaitunit="second">
    
        <available file="/home/liferay/apache-tomcat/logs/catalina.out"/>
      </waitfor>

      <echo message="Waiting for Liferay server to stop."/>
      <waitfor maxwait="300" maxwaitunit="second">
        <resourcecontains resource="${catalinaLog}" substring="Destroying ProtocolHandler [&quot;http-bio-8080&quot;]"/>
      </waitfor>    
    </target>

The additions verify that the log file is available and monitors it waiting for Apache Tomcat to report that the HTTP protocol handler has been destroyed. This log message lets us know that Tomcat has fully shutdown. What would happen if this message didn’t appear in the log? Eventually the waitfor tasks will timeout. One option would be to follow the waitfor tasks with a fail task to stop Ant from continuing. In this case if Liferay fails to shutdown properly I want to kill the process.

target to kill Liferay process.



    <target name="killLiferayIfRunning" depends="checkIfLiferayIsRunning" if="${isLiferayRunning}">
      <exec executable="pgrep" failonerror="false" outputproperty="pid">
        <arg value="-f"/>
        <arg value="Dcatalina.home=/home/liferay/apache-tomcat"/>
      </exec>

      <exec executable="kill" failonerror="false">
        <arg value="-9"/>
        <arg value="${pid}"/>
      </exec> 
    </target>

The killLiferayIfRunning target is very similar to our stopLiferayIfRunning task. It has the same dependencies and if property. One difference is the pgrep command used only returns the PID of the Liferay process. Another is instead of trying to properly shutdown Liferay we issue a kill command to the OS to terminate the process. In the following build file we introduce a new task. Antcall is used to run targets. The following brings what we have discussed together.

target to kill Liferay process.



    <project name="Stop Liferay" default="stopLiferay" basedir=".">

      <description>
         Stop Liferay Server
      </description>


     <target name="stopLiferay">

        <antcall target="stopLiferayIfRunning"/>
        <antcall target="killLiferayIfRunning"/>
        <antcall target="killLiferayIfRunning"/>
        <antcall target="killLiferayIfRunning"/>
    
      </target>

      <target name="checkIfLiferayIsRunning">
        <exec executable="pgrep" failonerror="false" outputproperty="result">
          <arg value="-lf"/>
          <arg value="Dcatalina.home=/home/liferay/apache-tomcat"/>
        </exec>
        <condition property="isLiferayRunning" else="false">
          <contains string="${result}" substring="Dcatalina.home=/home/liferay/apache-tomcat"/>
        </condition>
      </target>

      <target name="stopLiferayIfRunning" depends="checkIfLiferayIsRunning" if="${isLiferayRunning}">
        <exec executable="/home/liferay/apache-tomcat/bin/shutdown.sh" failonerror="true">
        </exec>

        <echo message="Waiting for the following log file to appear."/>
        <echo message="/home/liferay/apache-tomcat/logs/catalina.out"/>
        <waitfor maxwait="10" maxwaitunit="second">
    
          <available file="/home/liferay/apache-tomcat/logs/catalina.out"/>
        </waitfor>

        <echo message="Waiting for Liferay server to stop."/>
        <waitfor maxwait="300" maxwaitunit="second">
          <resourcecontains resource="${catalinaLog}" substring="Destroying ProtocolHandler [&quot;http-bio-8080&quot;]"/>
        </waitfor>    
      </target>

      <target name="killLiferayIfRunning" depends="checkIfLiferayIsRunning" if="${isLiferayRunning}">
        <exec executable="pgrep" failonerror="false" outputproperty="pid">
          <arg value="-f"/>
          <arg value="Dcatalina.home=/home/liferay/apache-tomcat"/>
        </exec>

        <exec executable="kill" failonerror="false">
          <arg value="-9"/>
          <arg value="${pid}"/>
        </exec> 
      </target>
        
    </project>

Using the antcall task we run the stopLiferayIfRunning target followed by running killLiferayIfRunning target three times. Both targets check to see if Liferay is running. If it is not running, they do nothing. The next part in this article will cover starting a Liferay server and monitoring for a successful startup.