summaryrefslogblamecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/PoolctrlRequest.java
blob: 0aa003c9ada5e1b77bfe512f1d63c201a19f2836 (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.getApplication_ini()
				.getString("event.updatePeriod")) * 60L * 1000000000L; // nanoseconds
		try {
			this.runURL = new URL("http://"
					+ ControllerWorkerMain.getApplication_ini().getString(
							"poolctrl.host")
					+ ControllerWorkerMain.getApplication_ini().getString(
							"poolctrl.runevent") + "apikey1");
			this.reportURL = new URL("http://"
					+ ControllerWorkerMain.getApplication_ini().getString(
							"poolctrl.host")
					+ ControllerWorkerMain.getApplication_ini().getString(
							"poolctrl.reportevent") + "apikey1");
		} 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) 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());
			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());
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}
}