odnoklassniki / one-nio

Unconventional I/O library for Java
Apache License 2.0
648 stars 96 forks source link

UndeclaredThrowableException thrown when RpcClient service as a bean in spring boot application #36

Closed forchid closed 5 years ago

forchid commented 5 years ago

When I use RpcClient proxy service as a spring bean in spring boot, the java.lang.reflect.UndeclaredThrowableException is thrown in application bootstrap. A demo stack trace:

Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
        at com.sun.proxy.$Proxy0.hashCode(Unknown Source)
        at java.util.HashMap.hash(HashMap.java:339)
        at java.util.HashMap.put(HashMap.java:612)
        at EchoClient.main(EchoClient.java:57)
Caused by: one.nio.pool.PoolException: SocketPool[localhost:9000] createObject failed
        at one.nio.pool.SocketPool.createObject(SocketPool.java:145)
        at one.nio.pool.SocketPool.createObject(SocketPool.java:24)
        at one.nio.pool.Pool.borrowObject(Pool.java:93)
        at one.nio.rpc.RpcClient.invokeRaw(RpcClient.java:100)
        at one.nio.rpc.RpcClient.invoke(RpcClient.java:43)
        at one.nio.rpc.RpcClient.invoke(RpcClient.java:73)
        ... 4 more
Caused by: java.net.SocketTimeoutException
        at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:118)
        at one.nio.net.JavaSocket.connect(JavaSocket.java:63)
        at one.nio.net.Socket.connect(Socket.java:76)
        at one.nio.pool.SocketPool.createObject(SocketPool.java:135)
        ... 9 more

The demo likes this:

    public static void main(String[] args) throws Exception {
        Map<EchoService, EchoService> services = new HashMap<>();

        ConnectionString conn = new ConnectionString(args[0]);
        EchoService client = (EchoService) Proxy.newProxyInstance(
                EchoClient.class.getClassLoader(),
                new Class[] {EchoService.class},
                new RpcClient(conn));

        services.put(client, client); //  UndeclaredThrowableException thrown here
        // ... emitted
apangin commented 5 years ago

There is a bug in RpcClient proxy which is already fixed in our internal repository. I'll sync the repo soon.

At the same time RpcClient's built-in InvocationHandler is just a proof-of-concept implementation. It is supposed that applications will use their own handlers with application-specific error handling, balancing, routing, etc.

apangin commented 5 years ago

I've committed some RpcClient fixes. But generally UndeclaredThrowableException is not a problem of RpcClient. Any remote method can fail with I/O exception. If the interface does not declare it in throws, the exception will be wrapped in UndeclaredThrowableException.