Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- jobdispatcher
- firebase
- Background
- shceduler
- workmanager
- Job
- schedule
- epmty
- Service
- Android
- PHP
- livedatam
- 검사
- 빈
- jobschduler
- Library
- alarmanager
Archives
- Today
- Total
에몽이
netty 재연결코드 본문
There are situations where it makes sense to have a netty client doing a reconnect in case of a lost connection. There are 2 states where we need to do a reconnect:
- The client gets no connection at startup
- The client lost the connection during processing
Getting both situations handled we need to implement a ChannelFutureListener which handles the reconnect during the bootstrap phase and of course a ChannelHandler which we can use to get informed if the channel gets inactive.
We starting with a small bootstraping client:
public class Client
{
private EventLoopGroup loop = new NioEventLoopGroup();
public static void main( String[] args )
{
new Client().run();
}
public Bootstrap createBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoop) {
if (bootstrap != null) {
final MyInboundHandler handler = new MyInboundHandler(this);
bootstrap.group(eventLoop);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(handler);
}
});
bootstrap.remoteAddress("localhost", 8888);
bootstrap.connect().addListener(new ConnectionListener(this));
}
return bootstrap;
}
public void run() {
createBootstrap(new Bootstrap(), loop);
}
}
The createBootstrap method is public because we need it to be called from within the reconnect handler.
The following shows the ConnectionListener which is responsible for reconnecting during connect invocation. If the connect fails we schedule a reconnect within the eventloop of the channel which means basically create the bootstrap again and try another connect.
The same as we do in the connection listener is done in the ChannelHandler below. If we lost the connection to the server we schedule a reconnect from within the handler which is also basically just create a new bootstrap and try to connect again.
This is all to get a netty client with reconnection handling at least for me. I don't know if it is the right way to do it but it works at least for me. You are welcome to post better solutions or maybe the right solution or just some corrections.
The following shows the ConnectionListener which is responsible for reconnecting during connect invocation. If the connect fails we schedule a reconnect within the eventloop of the channel which means basically create the bootstrap again and try another connect.
public class ConnectionListener implements ChannelFutureListener {
private Client client;
public ConnectionListener(Client client) {
this.client = client;
}
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (!channelFuture.isSuccess()) {
System.out.println("Reconnect");
final EventLoop loop = channelFuture.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), loop);
}
}, 1L, TimeUnit.SECONDS);
}
}
}
The same as we do in the connection listener is done in the ChannelHandler below. If we lost the connection to the server we schedule a reconnect from within the handler which is also basically just create a new bootstrap and try to connect again.
public class MyInboundHandler extends SimpleChannelInboundHandler {
private Client client;
public MyInboundHandler(Client client) {
this.client = client;
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), eventLoop);
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
This is all to get a netty client with reconnection handling at least for me. I don't know if it is the right way to do it but it works at least for me. You are welcome to post better solutions or maybe the right solution or just some corrections.
'java' 카테고리의 다른 글
Java Annotation: 인터페이스 강요로부터 자유를… (0) | 2017.07.04 |
---|---|
우분투투 자바 컴파일하기 (0) | 2017.06.30 |
Apache HttpComponents를 이용한 GET, POST 요청하기 (0) | 2017.01.03 |
이클립스 외부 jar 추가 (0) | 2017.01.03 |
HttpURLConnection 클래스 - URL 요청후 응답받기 ( GET방식, POST방식 ) [출처] [Java] HttpURLConnection 클래스 - URL 요청후 응답받기 ( GET방식, POST방식 )|작성자 자바킹 (1) | 2017.01.03 |
Comments