Groovy URL example to check if web application is alive

Here is a handy KeepAlive.groovy script that will simply connect to a web application to make sure it’s alive.  The database connection for the web application would often timeout because of lack of activity, especially during non-work hours. By periodically making a request to the web application the database connection will remain active.

KeepAlive.groovy

System.out.println(“Start KeepAlive.groovy”);

def urlList = [‘http://server1Name/appName/controller/action?id=xyz’,
‘http://server2Name/appName/controller/action?id=xyz’]

Date currentDate = new Date();
File f = new File(“%LOGDIR%/KeepAlive.out”);
f.append(“\n”);
f.append(currentDate);
f.append(“\n”);

// Send data
URL url;
URLConnection conn;
BufferedReader rd;
String line;

try {

for (urlString in urlList) {
System.out.println(“Try using URL..: ” + urlString);
url = new URL(urlString);
conn = url.openConnection();
conn.setDoOutput(true);

// Get the response
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
// Process line…
f.append(line);
}
rd.close();
f.append(“\n”);
}
f.close();
} catch (Exception e) {
System.out.println(“Exception: ” + e.getMessage());
}

System.out.println(“Finished KeepAlive.groovy”);

About admin

Richard has worked in the Information Technology field for 25 years on a variety of platforms and lines of business.
This entry was posted in Groovy Grails. Bookmark the permalink.