add: 添加CounterService

This commit is contained in:
Wendal Chen 2018-04-16 19:46:39 -07:00
parent c12e632b22
commit a6122d5ae1
4 changed files with 69 additions and 11 deletions

View File

@ -18,14 +18,13 @@ import org.nutz.boot.config.ConfigureLoader;
import org.nutz.boot.config.impl.PropertiesConfigureLoader;
import org.nutz.boot.env.SystemPropertiesEnvHolder;
import org.nutz.boot.ioc.IocLoaderProvider;
import org.nutz.boot.metrics.impl.MemoryCounterService;
import org.nutz.boot.resource.ResourceLoader;
import org.nutz.boot.resource.impl.SimpleResourceLoader;
import org.nutz.boot.tools.NbAppEventListener;
import org.nutz.boot.tools.PropDocReader;
import org.nutz.boot.tools.NbAppEventListener.EventType;
import org.nutz.ioc.Ioc2;
import org.nutz.boot.tools.PropDocReader;
import org.nutz.ioc.IocLoader;
import org.nutz.ioc.ObjectProxy;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.annotation.AnnotationIocLoader;
import org.nutz.ioc.loader.annotation.IocBean;
@ -420,10 +419,11 @@ public class NbApp extends Thread {
}
// 把核心对象放进ioc容器
if (!ctx.ioc.has("appContext")) {
Ioc2 ioc2 = (Ioc2) ctx.getIoc();
ioc2.getIocContext().save("app", "appContext", new ObjectProxy(ctx));
ioc2.getIocContext().save("app", "conf", new ObjectProxy(ctx.getConf()));
ioc2.getIocContext().save("app", "nbApp", new ObjectProxy(this));
ctx.ioc.addBean("appContext", ctx);
ctx.ioc.addBean("conf", ctx.getConf());
ctx.ioc.addBean("nbApp", this);
// 添加更多扩展bean
ctx.ioc.addBean("counterService", new MemoryCounterService());
}
Mvcs.ctx().iocs.put("nutz", ctx.getIoc());
}

View File

@ -0,0 +1,18 @@
package org.nutz.boot.metrics;
import java.util.Set;
public interface CounterService {
long increment(String metricName);
long decrement(String metricName);
void reset(String metricName);
void submit(String metricName, long value);
long get(String metricName);
Set<String> keys();
}

View File

@ -0,0 +1,42 @@
package org.nutz.boot.metrics.impl;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.nutz.boot.metrics.CounterService;
public class MemoryCounterService implements CounterService {
protected ConcurrentHashMap<String, AtomicLong> atoms = new ConcurrentHashMap<>();
protected AtomicLong getAtomicLong(String metricName) {
return atoms.computeIfAbsent(metricName, (name) -> new AtomicLong());
}
public long increment(String metricName) {
return getAtomicLong(metricName).incrementAndGet();
}
public long decrement(String metricName) {
return getAtomicLong(metricName).decrementAndGet();
}
public void reset(String metricName) {
atoms.remove(metricName);
}
public void submit(String metricName, long value) {
getAtomicLong(metricName).set(value);
}
public long get(String metricName) {
return getAtomicLong(metricName).get();
}
public Set<String> keys() {
return new HashSet<>(atoms.keySet());
}
}

View File

@ -1,12 +1,10 @@
package io.nutz.demo.ssdb;
import org.nutz.boot.NbApp;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.*;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.ssdb4j.SSDBs;
import org.nutz.ssdb4j.spi.Response;
import org.nutz.ssdb4j.spi.SSDB;