Ideal is full, the reality is very skinny.
As business needs "flexible configurable" functional requirements, when using java development Influxdb query function, encountered a problem, Measurement annotation name may need to change dynamically.
We look at @Measurement annotated code:
package org.influxdb.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
/**
* @author fmachado
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Measurement {
String name();
String database() default "[unassigned]";
String retentionPolicy() default "autogen";
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
The problem can be converted to:
-> Before @Measurement annotations take effect, the change of name value is written.
After about two days of information to find, discovered by: java reflection to solve the
major need to operate two classes of api
java.lang.reflect.InvocationHandler;
java.lang.reflect.Proxy;
The final solution by toPOJO methods inherited InfluxDBResultMapper solved.
Posted the following codes:
public class InfluxDBResultMapperHelper extends InfluxDBResultMapper {
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz,String name)
throws InfluxDBMapperException {
InvocationHandler handler = Proxy.getInvocationHandler(clazz.getAnnotation(Measurement.class));
Field hField = null;
try {
hField = handler.getClass().getDeclaredField(ME_VALUE);
hField.setAccessible(true);
Map memberValues = (Map) hField.get(handler);
memberValues.put(ME_NAME, name);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return toPOJO(queryResult, clazz, TimeUnit.MILLISECONDS);
}
}