Peter
Peter

Reputation: 2759

Netty: Can I give static Idle state handler in pipeline and channel future listener

In channel Initialiser ,

protected void initChannel(SocketChannel ch) throws Exception {
        MessageHandler handler = new MessageHandler(channelGroup);
        ch.pipeline().addLast(DECODER, new MessageDecoder())
                .addLast(ENCODER, newMessageEncoder())
                .addLast(idleExecutor, "idleHandler", new IdleStateHandler(0, 0, 6*60))
                .addLast(pipelineExecutor, "handler", handler);
    }

In the above piece of code when channel initialises can I use a static object of IdleStateHandler instead of using a new instance for every channel. Is it thread safe ?

Also,

When I write something to a channel. I add a idle read handler to it , so that if I don't get a response withing sometime , I close the channel.

     ChannelPipeline pipeline = channel.pipeline();
      pipeline.addAfter(ChannelInitializer.idleExecutor, 
"idleHandler", "idleReadHandler",new IdleStateHandler(60, 0, 0));

Can I use static idleReadHandler in above piece of code ?

I am using Netty-4.1.0

It was marked sharable in jboss docs until netty 3.x https://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/IdleStateHandler.html But don't see in 4.x docs

Upvotes: 1

Views: 424

Answers (1)

Ferrybig
Ferrybig

Reputation: 18834

In netty, the rule that determines if a handler can be used thread safe between multiple channels is the @Shareable annotation. Since IdleStateHandler is not annotated with the annotation, it means it cannot be safely used.

Upvotes: 1

Related Questions