forked from tang-jie/NettyRPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NettyRPC 2.8 filter chain and listener chain support by tangjie
NettyRPC 2.8 filter chain and listener chain support by tangjie
- Loading branch information
Showing
18 changed files
with
719 additions
and
3 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/com/newlandframework/rpc/core/DefaultModular.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/main/java/com/newlandframework/rpc/core/ModuleInvoker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/com/newlandframework/rpc/core/ModuleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/newlandframework/rpc/filter/ChainFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
89 changes: 89 additions & 0 deletions
89
src/main/java/com/newlandframework/rpc/filter/ModuleFilterChainWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
src/main/java/com/newlandframework/rpc/filter/support/ClassLoaderChainFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
src/main/java/com/newlandframework/rpc/filter/support/EchoChainFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.