Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of ClassLoaderFilter in dubbo

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the role of ClassLoaderFilter in dubbo". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the ClassLoaderFilter of dubbo.

ClassLoaderFilter

Dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ClassLoaderFilter.java

@ Activate (group = CommonConstants.PROVIDER, order =-30000) public class ClassLoaderFilter implements Filter {@ Override public Result invoke (Invoker invoker, Invocation invocation) throws RpcException {ClassLoader ocl = Thread.currentThread (). GetContextClassLoader (); Thread.currentThread (). SetContextClassLoader (invoker.getInterface (). GetClassLoader ()); try {return invoker.invoke (invocation);} finally {Thread.currentThread (). SetContextClassLoader (ocl);}

ClassLoaderFilter implements the Filter interface. Its invoke method first gets the contextClassLoader of the current thread, then sets its ContextClassLoader to invoker.getInterface (). GetClassLoader (), then executes the invoker.invoke method, and finally resets the classLoader of the current thread to the original contextClassLoader

Example

Dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ClassLoaderFilterTest.java

Public class ClassLoaderFilterTest {private ClassLoaderFilter classLoaderFilter = new ClassLoaderFilter (); @ Test public void testInvoke () throws Exception {URL url = URL.valueOf ("test://test:11/test?accesslog=true&group=dubbo&version=1.1"); String path = DemoService.class.getResource ("/") .getPath () Final URLClassLoader cl = new URLClassLoader (new java.net.URL [] {new java.net.URL ("file:" + path)}) {@ Override public Class loadClass (String name) throws ClassNotFoundException {try {return findClass (name);} catch (ClassNotFoundException e) {return super.loadClass (name) }; final Class clazz = cl.loadClass (DemoService.class.getCanonicalName ()); Invoker invoker = new MyInvoker (url) {@ Override public Class getInterface () {return clazz } @ Override public Result invoke (Invocation invocation) throws RpcException {Assertions.assertEquals (cl, Thread.currentThread (). GetContextClassLoader ()); return null;}}; Invocation invocation = Mockito.mock (Invocation.class); classLoaderFilter.invoke (invoker, invocation);}}

Here, the interface of invoker is set to DemoService.class, and then verify that Thread.currentThread (). GetContextClassLoader () in the invoke method is the URLClassLoader that loads DemoService.class

Summary

ClassLoaderFilter implements the Filter interface. Its invoke method first gets the contextClassLoader of the current thread, then sets its ContextClassLoader to invoker.getInterface (). GetClassLoader (), then executes the invoker.invoke method, and finally resets the classLoader of the current thread to the original contextClassLoader

This is the end of the content of "what is the role of ClassLoaderFilter in dubbo". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report