2014-05-17 2 views
0

내 서버를 개발하는 데 netty를 사용하고 있습니다. 또한 netty에서 Idle 상태 처리를 구현하고 있습니다. 나는 그것이 최근에 발견 한 문제를 가지고있다. userEventTriggered 메서드 내에서 채널 컨텍스트 특성에 액세스 할 수 없습니다. 여기 내 코드이며 아무도 왜 그것이 가능하지 않은지 말해 줄 수 있습니다.내부에서 ChannelHandlerContext.attr에 액세스 할 수 없습니다 .EventTriggered

나는

Agent agent = ctx.attr(CLIENT_MAPPING).get(); 

그러나 내부가 null가 반환 userEventTriggered (이 완벽하게 작동한다)

public static final AttributeKey<Agent> CLIENT_MAPPING = AttributeKey.valueOf("clientMapping"); 
... 
ctx.attr(CLIENT_MAPPING).set(agent); 

및 처리기 안에, 내가 좋아하는 값을 얻고있다처럼 설정하고있다. (이 함수가 호출되기 전에 설정되어 있다고 확신합니다).

public class Server 
{ 
    ... 

    public void run() throws Exception 
    { 
     ... 
      ServerBootstrap b = new ServerBootstrap(); 
      b.group(bossGroup, workerGroup). 
        channel(NioServerSocketChannel.class). 
        childHandler(new SslServerInitializer()); 
     ... 
    } 
} 

class SslServerInitializer extends ChannelInitializer<SocketChannel> 
{ 
    @Override 
    public void initChannel(SocketChannel ch) throws Exception 
    { 
     ChannelPipeline pipeline = ch.pipeline(); 
     .... 
     pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 0, Integer.parseInt(Main.configurations.get("netty.idleTimeKeepAlive.ms")))); 
     pipeline.addLast("idleTimeHandler", new ShelloidIdleTimeHandler()); 
    } 
} 

class ShelloidIdleTimeHandler extends ChannelDuplexHandler 
{ 
    @Override 
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception 
    { 
     if (evt instanceof IdleStateEvent) 
     { 
      try 
      { 
       // This I am getting null, but I confirmed that I set the attribute from my handler and is accessible inside handler. 
       Agent agt = ctx.attr(WebSocketSslServerHandler.CLIENT_MAPPING).get(); 
       ctx.channel().writeAndFlush(new TextWebSocketFrame("{\"type\":\"PING\", \"userId\": \"" + agt.getUserId() + "\"}")); 
      } 
      catch (Exception ex) 
      { 
       ctx.disconnect(); 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 

답변

0

동일한 ChannelHandler에 설정 했습니까? 다른 ChannelHandler에서 설정하고 가져 오려면 Channel.attr (...)을 사용해야합니다.

+0

예. 클라이언트가 처음으로 연결되어있는 동안 ChannelHandlerContext 특성을 설정하고 있습니다. 따라서 채널이 유휴 상태 일 때마다 해당 ChannelHandlerContext로 userEventTriggered를 호출합니다. – Harikrishnan

+0

여전히 속성을 가져올 수 없습니다. – Harikrishnan

+0

예. 나는 Channel.attr (............)에 의해 그것을 얻었다. – Harikrishnan

관련 문제