Skip to content

Commit 5dd1c76

Browse files
committed
sample
1 parent 2c121f5 commit 5dd1c76

File tree

6 files changed

+84
-68
lines changed

6 files changed

+84
-68
lines changed

pulse.sample.bizcounter/java-sample/pulse.sample.bizcounter.iml

Lines changed: 0 additions & 23 deletions
This file was deleted.

pulse.sample.bizcounter/java-sample/src/main/java/scouterx/pulse/http/HttpTrain.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package scouterx.pulse.http;
22

3+
import retrofit2.Call;
4+
import retrofit2.Callback;
5+
import retrofit2.Response;
36
import retrofit2.Retrofit;
47
import retrofit2.converter.gson.GsonConverterFactory;
58
import scouterx.pulse.protocol.counter.ObjectCounterBean;
69
import scouterx.pulse.protocol.register.RegisterBean;
7-
import scouterx.pulse.sample.bizcounter.App;
810

911
import java.util.List;
1012
import java.util.concurrent.BlockingQueue;
11-
import java.util.concurrent.LinkedBlockingDeque;
13+
import java.util.concurrent.LinkedBlockingQueue;
1214

1315
/**
1416
* @author Gun Lee (gunlee01@gmail.com) on 2016. 7. 31.
@@ -17,6 +19,10 @@ public class HttpTrain extends Thread {
1719
static final String TYPE_REGISTER = "R";
1820
static final String TYPE_COUNTER_ARRAY = "A";
1921
static final String TYPE_COUNTER_LIST = "L";
22+
public static final String ENV_KEY_TARGETADDR = "pulse.targetaddr";
23+
public static final String DEFAULT_TARGETADDR = "http://localhost:6180/";
24+
25+
private static ScouterPulseService service;
2026

2127
class Bucket {
2228
String type;
@@ -28,18 +34,12 @@ class Bucket {
2834
}
2935
}
3036

31-
static BlockingQueue<Bucket> queue = new LinkedBlockingDeque<>(100);
32-
33-
static Retrofit retrofit = null;
37+
static BlockingQueue<Bucket> queue = new LinkedBlockingQueue<>(100);
3438

3539
private static HttpTrain instance = null;
3640

3741
public final static synchronized HttpTrain getInstance() {
3842
if (instance == null) {
39-
retrofit =new Retrofit.Builder()
40-
.baseUrl(App.targetAddress)
41-
.addConverterFactory(GsonConverterFactory.create())
42-
.build();
4343
instance = new HttpTrain();
4444
instance.setDaemon(true);
4545
instance.setName(instance.getClass().getName());
@@ -55,21 +55,50 @@ private HttpTrain() {
5555
public void run() {
5656
while (true) {
5757
Object o = null;
58-
queue.stream().forEach(bucket -> {
58+
59+
try {
60+
Bucket bucket = queue.take();
5961
switch(bucket.type) {
6062
case TYPE_REGISTER:
63+
getService().register((RegisterBean)bucket.obj).enqueue(callback);
6164
break;
6265
case TYPE_COUNTER_ARRAY:
66+
getService().counter((ObjectCounterBean[]) bucket.obj).enqueue(callback);
6367
break;
6468
case TYPE_COUNTER_LIST:
69+
getService().counter((List<ObjectCounterBean>) bucket.obj).enqueue(callback);
6570
break;
6671
default:
6772
break;
6873
}
69-
});
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
7077
}
7178
}
7279

80+
Callback callback = new Callback() {
81+
@Override
82+
public void onResponse(Call call, Response response) {
83+
System.out.println("[SUCCESS]" + response.code());
84+
}
85+
86+
@Override
87+
public void onFailure(Call call, Throwable throwable) {
88+
System.out.println("[FAIL]" + throwable.getMessage());
89+
}
90+
};
91+
92+
private synchronized static ScouterPulseService getService() {
93+
if(service == null) {
94+
Retrofit client =new Retrofit.Builder()
95+
.baseUrl(System.getProperty(ENV_KEY_TARGETADDR, DEFAULT_TARGETADDR))
96+
.addConverterFactory(GsonConverterFactory.create())
97+
.build();
98+
service = client.create(ScouterPulseService.class);
99+
}
100+
return service;
101+
}
73102

74103
public boolean putRegisterBean(RegisterBean bean) {
75104
return queue.offer(new Bucket(TYPE_REGISTER, bean));
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package scouterx.pulse.http;
22

3-
import retrofit2.Response;
3+
import retrofit2.Call;
44
import retrofit2.http.Body;
55
import retrofit2.http.Headers;
66
import retrofit2.http.POST;
7-
import scouterx.pulse.protocol.counter.ObjectValue;
7+
import scouterx.pulse.protocol.counter.ObjectCounterBean;
88
import scouterx.pulse.protocol.register.RegisterBean;
99

1010
import java.util.List;
@@ -14,14 +14,14 @@
1414
*/
1515
public interface ScouterPulseService {
1616
@POST("/register")
17-
@Headers({"Accept: application/json", "Content-Yype: application/json"})
18-
Response register(@Body RegisterBean bean);
17+
@Headers({"accept: application/json", "content-type: application/json"})
18+
Call<Void> register(@Body RegisterBean bean);
1919

2020
@POST("/counter")
21-
@Headers({"Accept: application/json", "Content-Yype: application/json"})
22-
Response counter(@Body ObjectValue[] objs);
21+
@Headers({"accept: application/json", "content-type: application/json"})
22+
Call<Void> counter(@Body ObjectCounterBean[] objs);
2323

2424
@POST("/counter")
25-
@Headers({"Accept: application/json", "Content-Yype: application/json"})
26-
Response counter(@Body List<ObjectValue> objs);
25+
@Headers({"accept: application/json", "content-type: application/json"})
26+
Call<Void> counter(@Body List<ObjectCounterBean> objs);
2727
}

pulse.sample.bizcounter/java-sample/src/main/java/scouterx/pulse/sample/bizcounter/App.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scouterx.pulse.sample.bizcounter;
22

33
import org.apache.commons.cli.*;
4+
import scouterx.pulse.http.HttpTrain;
45

56
/**
67
* @author Gun Lee (gunlee01@gmail.com) on 2016. 7. 30.
@@ -11,7 +12,7 @@ public class App {
1112
public static void main(String[] args) {
1213
Options options = new Options();
1314

14-
Option address = new Option("t", "target", true, "target address, default => http://localhost:6180/");
15+
Option address = new Option("t", "target", true, "target address, default => " + HttpTrain.DEFAULT_TARGETADDR);
1516
address.setRequired(false);
1617
options.addOption(address);
1718

@@ -28,8 +29,7 @@ public static void main(String[] args) {
2829
System.exit(1);
2930
return;
3031
}
31-
32-
App.targetAddress = cmd.getOptionValue("target", "http://localhost:6180/");
32+
System.setProperty(HttpTrain.ENV_KEY_TARGETADDR, cmd.getOptionValue("target", HttpTrain.DEFAULT_TARGETADDR));
3333

3434
BizSampleAgent.getInstance().start();
3535

pulse.sample.bizcounter/java-sample/src/main/java/scouterx/pulse/sample/bizcounter/BizDataController.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
public class BizDataController {
1616
private static Map<String, Float> prev = new HashMap<>();
17+
private static Map<String, Float> prev2 = new HashMap<>();
1718

1819
static {
1920
prev.put("ops1", 100f);
@@ -22,6 +23,12 @@ public class BizDataController {
2223
prev.put("ops4", 400f);
2324
prev.put("ops5", 500f);
2425

26+
prev2.put("ops1", 100f);
27+
prev2.put("ops2", 200f);
28+
prev2.put("ops3", 300f);
29+
prev2.put("ops4", 400f);
30+
prev2.put("ops5", 500f);
31+
2532
prev.put("onairopm", 700f);
2633
prev.put("onairapm", 200f);
2734
prev.put("onaircpm", 34.5f);
@@ -33,18 +40,22 @@ public List<ObjectCounterBean> getRealtimeOrderInfoAsObjectCounterBean() {
3340
list.add(new ObjectCounterBean.Builder()
3441
.setObject(new ObjectValue("product-info", "prod-1001", BizSampleAgent.OBJECT_TYPE, "noaddress"))
3542
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_OPM, getRandomSample("ops1")))
43+
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_DAYORDER, getRandomSample2("ops1d")))
3644
.build());
3745
list.add(new ObjectCounterBean.Builder()
3846
.setObject(new ObjectValue("product-info", "prod-1021", BizSampleAgent.OBJECT_TYPE, "noaddress"))
3947
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_OPM, getRandomSample("ops2")))
48+
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_DAYORDER, getRandomSample2("ops2d")))
4049
.build());
4150
list.add(new ObjectCounterBean.Builder()
4251
.setObject(new ObjectValue("product-info", "prod-1139", BizSampleAgent.OBJECT_TYPE, "noaddress"))
4352
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_OPM, getRandomSample("ops3")))
53+
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_DAYORDER, getRandomSample2("ops3d")))
4454
.build());
4555
list.add(new ObjectCounterBean.Builder()
4656
.setObject(new ObjectValue("product-info", "prod-1240", BizSampleAgent.OBJECT_TYPE, "noaddress"))
4757
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_OPM, getRandomSample("ops4")))
58+
.addCounterValue(new CounterValue(BizSampleAgent.COUNTER_DAYORDER, getRandomSample2("ops4d")))
4859
.build());
4960

5061
list.add(new ObjectCounterBean.Builder()
@@ -64,7 +75,28 @@ private float getRandomSample(String type) {
6475
prev.put(type, value);
6576
}
6677

67-
value = value + ((float) Math.random() - 0.5f) * value / 30;
78+
if(value <= 50) {
79+
value = value + ((float) Math.random() - 0.1f) * value / 20;
80+
} else if(value <= 200) {
81+
value = value + ((float) Math.random() - 0.3f) * value / 10;
82+
} else {
83+
value = value + ((float) Math.random() - 0.5f) * value / 5;
84+
}
85+
86+
prev.put(type, value);
87+
88+
return value;
89+
}
90+
91+
private float getRandomSample2(String type) {
92+
Float value = prev2.get(type);
93+
if (value == null) {
94+
value = 10.0f;
95+
prev.put(type, value);
96+
}
97+
98+
value = value + (float) Math.random() * value / 20;
99+
prev2.put(type, value);
68100

69101
return value;
70102
}

pulse.sample.webtime/pulse.sample.webtime.iml

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)