2017-11-17 7 views
0

나는 netty를 사용 해본 적이 없어서 ChannelPipeline의 문서에 대해 질문이 있습니다. 여기가 정확히에 관한 것입니다 : 내가 꽤 MyBusinessLogicHandler 클래스를 이해하지 못하는비즈니스 로직에 netty 사용하기

static final EventExecutorGroup group = new DefaultEventExecutorGroup(16); 
... 

ChannelPipeline pipeline = ch.pipeline(); 

pipeline.addLast("decoder", new MyProtocolDecoder()); 
pipeline.addLast("encoder", new MyProtocolEncoder()); 

// Tell the pipeline to run MyBusinessLogicHandler's event handler methods 
// in a different thread than an I/O thread so that the I/O thread is not blocked by 
// a time-consuming task. 
// If your business logic is fully asynchronous or finished very quickly, you don't 
// need to specify a group. 
pipeline.addLast(group, "handler", new MyBusinessLogicHandler()); 

. DuplexChannelHandler 또는 다른 것을 구현해야합니까?

public class Packet{} 

가 좀 Packet 생산 MyBusinessLogicHandler에 제공, Packet에 일부 바이트 시퀀스를 디코딩 할 : 예를 들어, 내 특정 경우에 나는 다음과 같은 클래스가 있습니다. 바이트 스트림으로 다시 인코딩하여 클라이언트로 다시 보냅니다. 나는 이것을 다음과 같이보고 있습니다 :

public class MyBusinessLogicHandler extends SimpleChannelInboundHandler<MyModel>{ 
    public void channelRead0(ChannelHandlerContext ctx, Packet msg){ 
     Packet rslt = null; 
     //Do some complicated business logic 
     ctx.write(rslt); 
    } 
} 

netty에서 일하는 것이 일반적인 방법인지 잘 모르겠습니다. 제발 좀 더 명확하게 해줄 수 있니?

답변

2

예감. MyBusinessLogicHandler을 구현하는 것은 완전히 정확하고 올바른 방법입니다. 당신은 이미 MyProtocolEncoder (나는 ChannelOutboundHandler을 구현한다고 가정합니다)과 같이 DuplexChannelHandler을 필요로하지 않습니다. ctx.write(rslt)으로 전화를 걸면 아웃 바운드 처리기에 대한 쓰기 이벤트가 트리거됩니다.

DuplexChannelHandler은 동일한 클래스의 인코더와 디코더를 모두 구현하려는 경우에만 유용합니다. 예를 들어 MyProtocolEncoderMyProtocolDecoder에 가입하면됩니다.

+1

알았습니다! 감사. –