summaryrefslogtreecommitdiffstats
path: root/gearman/controllerWorker/ControllerWorker/PoolctrlRequest.java
blob: 322cb3d00ce62e3c0369adcfdd0f012084edccaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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"));
			this.reportURL = new URL("http://"
					+ ControllerWorkerMain.getApplication_ini().getString(
							"poolctrl.host")
					+ ControllerWorkerMain.getApplication_ini().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());
		}
	}
}