从零并发框架(三)异步转同步注解+字节码增强代理实现( 三 )


package com.github.houbb.sync.core.bs;import com.github.houbb.sync.api.api.ISync;import com.github.houbb.sync.api.api.ISyncContext;import com.github.houbb.sync.core.core.SimpleSync;/** * 引导类 * @author binbin.hou * @since 0.0.1 */public final class SyncBs {private SyncBs(){}/*** 同步实现* @since 0.0.1*/private final ISync sync = new SimpleSync();/*** 同步上下文* @since 0.0.1*/private ISyncContext syncContext;/*** 新建对象实例* @return 实例* @since 0.0.1*/public static SyncBs newInstance() {return new SyncBs();}public SyncBs syncContext(ISyncContext syncContext) {this.syncContext = syncContext;return this;}public Object execute() throws Throwable {return this.sync.sync(syncContext);}}代理实现接口定义我们为上述三种情况定义统一的接口:
package com.github.houbb.sync.core.support.proxy;/** * 代理接口 * @author binbin.hou * @since 0.0.1 */public interface ISyncProxy {/*** 获取代理实现* @return 代理* @since 0.0.6*/Object proxy();}不需要代理/* * Copyright (c)2019. houbinbin Inc. * async All rights reserved. */package com.github.houbb.sync.core.support.proxy.none;import com.github.houbb.sync.core.support.proxy.ISyncProxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/** *没有代理
* *Created: 2019/3/5 10:23 PM *Project: async * * @author houbinbin * @since 0.0.1 */public class NoneProxy implements InvocationHandler, ISyncProxy {/*** 代理对象*/private final Object target;public NoneProxy(Object target) {this.target = target;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {return method.invoke(proxy, args);}/*** 返回原始对象 , 没有代理* @return 原始对象*/@Overridepublic Object proxy() {return this.target;}}
这种比较简单 , 直接反射执行原来的方法即可 。
动态代理/* * Copyright (c)2019. houbinbin Inc. * async All rights reserved. */package com.github.houbb.sync.core.support.proxy.dynamic;import com.github.houbb.sync.api.api.ISyncContext;import com.github.houbb.sync.core.bs.SyncBs;import com.github.houbb.sync.core.core.SimpleSyncContext;import com.github.houbb.sync.core.support.proxy.ISyncProxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.concurrent.CompletionService;/** *动态代理
* *Created: 2019/3/5 10:23 PM *Project: sync * * @author houbinbin * @since 0.0.1 */public class DynamicProxy implements InvocationHandler, ISyncProxy {/*** 被代理的对象*/private final Object target;public DynamicProxy(Object target) {this.target = target;}/*** 这种方式虽然实现了异步执行 , 但是存在一个缺陷:* 强制用户返回值为 Future 的子类 。** 如何实现不影响原来的值 , 要怎么实现呢?* @param proxy 原始对象* @param method 方法* @param args 入参* @return 结果* @throws Throwable 异常*/@Override@SuppressWarnings("all")public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {ISyncContext context = SimpleSyncContext.newInstance().method(method).params(args).target(target);return SyncBs.newInstance().syncContext(context).execute();}@Overridepublic Object proxy() {// 我们要代理哪个真实对象 , 就将该对象传进去 , 最后是通过该真实对象来调用其方法的InvocationHandler handler = new DynamicProxy(target);return Proxy.newProxyInstance(handler.getClass().getClassLoader(),target.getClass().getInterfaces(), handler);}}