The microkernel architecture pattern allows you to add additional application features as plug-ins to the core application, providing extensibility as well as feature separation and isolation. The microkernel architecture pattern consists of two types of architecture components: a core system and plug-in modules. Application logic is divided between independent plug-in modules and the basic core system, providing extensibility, flexibility, and isolation of application features and custom processing logic.

1:  Oriented-Interface Plug-in development

2:  Using Interceptor to develop Plug-in

3:  Poxy or Reflection also can be used in Plug-in development (InvocationHandler)

https://blog.csdn.net/huxiaoyonglan1/article/details/72956184

https://blog.csdn.net/danchu/article/details/70238002

public class JavassistProxyFactory extends AbstractProxyFactory {
@SuppressWarnings("unchecked")
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
return (T) Proxy.getProxy(interfaces).newInstance(
new InvokerInvocationHandler(invoker)
);
} public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
final Wrapper wrapper = Wrapper.getWrapper(
proxy.getClass().getName().indexOf('$') < 0 ?
proxy.getClass() : type);
return new AbstractProxyInvoker<T>(proxy, type, url) {
@Override
protected Object doInvoke(T proxy, String methodName,
Class<?>[] parameterTypes,
Object[] arguments) throws Throwable {
return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
}
};
}
}
public static <T> T create(Class<T> interfaceClass) {
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),new Class<?>[]{interfaceClass},new ObjectProxy<T>(interfaceClass)
);
} public static <T> IAsyncObjectProxy createAsync(Class<T> interfaceClass) {
return new ObjectProxy<T>(interfaceClass);
} public class ObjectProxy<T> implements InvocationHandler, IAsyncObjectProxy {
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectProxy.class);
private Class<T> clazz; public ObjectProxy(Class<T> clazz) {
this.clazz = clazz;
} //for synchronized call, package the call with classname, method, arguments
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class == method.getDeclaringClass()) {
String name = method.getName();
if ("equals".equals(name)) {
return proxy == args[0];
} else if ("hashCode".equals(name)) {
return System.identityHashCode(proxy);
} else if ("toString".equals(name)) {
return proxy.getClass().getName() + "@" +
Integer.toHexString(System.identityHashCode(proxy)) +
", with InvocationHandler " + this;
} else {
throw new IllegalStateException(String.valueOf(method));
}
} //package the classname, method, types of arguments and arguments,additional request id
RpcRequest request = new RpcRequest();
request.setRequestId(UUID.randomUUID().toString());
request.setClassName(method.getDeclaringClass().getName());
request.setMethodName(method.getName());
request.setParameterTypes(method.getParameterTypes());
request.setParameters(args); // Debug
LOGGER.debug(method.getDeclaringClass().getName());
LOGGER.debug(method.getName());
for (int i = 0; i < method.getParameterTypes().length; ++i) {
LOGGER.debug(method.getParameterTypes()[i].getName());
}
for (int i = 0; i < args.length; ++i) {
LOGGER.debug(args[i].toString());
} //get one client from list of client
RpcClientHandler handler = ConnectManage.getInstance().chooseHandler(); //send rpc message to rpc server and wait for results
RPCFuture rpcFuture = handler.sendRequest(request);
return rpcFuture.get();
} @Override
public RPCFuture call(String funcName, Object... args) {
RpcClientHandler handler = ConnectManage.getInstance().chooseHandler();
RpcRequest request = createRequest(this.clazz.getName(), funcName, args);
RPCFuture rpcFuture = handler.sendRequest(request);
return rpcFuture;
}
}
public class RpcClient implements InvocationHandler {	

	@SuppressWarnings("unchecked")
public <T> T proxy(Class<T> interfaceClass) throws Throwable {
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(interfaceClass.getName()
+ " is not an interface");
}
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),new Class<?>[] { interfaceClass }, this);
} @Override
public RpcClient interfaceClass(Class<?> interfaceClass) {
// TODO Auto-generated method stub
this.interfaceClass=interfaceClass;
return this;
} @Override
public RpcClient version(String version) {
// TODO Auto-generated method stub
this.version=version;
return this;
} @Override
public RpcClient clientTimeout(int clientTimeout) {
// TODO Auto-generated method stub
this.timeout=clientTimeout;
return this;
} @Override
public RpcConsumer hook(ConsumerHook hook) {
// TODO Auto-generated method stub
this.hook=hook;
return this;
} @Override
public Object instance() {
try {
return proxy(this.interfaceClass);
}
catch (Throwable e)
{
e.printStackTrace();
}
return null;
} @Override
public void asynCall(String methodName) {
asynCall(methodName, null);
} @Override
public <T extends ResponseCallbackListener> void asynCall(String methodName, T callbackListener) {
this.asyncMethods.put(methodName, callbackListener);
this.connection.setAsyncMethod(asyncMethods);
for (RpcConnection conn:connection_list)
{
conn.setAsyncMethod(asyncMethods);
}
} @Override
public void cancelAsyn(String methodName) {
this.asyncMethods.remove(methodName);
this.connection.setAsyncMethod(asyncMethods);
for (RpcConnection conn:connection_list)
{
conn.setAsyncMethod(asyncMethods);
}
} @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
List<String> parameterTypes = new LinkedList<String>();
for (Class<?> parameterType : method.getParameterTypes()) {
parameterTypes.add(parameterType.getName());
}
RpcRequest request = new RpcRequest();
request.setRequestId(UUID.randomUUID().toString());
request.setClassName(method.getDeclaringClass().getName());
request.setMethodName(method.getName());
request.setParameterTypes(method.getParameterTypes());
request.setParameters(args);
if(hook!=null)
hook.before(request);
RpcResponse response = null;
try
{
request.setContext(RpcContext.props);
response = (RpcResponse) select().Send(request,asyncMethods.containsKey(request.getMethodName()));
if(hook!=null)
hook.after(request); if(!asyncMethods.containsKey(request.getMethodName())&&response.getExption()!=null)
{
Throwable e=(Throwable) Tool.deserialize(response.getExption(),response.getClazz());
throw e.getCause();
}
}
catch (Throwable t)
{
//t.printStackTrace();
//throw new RuntimeException(t);
throw t;
}
finally
{
// if(asyncMethods.containsKey(request.getMethodName())&&asyncMethods.get(request.getMethodName())!=null)
// {
// cancelAsyn(request.getMethodName());
// }
}
if(response==null)
{
return null;
}
else if (response.getErrorMsg() != null)
{
throw response.getErrorMsg();
}
else
{
return response.getAppResponse();
}
}
}
public class ProxyFactory implements MethodInterceptor {  

    private Object obj;
public Object createProxy(Object target) {
this.obj = target;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.obj.getClass());
enhancer.setCallback(this);
enhancer.setClassLoader(target.getClass().getClassLoader());
return enhancer.create();
} @Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Object result = null;
try { before();
result = proxy.invokeSuper(obj, args);
after();
} catch (Exception e) {
exception();
}finally{
beforeReturning();
}
return result;
}
} Hello hello = new Hello();
ProxyFactory cglibProxy = new ProxyFactory();
Hello proxy = (Hello) cglibProxy.createProxy(hello);
String result=proxy.sayHello(true); //////////////////////////////////////////////////////
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(SampleClass.class);
enhancer.setCallback(new FixedValue() {
@Override
public Object loadObject() throws Exception {
return "Hello cglib!";
}
});
SampleClass proxy = (SampleClass) enhancer.create(); ////////////////////////////////////////////////////////
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(SampleClass.class);
enhancer.setCallback(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
return "Hello cglib!";
} else {
throw new RuntimeException("Do not know what to do.");
}
}
});
SampleClass proxy = (SampleClass) enhancer.create(); /////////////////////////////////////////////////////////////////
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(SampleClass.class);
enhancer.setCallbackFilter(new filter());
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
throws Throwable {
if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
return "Hello cglib!";
} else {
return proxy.invokeSuper(obj, args);
}
}
});
SampleClass proxy = (SampleClass) enhancer.create();
//////////////////////////////////////////////////////////////////

https://www.jianshu.com/p/20203286ccd9

microkernel architecture - Proxy的更多相关文章

  1. 微内核架构(Microkernel Architecture)

    微内核架构(Microkernel Architecture) 微内核架构有时也被成为插件架构模式(plug-in architecture pattern),通常用于实现基于产品的应用,如Eclip ...

  2. Software Architecture Pattern(Mark Richards)笔记

    软件架构模式 缺少规范架构的程序通常会变得紧耦合.脆弱.难以更改,缺少清晰的发展方向和愿景.这本小书用50多页介绍了常用的5种常见架构模式,相信不管是大牛还是萌新都会有所收获,特别是对我这种偏爱系统设 ...

  3. 理解 Dubbo SPI 扩展机制

    写在前面 最近接触了 gRPC 体会到虽然众多 RPC 框架各有各的特点但是他们提供的特性和功能有很多的相似之处 , 这就说明他们面对同样的分布式系统带来的问题.从 2016 年左右开始接触到 dub ...

  4. [转] 理解 Dubbo SPI 扩展机制

    写在前面 最近接触了 gRPC 体会到虽然众多 RPC 框架各有各的特点但是他们提供的特性和功能有很多的相似之处 , 这就说明他们面对同样的分布式系统带来的问题.从 2016 年左右开始接触到 dub ...

  5. iOS动态部署方案

    转载: iOS动态部署方案 前言 这里讨论的动态部署方案,就是指通过不发版的方式,将新的内容.新的业务流程部署进已发布的App.因为苹果的审核周期比较长,而且苹果的限制比较多,业界在这里也没有特别多的 ...

  6. Anatomy of the Linux kernel--转

    ref:http://www.ibm.com/developerworks/linux/library/l-linux-kernel/?S_TACT=105AGX52&S_CMP=cn-a-l ...

  7. ssiOS应用架构谈 本地持久化方案及动态部署

    本文转载至 http://casatwy.com/iosying-yong-jia-gou-tan-ben-di-chi-jiu-hua-fang-an-ji-dong-tai-bu-shu.html ...

  8. iOS应用架构谈part4-本地持久化方案及动态部署

    前言 嗯,你们要的大招.跟着这篇文章一起也发布了CTPersistance和CTJSBridge这两个库,希望大家在实际使用的时候如果遇到问题,就给我提issue或者PR或者评论区.每一个issue和 ...

  9. 操作系统微内核和Dubbo微内核,有何不同?

    你好,我是 yes. 在之前的文章已经提到了 RPC 的核心,想必一个 RPC 通信大致的流程和基本原理已经清晰了. 这篇文章借着 Dubbo 来说说微内核这种设计思想,不会扯到 Dubbo 某个具体 ...

随机推荐

  1. COCO2018 全景分割

    全景分割是18年新推出的一个任务,它要求同时分割出目标和背景,也就是既有实例分割也有语义分割,用官方的话讲是朝着真实世界视觉系统的重要一步 如图所示,里面既有对天空,草地等stuff的分割,也有对目标 ...

  2. Subsequence(二分)

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  3. 最大比例(贪心+思维+gcd)

    X星球的某个大奖赛设了M级奖励.每个级别的奖金是一个正整数. 并且,相邻的两个级别间的比例是个固定值. 也就是说:所有级别的奖金数构成了一个等比数列.比如: 16,24,36,54 其等比值为:3/2 ...

  4. loj 2038 / 洛谷 P4345 [SHOI2015] 超能粒子炮・改 题解

    好玩的推式子 题目描述 曾经发明了脑洞治疗仪与超能粒子炮的发明家 SHTSC 又公开了他的新发明:超能粒子炮・改--一种可以发射威力更加强大的粒子流的神秘装置. 超能粒子炮・改相比超能粒子炮,在威力上 ...

  5. 119th LeetCode Weekly Contest Largest Perimeter Triangle

    Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...

  6. ASP.NET Core文件上传、下载与删除

    首先我们需要创建一个form表单如下: <form method="post" enctype="multipart/form-data" asp-con ...

  7. knime 设置 小数点精度

    kinme 默认小数精度是保留三位小数. 如果0.0003,knime会自动舍弃,读出0.下面步骤教你怎么把小数精度全部显示. File->references->preferred re ...

  8. mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)

    1.使用相同索引键值的冲突 由于mysql 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但如果是使用相同的索引键,是会出现锁冲突的.设计时要注意 例如:city表city_ ...

  9. RabbitMQ原理——exchange、route、queue的关系

    从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件    从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...

  10. Eclipse的企业开发时常用快捷键使用、优化配置(博主推荐)

    不多说,直接上干货! 一.简介 eclipse可谓是Java开发界的神器,基本占据了大部分的Java开发市场,而且其官方还对其他语言提供支持,如C++,Ruby,JavaScript等等.为什么使用它 ...