Talk about dubbo's ClassLoaderFilter

Keywords: Programming Dubbo Java Apache

order

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

}
  • ClassLoaderFilter implements the Filter interface. Its invoke method first obtains 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 the invoker is set to DemoService.class, and then verify that the Thread.currentThread().getContextClassLoader() in the invoke method is the URLClassLoader for loading DemoService.class.

Summary

ClassLoaderFilter implements the Filter interface. Its invoke method first obtains 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.

doc

Posted by binumathew on Thu, 31 Oct 2019 05:42:07 -0700