简体   繁体   中英

Implementing a bootstrap pool in Netty 4.x

I'm trying to implement a async redis client using netty 4.x. After reading the netty source code and doc, it seems like it's more efficient to use the same NioEventLoop[s] which I use as childEventLoop in ServerBootstrap. The question is it seems like I need to attach such a RedisClientPool to each NioChildEventLoop , otherwise I can not share those cached connections, but the EventLoop is not a AttributeMap . I tried to extend the NioEventLoop and overwrite the newChild to return a custom NioChildEventLoop (just copied the codes, because it is declared as final) with a AttributeMap field so I can share some objects through it.

public class NioChildEventLoopWithAttributeMap extends SingleThreadEventLoop {

    private AttributeMap map = new DefaultAttributeMap();

    public <T> Attribute<T> attr(AttributeKey<T> key) {
        return map.attr(key);
    }

    //.... omit the copied codes
}

and

public class CustomNioEventLoop extends NioEventLoop {
    public EventExecutor newChild() {
        return new NioChildEventLoopWithAttributeMap();
    }
}

But in AbstractChannel it checks the EventLoop

protected boolean isCompatible(EventLoop loop) {
    return loop instanceof NioChildEventLoop;
}

I have no idea what to do now, any suggestions? Sorry for my terrible english.

Just for the record there is a redis-codec for netty4 and netty3 at github:

https://github.com/spullara/redis-protocol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM