Skip to content

Commit 5d0cc5c

Browse files
committed
handle argument
1 parent bc5f27d commit 5d0cc5c

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

pulse.sample.webtime/samples.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
http://www.naver.com
2+
http://www.daum.net
3+
http://www.facebook.com
4+
http://www.nate.com
5+
http://www.yahoo.com
6+
http://www.youtube.com
7+
http://www.google.com

pulse.sample.webtime/src/main/java/scouter/pulse/webtime/App.java

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package scouter.pulse.webtime;
22

3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
36
import java.net.InetAddress;
7+
import java.util.ArrayList;
48
import java.util.HashMap;
9+
import java.util.List;
510
import java.util.concurrent.Executors;
611
import java.util.concurrent.ScheduledExecutorService;
712
import java.util.concurrent.TimeUnit;
@@ -23,37 +28,68 @@
2328
public class App
2429
{
2530

26-
public final static String[] TARGET_SITES = {
31+
public final static String[] SAMPLE_SITES = {
2732
"http://www.google.co.kr",
2833
"http://www.google.com",
2934
"http://www.google.co.uk",
3035
"http://www.google.co.jp",
31-
"http://www.google.co.be", // Belgium
32-
"http://www.google.com.br", // Brazil
36+
"http://www.google.co.be", // Belgium Google
37+
"http://www.google.com.br", // Brazil Google
3338
"http://www.google.de",
34-
"http://www.google.co.za", // South Africa
39+
"http://www.google.co.za", // SouthAfrica Google
3540
};
3641

3742
static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
38-
43+
44+
static String ip = "127.0.0.1";
45+
static String port = "6180";
46+
47+
static List<String> siteList = null;
3948

4049
public static void main( String[] args ) throws Exception
4150
{
4251

52+
if (args != null && args.length > 0) {
53+
ip = args[0];
54+
if (args.length > 1) {
55+
port = args[1];
56+
if (args.length > 2) {
57+
String filename = args[2];
58+
File f = new File(filename);
59+
if (f.canRead()) {
60+
BufferedReader br = new BufferedReader(new FileReader(f));
61+
String line = null;
62+
siteList = new ArrayList<String>();
63+
while ((line = br.readLine()) != null) {
64+
siteList.add(line);
65+
}
66+
br.close();
67+
}
68+
}
69+
}
70+
}
71+
72+
if (siteList == null) {
73+
siteList = new ArrayList<String>();
74+
for (String site : SAMPLE_SITES) {
75+
siteList.add(site);
76+
}
77+
}
78+
4379
int status = registerMetadata();
4480

4581
if (status == HttpStatus.SC_CREATED) {
4682

4783
Runnable r = new Runnable() {
48-
HashMap<String, InternetObject> statusMap = new HashMap<String, InternetObject>();
84+
HashMap<String, WebObject> statusMap = new HashMap<String, WebObject>();
4985

5086
public void run() {
5187
sendCounterData();
52-
for (int i = 0; i < TARGET_SITES.length; i++) {
53-
final String site = TARGET_SITES[i];
54-
InternetObject obj = statusMap.get(site);
88+
for (int i = 0; i < siteList.size(); i++) {
89+
final String site = siteList.get(i);
90+
WebObject obj = statusMap.get(site);
5591
if (obj == null) {
56-
obj = new InternetObject();
92+
obj = new WebObject();
5793
obj.site = site;
5894
statusMap.put(site, obj);
5995
}
@@ -63,13 +99,13 @@ public void run() {
6399
Unirest.get(site).asStringAsync(new Callback<String>() {
64100

65101
public void failed(UnirestException e) {
66-
InternetObject obj = statusMap.get(site);
102+
WebObject obj = statusMap.get(site);
67103
obj.endTime = System.currentTimeMillis();
68104
obj.status = WebStatusEnum.FAILED;
69105
}
70106

71107
public void completed(HttpResponse<String> response) {
72-
InternetObject obj = statusMap.get(site);
108+
WebObject obj = statusMap.get(site);
73109
obj.endTime = System.currentTimeMillis();
74110
obj.status = WebStatusEnum.DONE;
75111
}
@@ -86,7 +122,7 @@ private void sendCounterData() {
86122
try {
87123
JSONArray jsonArray = new JSONArray();
88124
for (String key : statusMap.keySet()) {
89-
InternetObject obj = statusMap.get(key);
125+
WebObject obj = statusMap.get(key);
90126
JSONObject element = null;
91127
JSONObject objectJson = null;
92128
JSONArray countersArray = null;
@@ -103,7 +139,7 @@ private void sendCounterData() {
103139
countersArray = new JSONArray();
104140
element.put("counters", countersArray);
105141
counterJson = new JSONObject();
106-
counterJson.put("name", "WebTime");
142+
counterJson.put("name", "Time");
107143
counterJson.put("value", obj.endTime - obj.startTime);
108144
countersArray.put(counterJson);
109145
obj.status = WebStatusEnum.READY;
@@ -134,7 +170,7 @@ private void sendCounterData() {
134170
}
135171
}
136172

137-
HttpResponse<JsonNode> response = Unirest.post("http://127.0.0.1:6180/counter")
173+
HttpResponse<JsonNode> response = Unirest.post("http://" + ip + ":" + port + "/counter")
138174
.header("accept", "application/json")
139175
.header("content-type", "application/json")
140176
.body(jsonArray).asJson();
@@ -152,17 +188,16 @@ private static int registerMetadata() throws UnirestException {
152188
JSONObject rootJson = new JSONObject();
153189
JSONObject objectJson = new JSONObject();
154190
objectJson.put("type", "website");
155-
objectJson.put("display", "WebSite");
191+
objectJson.put("display", "Web");
156192
rootJson.put("object", objectJson);
157193
JSONArray counterArray = new JSONArray();
158194
rootJson.put("counters", counterArray);
159195
JSONObject counterMap = new JSONObject();
160196
counterArray.put(counterMap);
161-
counterMap.put("name", "WebTime");
197+
counterMap.put("name", "Time");
162198
counterMap.put("unit", "ms");
163199

164-
165-
HttpResponse<JsonNode> response = Unirest.post("http://127.0.0.1:6180/register")
200+
HttpResponse<JsonNode> response = Unirest.post("http://" + ip + ":" + port + "/register")
166201
.header("accept", "application/json")
167202
.header("content-type", "application/json")
168203
.body(rootJson).asJson();

pulse.sample.webtime/src/main/java/scouter/pulse/webtime/InternetObject.java renamed to pulse.sample.webtime/src/main/java/scouter/pulse/webtime/WebObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scouter.pulse.webtime;
22

3-
public class InternetObject {
3+
public class WebObject {
44
public String site;
55
WebStatusEnum status = WebStatusEnum.READY;
66
long startTime;

0 commit comments

Comments
 (0)