Java tutorial
/* * Copyright 2014 arnab. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.arnabk; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.lang3.StringUtils; import org.apache.http.client.fluent.Request; /** * This is a blocking call and the caller will be waiting forever after this call. This class will continuously check the hostsToCheck status after * frequencyInMins interval has elapsed * * This class will also prepend http if it has not been already added to the website URL * * @author arnab */ public class BlockingChecker { public void execute(int frequencyInMins, String... hostsToCheck) { while (true) { for (int index = 1; index < hostsToCheck.length; index++) { StringBuilder sb = new StringBuilder(); sb.append("Timestamp : ").append(new Date()).append(", ").append("Server : ") .append(hostsToCheck[index]).append(", ").append("Status code : "); try { sb.append(Request.Head(prepareURL(StringUtils.trimToEmpty(hostsToCheck[index]))).execute() .returnResponse().getStatusLine().getStatusCode()); } catch (Exception e) { sb.append(" Request failed!"); } System.out.println(sb); } try { synchronized (this) { wait(frequencyInMins * 60 * 1000); } } catch (InterruptedException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } } } private String prepareURL(String url) { if (url.startsWith("http")) { return url; } else { return "http://" + url; } } }