2017-08-14 45 views
1

在信道的初始化劑,Netty的:可以得到靜態空閒狀態處理器在管道和通道未來聽者

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

在上述片的代碼時的信道初始化我可以使用IdleStateHandler的靜態對象,而不是使用的每個頻道的新實例。線程安全嗎?

另外,

當我寫一些東西給頻道。 我給它增加了一個空閒的讀取處理程序,這樣如果我沒有收到響應,我會關閉通道。

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

我可以使用上面這段代碼的靜態idleReadHandler?

我使用了Netty-4.1.0

它標誌着JBoss的文檔共享,直到網狀3.X https://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/IdleStateHandler.html 但在4.x的文檔不看

回答

1

在網狀,規則確定一個處理程序是否可用於多個通道之間的線程安全是@Shareable註釋。由於IdleStateHandler未使用註釋進行註釋,因此意味着它無法安全使用。

+0

這是,但它不是在4.x文檔 https://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/timeout/IdleStateHandler.html。所以這是故意的還是偶然錯過? – Peter

+1

它不能共享,所以你需要創建一個新的實例,如Ferrybig所說。 –