summaryrefslogblamecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/PoolctrlRequest.java
blob: f35d10c92343980f37076dfe4782d089a9d7494a (plain) (tree)
1
2
3
4
5
6
7
8
9




                                  



                               
                                             
                              

                              

                                        
                                  
                                                          

                                                                                                     
                     
                                                       



                                                                                      
                                                          



                                                                                      
                                       
                                                   
                 

















                                                                                
                                                
                                               
                                                           









                                                    












                                                                                    











                                                                                
                                                   


                 
package ControllerWorker;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.log4j.Logger;

public class PoolctrlRequest extends Thread {
	private Logger logger;
	private URL runURL;
	private URL reportURL;
	private final long updatePeriod;

	public PoolctrlRequest() {
		logger = ControllerWorkerMain.getLogger();
		updatePeriod = Long.parseLong(ControllerWorkerMain.getIniFile()
				.getString("event.updatePeriod")) * 60L * 1000000000L; // nanoseconds
		try {
			this.runURL = new URL("http://"
					+ ControllerWorkerMain.getIniFile().getString(
							"poolctrl.host")
					+ ControllerWorkerMain.getIniFile().getString(
							"poolctrl.runevent"));
			this.reportURL = new URL("http://"
					+ ControllerWorkerMain.getIniFile().getString(
							"poolctrl.host")
					+ ControllerWorkerMain.getIniFile().getString(
							"poolctrl.reportevent"));
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public void run() {
		workerLoop();
	}

	public void workerLoop() {
		long beginTime;
		long timeTaken;
		long timeLeft;
		while (true) {
			try {
				beginTime = System.nanoTime();
				update();
				timeTaken = System.nanoTime() - beginTime;
				timeLeft = (updatePeriod - timeTaken) / 1000000;
				if (timeLeft < 10)
					timeLeft = 10;
				sleep(timeLeft);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
	}

	public void update() {
		HttpURLConnection connection = null;
		BufferedReader rd = null;
		StringBuilder sb = null;
		String line = null;
		try {
			connection = (HttpURLConnection) runURL.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);
			connection.connect();
			rd = new BufferedReader(new InputStreamReader(connection
					.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null) {
				sb.append(line + '\n');
			}
			logger.info(sb.toString());
			connection = (HttpURLConnection) reportURL.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);
			connection.connect();
			rd = new BufferedReader(new InputStreamReader(connection
					.getInputStream()));
			sb = new StringBuilder();

			while ((line = rd.readLine()) != null) {
				sb.append(line + '\n');
			}
			logger.info(sb.toString());
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}
}