package com.kafka.consume;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
interface A {
String func1();
}
class B implements A {
@Override
public String func1() {
return "f1";
}
public String func2(){
return "f2";
}
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Foo {
String value();
}
class Bar {
@Foo("fff")
private String val;
@KafkaListener(groupId = "demo", topics = "demo")
public void listenerDemo(ConsumerRecord<?, ?> record) throws Exception{
System.out.println("--"+record.value());
}
}
public class Main {
public static void main(String ...args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
Bar bar = new Bar();
Field field = Bar.class.getDeclaredField("val");
Foo foo = field.getAnnotation(Foo.class);
InvocationHandler h = Proxy.getInvocationHandler(foo);
Field hField = h.getClass().getDeclaredField("memberValues");
hField.setAccessible(true);
Map memberValues = (Map) hField.get(h);
memberValues.put("value", "ddd");
String value = foo.value();
System.out.println(value); // ddd
///////
Bar bar1 = new Bar();
Method[] method = Bar.class.getDeclaredMethods();
KafkaListener listener = method[0].getAnnotation(KafkaListener.class);
InvocationHandler hh = Proxy.getInvocationHandler(listener);
Field hFields = h.getClass().getDeclaredField("memberValues");
hFields.setAccessible(true);
Map memberValuess = (Map) hFields.get(hh);
memberValuess.put("groupId", "mayunzhenGroup");
System.out.println(listener.groupId());
}
}
@KafkaListener(groupId = "demo", topics = "testx",containerFactory = "kafkaListenerContainerFactory")
public void listenerDemo(ConsumerRecord<?, ?> record) throws Exception{
Method[] method = BSMListener.class.getDeclaredMethods();
KafkaListener listener = method[0].getAnnotation(KafkaListener.class);
InvocationHandler hh = Proxy.getInvocationHandler(listener);
Field hFields = null;
hFields = hh.getClass().getDeclaredField("memberValues");
hFields.setAccessible(true);
Map memberValuess = null;
memberValuess = (Map) hFields.get(hh);
System.out.println(method[0].getName()+","+memberValuess.get("groupId")+"----------------------------"+record.value());
}