diff --git a/resources/idea/go-settings.jar b/resources/idea/go-settings.jar index efa0cd3..c0b7a2b 100644 Binary files a/resources/idea/go-settings.jar and b/resources/idea/go-settings.jar differ diff --git a/src/main/java/me/ehlxr/proxy/Bird.java b/src/main/java/me/ehlxr/proxy/Bird.java new file mode 100644 index 0000000..5f4bad7 --- /dev/null +++ b/src/main/java/me/ehlxr/proxy/Bird.java @@ -0,0 +1,24 @@ +package me.ehlxr.proxy; + +import java.util.Random; + +/** + * @author lixiangrong + * @since 2019-06-28. + */ +public class Bird implements Flyable { + @Override + public void fly() { + System.out.println("Bird is flying..."); + try { + Thread.sleep(new Random().nextInt(1000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + // @Override + // public void run() { + // System.out.println("Bird is running..."); + // } +} diff --git a/src/main/java/me/ehlxr/proxy/Flyable.java b/src/main/java/me/ehlxr/proxy/Flyable.java new file mode 100644 index 0000000..0d45cd8 --- /dev/null +++ b/src/main/java/me/ehlxr/proxy/Flyable.java @@ -0,0 +1,11 @@ +package me.ehlxr.proxy; + +/** + * @author lixiangrong + * @since 2019-06-28. + */ +public interface Flyable { + void fly(); + + // void run(); +} diff --git a/src/main/java/me/ehlxr/proxy/Main.java b/src/main/java/me/ehlxr/proxy/Main.java new file mode 100644 index 0000000..f294eec --- /dev/null +++ b/src/main/java/me/ehlxr/proxy/Main.java @@ -0,0 +1,42 @@ +package me.ehlxr.proxy; + +import java.lang.reflect.Proxy; + +/** + * @author lixiangrong + * @since 2019-06-28. + */ +public class Main { + public static void main(String[] args) { + Flyable proxy = (Flyable) Proxy.newProxyInstance(Flyable.class.getClassLoader(), + new Class[]{Flyable.class}, new MyInvocationHandler(new Bird())); + + // 动态代理会生成类似以下的 Java 代码 + /* + package me.ehlxr.proxy; + + import java.lang.reflect.InvocationHandler; + import java.lang.reflect.Method; + + public class Proxy implements Flyable { + private InvocationHandler handler; + + public Proxy(InvocationHandler handler) { + this.handler = handler; + } + + @Override + public void fly() { + try { + Method method = Flyable.class.getMethod("fly"); + this.handler.invoke(this, method, null); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + */ + + proxy.fly(); + } +} diff --git a/src/main/java/me/ehlxr/proxy/MyInvocationHandler.java b/src/main/java/me/ehlxr/proxy/MyInvocationHandler.java new file mode 100644 index 0000000..a293dcf --- /dev/null +++ b/src/main/java/me/ehlxr/proxy/MyInvocationHandler.java @@ -0,0 +1,32 @@ +package me.ehlxr.proxy; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * @author lixiangrong + * @since 2019-06-28. + */ +public class MyInvocationHandler implements InvocationHandler { + private Bird bird; + + public MyInvocationHandler(Bird bird) { + this.bird = bird; + } + + @Override + public Object invoke(Object proxy, Method method, Object[] args) { + long start = System.currentTimeMillis(); + + try { + method.invoke(bird, args); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + + long end = System.currentTimeMillis(); + System.out.println("Fly time = " + (end - start)); + return null; + } +} diff --git a/src/main/java/me/ehlxr/rabbitmq/topic/Receiver1.java b/src/main/java/me/ehlxr/rabbitmq/topic/Receiver1.java index 795ceb8..50e8f74 100755 --- a/src/main/java/me/ehlxr/rabbitmq/topic/Receiver1.java +++ b/src/main/java/me/ehlxr/rabbitmq/topic/Receiver1.java @@ -14,6 +14,7 @@ import java.util.concurrent.TimeoutException; * @author lixiangrong * @since 2019-01-22. */ +@SuppressWarnings("ALL") public class Receiver1 { private final static String QUEUE_NAME = "queue_topic2"; private final static String EXCHANGE_NAME = "exchange_topic";