站点更新:2019-06-28 20:52:00
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										24
									
								
								src/main/java/me/ehlxr/proxy/Bird.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/main/java/me/ehlxr/proxy/Bird.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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..."); | ||||||
|  |     // } | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								src/main/java/me/ehlxr/proxy/Flyable.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/main/java/me/ehlxr/proxy/Flyable.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | package me.ehlxr.proxy; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author lixiangrong | ||||||
|  |  * @since 2019-06-28. | ||||||
|  |  */ | ||||||
|  | public interface Flyable { | ||||||
|  |     void fly(); | ||||||
|  |  | ||||||
|  |     // void run(); | ||||||
|  | } | ||||||
							
								
								
									
										42
									
								
								src/main/java/me/ehlxr/proxy/Main.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/main/java/me/ehlxr/proxy/Main.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										32
									
								
								src/main/java/me/ehlxr/proxy/MyInvocationHandler.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/main/java/me/ehlxr/proxy/MyInvocationHandler.java
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -14,6 +14,7 @@ import java.util.concurrent.TimeoutException; | |||||||
|  * @author lixiangrong |  * @author lixiangrong | ||||||
|  * @since 2019-01-22. |  * @since 2019-01-22. | ||||||
|  */ |  */ | ||||||
|  | @SuppressWarnings("ALL") | ||||||
| public class Receiver1 { | public class Receiver1 { | ||||||
|     private final static String QUEUE_NAME = "queue_topic2"; |     private final static String QUEUE_NAME = "queue_topic2"; | ||||||
|     private final static String EXCHANGE_NAME = "exchange_topic"; |     private final static String EXCHANGE_NAME = "exchange_topic"; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user