Java動態代理--Proxy.newProxyInstance4
阿新 • • 發佈:2018-11-11
注意:動態代理 並不一定非得攔截目標方法 場景也可以是 懶得寫實現類和其邏輯 利用動態代理 動態實現 類似於通用工具類的模式,如xxl-job原始碼中的那樣,只是獲取物件 組織物件內容
package com.xxl.job.core.rpc.netcom; import com.xxl.job.core.rpc.codec.RpcRequest; import com.xxl.job.core.rpc.codec.RpcResponse; import com.xxl.job.core.rpc.netcom.jetty.client.JettyClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.FactoryBean; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * rpc proxy */ public class NetComClientProxy implements FactoryBean<Object> { private static final Logger logger = LoggerFactory.getLogger(NetComClientProxy.class); // ---------------------- config ---------------------- private Class<?> iface; private String serverAddress; private String accessToken; private JettyClient client = new JettyClient(); public NetComClientProxy(Class<?> iface, String serverAddress, String accessToken) { this.iface = iface; this.serverAddress = serverAddress; this.accessToken = accessToken; } @Override public Object getObject() throws Exception { return Proxy.newProxyInstance(Thread.currentThread() .getContextClassLoader(), new Class[] { iface }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // filter method like "Object.toString()" if (Object.class.getName().equals(method.getDeclaringClass().getName())) { logger.error(">>>>>>>>>>> xxl-rpc proxy class-method not support [{}.{}]", method.getDeclaringClass().getName(), method.getName()); throw new RuntimeException("xxl-rpc proxy class-method not support"); } // request RpcRequest request = new RpcRequest(); request.setServerAddress(serverAddress); request.setCreateMillisTime(System.currentTimeMillis()); request.setAccessToken(accessToken); request.setClassName(method.getDeclaringClass().getName()); request.setMethodName(method.getName()); request.setParameterTypes(method.getParameterTypes()); request.setParameters(args); // send RpcResponse response = client.send(request); // valid response if (response == null) { logger.error(">>>>>>>>>>> xxl-rpc netty response not found."); throw new Exception(">>>>>>>>>>> xxl-rpc netty response not found."); } if (response.isError()) { throw new RuntimeException(response.getError()); } else { return response.getResult(); } } }); } @Override public Class<?> getObjectType() { return iface; } @Override public boolean isSingleton() { return false; } }
簡單的copy其中的一段程式碼:
package com.xxl.job.core.executor; import com.xxl.job.core.biz.AdminBiz; import com.xxl.job.core.biz.ExecutorBiz; import com.xxl.job.core.biz.impl.ExecutorBizImpl; import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.annotation.JobHandler; import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.rpc.netcom.NetComClientProxy; import com.xxl.job.core.rpc.netcom.NetComServerFactory; import com.xxl.job.core.thread.JobLogFileCleanThread; import com.xxl.job.core.thread.JobThread; import com.xxl.job.core.util.NetUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Created by xuxueli on 2016/3/2 21:14. */ public class XxlJobExecutor implements ApplicationContextAware { private static final Logger logger = LoggerFactory.getLogger(XxlJobExecutor.class); // ---------------------- admin-client ---------------------- private static List<AdminBiz> adminBizList; private static void initAdminBizList(String adminAddresses, String accessToken) throws Exception { if (adminAddresses!=null && adminAddresses.trim().length()>0) { for (String address: adminAddresses.trim().split(",")) { if (address!=null && address.trim().length()>0) { String addressUrl = address.concat(AdminBiz.MAPPING); AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, addressUrl, accessToken).getObject(); if (adminBizList == null) { adminBizList = new ArrayList<AdminBiz>(); } adminBizList.add(adminBiz); } } } } public static List<AdminBiz> getAdminBizList(){ return adminBizList; } }