Netty 2

 

Netty Channel 의 pipline 으로 수행되는

ChannelHandler 가 제공하는 메서드를 하나씩 정리해보겠다.

 

ChannelInboundHandler

inbound I/O 이벤트를 처리하기 위한 ChannelHandler 이다.

inbound I/O 이벤트가 발생되면 각 이벤트에 맞는 아래 메서드를 호출 해준다.

 

void channelRegistered(ChannelHandlerContext ctx) throws Exception;

Netty Channel 이 EventLoop 에 등록되는 경우 호출된다.

- Netty ServerSocketChannel 의 경우 서버를 시작하며 EventLoop 에 최초 1회 등록할 것이므로

그 때 1회 호출 될 것이다.

- Netty SocketChannel 의 경우 ServerSocketChannel 에서 accept 이벤트 발생시 EventLoop 에 등록하게 되므로

accept 이벤트 발생 시점마다 호출 될 것이다.

 

void channelUnregistered(ChannelHandlerContext ctx) throws Exception;

Netty Channel 이 EventLoop 에서 제거 되는 경우 호출된다.

 

void channelActive(ChannelHandlerContext ctx) throws Exception;

Netty Channel 이 활성화(데이터 입출력 가능 상태) 되는 경우 호출된다.

 

void channelInactive(ChannelHandlerContext ctx) throws Exception;

Netty Channel 이 비활성화(데이터 입출력 불가한 상태) 되는 경우 호출된다.

 

void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;

Netty Channel 에 읽을 데이터가 준비 되었을 때 호출된다.

 

ServerSocketChannel 의 경우

- accept 가 되었다는 뜻으로 msg 로 SocketChannel 이 전달된다.

 

SocketChannel 의 경우

- read 가 되었다는 뜻으로 msg 로 ByteBuf 가 전달된다.

 

void channelReadComplete(ChannelHandlerContext ctx) throws Exception;

Netty Channel 에 더이상 읽을 데이터가 없을 때 호출된다.

 

void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;

User event 가 트리거 된 경우 호출된다.

 

void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception;

Netty Channel 이 쓰기 가능 상태가 변경될 경우 1회 호출된다.

 

 

Netty Channel 의 라이프 사이클에 따라

위 메서드들의 호출 순서는 다음과 같다.

 

1. channelRegistered

2. channelActive

3. channelRead

4. channelReadComplete

5. channelInactive

6. chennelUnregistered

 

 

 

 

ChannelOutboundHandler

outbound I/O 이벤트를 처리 하기 위한 ChannelHandler 이다.

outbound I/O 이벤트가 발생되면 각 이벤트에 맞는 아래 메서드를 호출 해준다.

 

참고

inbound 에서는 Client 로 부터 이벤트가 트리거 되는 방식이라 

Netty Channel 에서 ChannelPipline 이 순차적으로 수행된다고 생각하면 되지만,

outbound 에서의 I/O 이벤트 트리거는 보통 server application 내부에서 발생될 것이다.

예를 들면, write 의 경우 inbound Handler 일 가능성이 높고, bind 의 경우 main 함수 일 가능성이 높을 것이다.

 

 

 

void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception;

Netty ServerSocketChannel 에 bind 요청 시 호출 된다. (서버 측)

 

void connect(
        ChannelHandlerContext ctx, SocketAddress remoteAddress,
        SocketAddress localAddress, ChannelPromise promise) throws Exception;

Netty SocketChannel 에 connect 요청 시 호출 된다. (클라이언트 측)

 

void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

Netty SocketChannel 에 disconnect 요청 시 호출 된다. (클라이언트 측)

 

void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

Netty Channel 에 close 요청 시 호출 된다.

 

void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;

Netty Channel 이 현재 등록된 EventLoop 로 부터 deregister 되면 호출 된다.

 

void read(ChannelHandlerContext ctx) throws Exception;

Netty Channel 에 read 요청 시 호출 된다.

 

void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception;

Netty Channel 에 write 요청 시 호출 된다.

보통 inbound ChannelHandler 에서 ctx.writeAndFlush(response); 를 호출하면

Netty Channel 에 write 요청을 한 것이고 결과적으로 해당 메서드가 호출된다.

msg 로는 inbound 에서 전달한 response 가 그대로 전달된다.

 

void flush(ChannelHandlerContext ctx) throws Exception;

Netty Channel 에 flush 요청 시 호출 된다.

 

 

데이터 전송 시, outbound I/O 의 일반적인 호출 순서는 다음과 같다.

1. write

2. flush

3. close

 

 

 

주의사항

ChannelHandlerContext 를 통해 outbound I/O 를 트리거 한 경우(비즈니스 완료)..

addListener 를 통해 리소스 해제를 꼭 해주는 습관을 가져야한다.

ctx.writeAndFlush(~~).addListener(ChannelFutureListener.CLOSE);

 

또한, inbound I/O 에서 channelRead 를 통해 전달되는 ByteBuf 객체 역시 해제 해주는 것을 잊지말자..

ReferenceCountUtil.release(msg);

 

'Spring Reactive Stack > Netty' 카테고리의 다른 글

Netty 1  (0) 2024.01.17
Proactor pattern  (0) 2023.11.20
Reactor pattern  (0) 2023.11.19