Skip to content

Commit

Permalink
NettyRPC 2.8 filter chain and listener chain support by tangjie
Browse files Browse the repository at this point in the history
NettyRPC 2.8 filter chain and listener chain support by tangjie
  • Loading branch information
tang-jie committed Feb 2, 2018
1 parent a8fc55f commit b187131
Show file tree
Hide file tree
Showing 18 changed files with 719 additions and 3 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/newlandframework/rpc/core/DefaultModular.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.core;

import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:DefaultModular.java
* @description:DefaultModular功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/2/1
*/
public class DefaultModular implements Modular {
@Override
public <T> ModuleProvider<T> invoke(ModuleInvoker<T> invoker, MessageRequest request) {
return new ModuleProvider<T>() {
@Override
public ModuleInvoker<T> getInvoker() {
return invoker;
}

@Override
public void destoryInvoker() {
invoker.destroy();
}
};
}
}

30 changes: 30 additions & 0 deletions src/main/java/com/newlandframework/rpc/core/Modular.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.core;

import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:Modular.java
* @description:Modular功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/2/1
*/
public interface Modular {
<T> ModuleProvider<T> invoke(ModuleInvoker<T> invoker, MessageRequest request);
}

34 changes: 34 additions & 0 deletions src/main/java/com/newlandframework/rpc/core/ModuleInvoker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.core;

import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ModuleInvoker.java
* @description:ModuleInvoker功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/1/31
*/
public interface ModuleInvoker<T> {
Class<T> getInterface();

Object invoke(MessageRequest request) throws Throwable;

void destroy();
}

30 changes: 30 additions & 0 deletions src/main/java/com/newlandframework/rpc/core/ModuleProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.core;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ModuleProvider.java
* @description:ModuleProvider
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/1/31
*/
public interface ModuleProvider<T> {
ModuleInvoker<T> getInvoker();

void destoryInvoker();
}

Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private void listField(Field f, boolean html) {
}

public void listMethod(Executable member, boolean html) {
provider.append(html ? "<br>&nbsp&nbsp" : "\n " + modifiers(member.getModifiers()));
provider.append(html ? "<br>&nbsp&nbsp" : "\n " + modifiers(member.getModifiers() & (~Modifier.FINAL)));
if (member instanceof Method) {
provider.append(getType(((Method) member).getReturnType()) + " ");
}
Expand Down Expand Up @@ -339,6 +339,9 @@ public List<String> getClassMethodSignature(Class<?> cls) {
int modifiers = member.getModifiers();
if (Modifier.isAbstract(modifiers) && Modifier.isPublic(modifiers)) {
signatureMethod.append(modifiers(Modifier.PUBLIC));
if (Modifier.isFinal(modifiers)) {
signatureMethod.append(modifiers(Modifier.FINAL));
}
} else {
signatureMethod.append(modifiers);
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/newlandframework/rpc/filter/ChainFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.filter;

import com.newlandframework.rpc.core.ModuleInvoker;
import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ChainFilter.java
* @description:ChainFilter功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/1/31
*/
public interface ChainFilter {
Object invoke(ModuleInvoker<?> invoker, MessageRequest request) throws Throwable;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.filter;

import com.newlandframework.rpc.core.Modular;
import com.newlandframework.rpc.core.ModuleInvoker;
import com.newlandframework.rpc.core.ModuleProvider;
import com.newlandframework.rpc.model.MessageRequest;

import java.util.List;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ModuleFilterChainWrapper.java
* @description:ModuleFilterChainWrapper功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/2/2
*/
public class ModuleFilterChainWrapper implements Modular {
private Modular modular;
private List<ChainFilter> filters;

public ModuleFilterChainWrapper(Modular modular) {
if (modular == null) {
throw new IllegalArgumentException("module is null");
}
this.modular = modular;
}

@Override
public <T> ModuleProvider<T> invoke(ModuleInvoker<T> invoker, MessageRequest request) {
return modular.invoke(buildChain(invoker), request);
}

private <T> ModuleInvoker<T> buildChain(ModuleInvoker<T> invoker) {
ModuleInvoker last = invoker;

if (filters.size() > 0) {
for (int i = filters.size() - 1; i >= 0; i--) {
ChainFilter filter = filters.get(i);
ModuleInvoker<T> next = last;
last = new ModuleInvoker<T>() {
@Override
public Object invoke(MessageRequest request) throws Throwable {
return filter.invoke(next, request);
}

@Override
public Class<T> getInterface() {
return invoker.getInterface();
}

@Override
public String toString() {
return invoker.toString();
}

@Override
public void destroy() {
invoker.destroy();
}
};
}
}
return last;
}

public List<ChainFilter> getFilters() {
return filters;
}

public void setFilters(List<ChainFilter> filters) {
this.filters = filters;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.filter.support;

import com.newlandframework.rpc.core.ModuleInvoker;
import com.newlandframework.rpc.filter.ChainFilter;
import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:ClassLoaderChainFilter.java
* @description:ClassLoaderChainFilter功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/1/31
*/
public class ClassLoaderChainFilter implements ChainFilter {
@Override
public Object invoke(ModuleInvoker<?> invoker, MessageRequest request) throws Throwable {
ClassLoader ocl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader());

Object result = null;
try {
result = invoker.invoke(request);
return result;
} catch (Throwable throwable) {
throwable.printStackTrace();
throw throwable;
} finally {
Thread.currentThread().setContextClassLoader(ocl);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2018 Newland Group Holding Limited
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.newlandframework.rpc.filter.support;

import com.newlandframework.rpc.core.ModuleInvoker;
import com.newlandframework.rpc.filter.ChainFilter;
import com.newlandframework.rpc.model.MessageRequest;

/**
* @author tangjie<https://github.com/tang-jie>
* @filename:EchoChainFilter.java
* @description:EchoChainFilter功能模块
* @blogs http://www.cnblogs.com/jietang/
* @since 2018/1/31
*/
public class EchoChainFilter implements ChainFilter {
@Override
public Object invoke(ModuleInvoker<?> invoker, MessageRequest request) throws Throwable {
Object o = null;
try {
System.out.println("EchoChainFilter##TRACE MESSAGE-ID:" + request.getMessageId());
o = invoker.invoke(request);
return o;
} catch (Throwable throwable) {
throwable.printStackTrace();
throw throwable;
}
}
}

Loading

0 comments on commit b187131

Please sign in to comment.