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);
}
}









share|improve this question
























  • 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 T1 connect() fails.
    – Hel
    Nov 22 at 9:41

















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);
}
}









share|improve this question
























  • 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 T1 connect() fails.
    – Hel
    Nov 22 at 9:41















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);
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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 T1 connect() 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Lallio

Futebolista

Jornalista