Talk about dubbo’s ClassLoaderFilter

  dubbo

Order

This article mainly studies dubbo’s ClassLoaderFilter

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);
        }
    }

}
  • The ClassLoaderFilter implements the Filter interface. its invoke method first obtains the contextClassLoader of the current thread, then sets its ContextClassLoader to invoke. 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 the invoker is set to DemoService.class, and then verify that thread.currentthread (). getcontextclassloader () in the invoker method is the URLClassLoader that loads demoservice.class.

Summary

The ClassLoaderFilter implements the Filter interface. its invoke method first obtains the contextClassLoader of the current thread, then sets its ContextClassLoader to invoke. getinterface (). getclassLoader (), then executes the invoker.invoke method, and finally resets the classloader of the current thread to the original contextClassLoader.

doc