why does client reconnecting leads to double connections with netty
up vote
0
down vote
favorite
Client reconnects to server when connection is closed with Netty. But every time when connection is lost, client will reconnnect using two threads. Reconnection leads to double connections. How can I solve the problem?
public class ImConnection {
private static Logger logger = LoggerFactory.getLogger(ImConnection.class);
private static Bootstrap b;
private Channel channel;
static {
b = new Bootstrap();
EventLoopGroup workerGroup = new NioEventLoopGroup();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ConnectionInitializer());
}
public Channel connect() {
doConnect(PropertyConstant.cfgcServerIP, PropertyConstant.cfgcServerPort);
return this.channel;
}
private void doConnect(String host, int port) {
try {
ChannelFuture f = b.connect(host, port);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
final EventLoop loop = future.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
try {
connect(); // reconnect if fail
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1L, TimeUnit.SECONDS);
} else {
logger.info("connected");
}
}});
channel = f.channel();
} catch(Exception e) {
e.printStackTrace();
}
}
}
handler initializer
public class ConnectionInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ConnectionHandler());
}
}
handler to reconnect when connection is lost
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection();
// get executed when connection drops
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("connection lost");
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
imConnection.connect();
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
java netty
add a comment |
up vote
0
down vote
favorite
Client reconnects to server when connection is closed with Netty. But every time when connection is lost, client will reconnnect using two threads. Reconnection leads to double connections. How can I solve the problem?
public class ImConnection {
private static Logger logger = LoggerFactory.getLogger(ImConnection.class);
private static Bootstrap b;
private Channel channel;
static {
b = new Bootstrap();
EventLoopGroup workerGroup = new NioEventLoopGroup();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ConnectionInitializer());
}
public Channel connect() {
doConnect(PropertyConstant.cfgcServerIP, PropertyConstant.cfgcServerPort);
return this.channel;
}
private void doConnect(String host, int port) {
try {
ChannelFuture f = b.connect(host, port);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
final EventLoop loop = future.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
try {
connect(); // reconnect if fail
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1L, TimeUnit.SECONDS);
} else {
logger.info("connected");
}
}});
channel = f.channel();
} catch(Exception e) {
e.printStackTrace();
}
}
}
handler initializer
public class ConnectionInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ConnectionHandler());
}
}
handler to reconnect when connection is lost
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection();
// get executed when connection drops
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("connection lost");
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
imConnection.connect();
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
java netty
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
@OomphFortuity I think T1 willconnect()just once, fail or succeed. And its listener gets executed to re-connect when T1connect()fails.
– Hel
Nov 22 at 9:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Client reconnects to server when connection is closed with Netty. But every time when connection is lost, client will reconnnect using two threads. Reconnection leads to double connections. How can I solve the problem?
public class ImConnection {
private static Logger logger = LoggerFactory.getLogger(ImConnection.class);
private static Bootstrap b;
private Channel channel;
static {
b = new Bootstrap();
EventLoopGroup workerGroup = new NioEventLoopGroup();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ConnectionInitializer());
}
public Channel connect() {
doConnect(PropertyConstant.cfgcServerIP, PropertyConstant.cfgcServerPort);
return this.channel;
}
private void doConnect(String host, int port) {
try {
ChannelFuture f = b.connect(host, port);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
final EventLoop loop = future.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
try {
connect(); // reconnect if fail
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1L, TimeUnit.SECONDS);
} else {
logger.info("connected");
}
}});
channel = f.channel();
} catch(Exception e) {
e.printStackTrace();
}
}
}
handler initializer
public class ConnectionInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ConnectionHandler());
}
}
handler to reconnect when connection is lost
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection();
// get executed when connection drops
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("connection lost");
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
imConnection.connect();
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
java netty
Client reconnects to server when connection is closed with Netty. But every time when connection is lost, client will reconnnect using two threads. Reconnection leads to double connections. How can I solve the problem?
public class ImConnection {
private static Logger logger = LoggerFactory.getLogger(ImConnection.class);
private static Bootstrap b;
private Channel channel;
static {
b = new Bootstrap();
EventLoopGroup workerGroup = new NioEventLoopGroup();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ConnectionInitializer());
}
public Channel connect() {
doConnect(PropertyConstant.cfgcServerIP, PropertyConstant.cfgcServerPort);
return this.channel;
}
private void doConnect(String host, int port) {
try {
ChannelFuture f = b.connect(host, port);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
final EventLoop loop = future.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
try {
connect(); // reconnect if fail
} catch (Exception e) {
e.printStackTrace();
}
}
}, 1L, TimeUnit.SECONDS);
} else {
logger.info("connected");
}
}});
channel = f.channel();
} catch(Exception e) {
e.printStackTrace();
}
}
}
handler initializer
public class ConnectionInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ConnectionHandler());
}
}
handler to reconnect when connection is lost
public class ConnectionHandler extends ChannelInboundHandlerAdapter {
private ImConnection imConnection = new ImConnection();
// get executed when connection drops
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("connection lost");
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
imConnection.connect();
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
java netty
java netty
edited Nov 22 at 9:30
asked Nov 22 at 8:33
Hel
10218
10218
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
@OomphFortuity I think T1 willconnect()just once, fail or succeed. And its listener gets executed to re-connect when T1connect()fails.
– Hel
Nov 22 at 9:41
add a comment |
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
@OomphFortuity I think T1 willconnect()just once, fail or succeed. And its listener gets executed to re-connect when T1connect()fails.
– Hel
Nov 22 at 9:41
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
@OomphFortuity I think T1 will
connect() just once, fail or succeed. And its listener gets executed to re-connect when T1 connect() fails.– Hel
Nov 22 at 9:41
@OomphFortuity I think T1 will
connect() just once, fail or succeed. And its listener gets executed to re-connect when T1 connect() fails.– Hel
Nov 22 at 9:41
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426761%2fwhy-does-client-reconnecting-leads-to-double-connections-with-netty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How ImConnecton() class get ChannelHandlerContext to create event loop? I think you are calling connect() & reConnect() call on two different event loops, Hence two threads(event loop thread) create two connections
– Oomph Fortuity
Nov 22 at 9:11
@OomphFortuity Once connection lost, thread 1 tries to connect and listener waits for the connecting result. If connecting fails, the listener fires a new connection task. I can't find two conncurrent connecting.
– Hel
Nov 22 at 9:23
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections.
– Oomph Fortuity
Nov 22 at 9:30
You are calling connect() in T1 and Listener calling reconnect() in T2. Example T1's connection fails it reconnects on T2 with its Listener calling reconnect() in T2. Here you have two open threads T1 and T2. Again it fails then you can see two connections because your Bootstrap will have two Listeners for reconnect()
– Oomph Fortuity
Nov 22 at 9:36
@OomphFortuity I think T1 will
connect()just once, fail or succeed. And its listener gets executed to re-connect when T1connect()fails.– Hel
Nov 22 at 9:41