com.envisioniot.sub.client.demo.DataServiceDemo Maven / Gradle / Ivy
package com.envisioniot.sub.client.demo;
import com.envisioniot.sub.client.EosClient;
import com.envisioniot.sub.client.data.IDataHandler;
import com.envisioniot.sub.client.data.IDataService;
import com.envisioniot.sub.common.exception.SubscribeException;
import com.envisioniot.sub.common.model.InternalMeasurePoint;
import com.envisioniot.sub.common.model.dto.MeasurePoint;
import com.envisioniot.sub.common.model.dto.StreamMessage;
import com.envisioniot.sub.common.utils.CommonUtils;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
public class DataServiceDemo {
private static final Logger LOG = Logger.getLogger(DataServiceDemo.class);
public static void main(String[] args) throws SubscribeException {
String host = args[0];
int port = Integer.parseInt(args[1]);
String accessKey = args[2];
String secret = args[3];
String subId = args[4];
String cg = args.length >= 6 ? args[5] : null;
EosClient client = new EosClient(
host,
port,
accessKey,
secret);
client.setRequestTimeout(10000);
final Map map = new HashMap<>();
LOG.info(Arrays.deepToString(args));
final AtomicLong counter = new AtomicLong(0L);
final AtomicLong total = new AtomicLong(0L);
IDataHandler userHandler = new IDataHandler() {
@Override
public void dataRead(StreamMessage point) {
System.out.println(new Gson().toJson(point));
counter.incrementAndGet();
total.incrementAndGet();
for (MeasurePoint measurePoint : point.getMeasurePoints()) {
if (null == map.get(measurePoint.getAssetId())) {
map.put(measurePoint.getAssetId(), 0);
}
map.put(measurePoint.getAssetId(), map.get(measurePoint.getAssetId())+1);
}
}
};
IDataService dataService = client.getDataService();
dataService.subscribe(userHandler, subId, cg);
CommonUtils.getSingleThreadPool("counter").execute(new Runnable() {
@Override
public void run() {
long last = System.currentTimeMillis();
while (true) {
long now = System.currentTimeMillis();
long escaped = now - last;
LOG.info("Count:\t" + counter.get());
LOG.info("QPS:\t" + counter.get() * 1.0 / (escaped / 1000));
LOG.info("Total:\t" + total.get());
last = now;
counter.set(0L);
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + ">>>" + entry.getValue());
}
try {
Thread.sleep(60000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
}