zhao yao
zhao yao

Reputation: 21

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 aAttributeMap 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.

Upvotes: 2

Views: 638

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23557

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

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

Upvotes: 1

Related Questions