我阅读了一些关于 CDI 自定义扩展的文档,并阅读了一些示例代码,例如以下链接上的属性加载器:Link-A - Link-B。
我写了一个像这样的代码的简单 cdi 扩展。
public class SystemStatisticExtension implements Extension {
public <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> pit) {
AnnotatedType<T> at = pit.getAnnotatedType();
InjectionTarget<T> it = pit.getInjectionTarget();
if (at.isAnnotationPresent(Monitor.class)) {
pit.setInjectionTarget(new MemoryInjectionPoint<>(it, at));
}
}
}
这是我的 InjectionPointTarget 实现:
public class MemoryInjectionPoint<T> implements InjectionTarget<T> {
private InjectionTarget<T> it;
private AnnotatedType<T> at;
private int kB = 1024;
private int mB = kB / 1024;
private int gB = mB / 1024;
public MemoryInjectionPoint(InjectionTarget<T> it, AnnotatedType<T> at) {
this.it = it;
this.at = at;
}
@Override
public void inject(T instance, CreationalContext<T> ctx) {
it.inject(instance, ctx);
int swapUsed = SystemPropertiesLoader.newInstance().getSwapUsed();
Set<AnnotatedField<? super T>> annotatedFields = at.getFields();
for (AnnotatedField<? super T> annotatedField : annotatedFields) {
if (annotatedField.isAnnotationPresent(Memory.class)) {
int memUsed = SystemPropertiesLoader.newInstance().getMemUsed();
Memory memory = annotatedField.getAnnotation(Memory.class);
Unit u = memory.unitType();
switch (u) {
case KILOBYTE:
setFieldMemValue(instance, memUsed / kB, annotatedField);
break;
case MEGABYTE:
setFieldMemValue(instance, memUsed / mB, annotatedField);
break;
case GIGABYTE:
setFieldMemValue(instance, memUsed / gB, annotatedField);
break;
}
}
if (at.isAnnotationPresent(Swap.class)) {
Memory memory = annotatedField.getAnnotation(Memory.class);
Unit u = memory.unitType();
switch (u) {
case kILOBYTE:
setFieldSwapValue(instance, swapUsed / kB, annotatedField);
break;
case MEGABYTE:
setFieldSwapValue(instance, swapUsed / mB, annotatedField);
break;
case GIGABYTE:
setFieldSwapValue(instance, swapUsed / gB, annotatedField);
break;
}
}
}
}
private void setFieldMemValue(T instance, int memUsed, AnnotatedField<? super T> annotatedField) {
try {
Field field = annotatedField.getJavaMember();
field.setAccessible(true);
field.setInt(instance, memUsed);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void setFieldSwapValue(T instance, int swapUsed, AnnotatedField<? super T> annotatedField) {
try {
Field field = annotatedField.getJavaMember();
field.setAccessible(true);
field.setInt(instance, swapUsed);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void postConstruct(T instance) {
it.postConstruct(instance);
}
@Override
public void preDestroy(T instance) {
it.preDestroy(instance);
}
@Override
public T produce(CreationalContext<T> ctx) {
return it.produce(ctx);
}
@Override
public void dispose(T instance) {
it.dispose(instance);
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return it.getInjectionPoints();
}
}
这是我的 SystemPropertiesLoader :
public class SystemPropertiesLoader {
private static Supplier<Stream<String>> supplier;
private SystemPropertiesLoader() {
}
public static SystemPropertiesLoader newInstance() {
supplier = () -> {
Stream<String> lines = Stream.empty();
try {
lines = Files.lines(Paths.get("/proc/meminfo"));
} catch (IOException e) {
e.printStackTrace();
}
return lines;
};
return new SystemPropertiesLoader();
}
public int getMemTotal() {
return Integer.valueOf(
supplier.get()
.findFirst()
.orElse("")
.split(":")[1]
.trim()
.replace(" kB", ""));
}
public int getMemFree() {
return Integer.valueOf(
supplier.get()
.skip(1)
.findFirst()
.orElse("")
.split(":")[1]
.trim()
.replace(" kB", ""));
}
public int getSwapTotal() {
return Integer.valueOf(supplier.get()
.skip(14)
.findFirst()
.orElse("")
.split(":")[1]
.trim()
.replace(" kB", ""));
}
public int getSwapFree() {
return Integer.valueOf(supplier.get()
.skip(15)
.findFirst()
.orElse("")
.split(":")[1]
.trim()
.replace(" kB", ""));
}
public int getMemUsed() {
return getMemTotal() - getMemFree();
}
public int getSwapUsed() {
return getSwapTotal() - getSwapFree();
}
}
和注释:
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface Memory {
Unit unitType() default Unit.MEGABYTE;
}
@Target({METHOD,TYPE})
@Retention(RUNTIME)
public @interface Monitor {
}
这是我的用例:
@Monitor
public class MemoryMonitor {
@Memory
private int memUsed;
public int getMemUsed() {
return memUsed;
}
}
现在我的问题是更新 /proc/meminfo 后 memUsed 属性没有改变。
怎么了 ?
可以为此创建动态 CDI 扩展吗?
注 1:我复制并粘贴整个代码。
注 2:/proc/meminfo 是 Linux&Unix 操作系统支持的 proc 文件系统中使用的内存信息。