com.envisioniot.sub.client.demo.AlertServiceDemo Maven / Gradle / Ivy
package com.envisioniot.sub.client.demo;
import com.envisioniot.sub.client.EosClient;
import com.envisioniot.sub.client.event.IAlertHandler;
import com.envisioniot.sub.client.event.IAlertService;
import com.envisioniot.sub.common.model.Alert;
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;
/**
* created by jie.jin on 2018/9/14.
*/
public class AlertServiceDemo {
private static final Logger LOG = Logger.getLogger(AlertServiceDemo.class);
public static void main(String[] args) throws Exception {
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);
LOG.info(Arrays.deepToString(args));
final Map map = new HashMap<>();
final AtomicLong counter = new AtomicLong(0L);
final AtomicLong total = new AtomicLong(0L);
IAlertHandler userHandler = new IAlertHandler() {
@Override
public void alertRead(Alert alert) {
System.out.println(new Gson().toJson(alert));
counter.incrementAndGet();
total.incrementAndGet();
if (null == map.get(alert.getDeviceId())) {
map.put(alert.getDeviceId(), 0);
}
map.put(alert.getDeviceId(), map.get(alert.getDeviceId())+1);
}
};
IAlertService service = client.getAlertService();
service.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();
}
}
}
});
}
}