mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
Merge pull request #473 from jasonhx140/master
connect mysql server failed when reconnect mysql server frequently
This commit is contained in:
@@ -54,31 +54,25 @@ public abstract class SocketChannelPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static SocketChannel open(SocketAddress address) throws Exception {
|
public static SocketChannel open(SocketAddress address) throws Exception {
|
||||||
final SocketChannel socket = new SocketChannel();
|
SocketChannel socket = null;
|
||||||
final BooleanMutex mutex = new BooleanMutex(false);
|
ChannelFuture future = boot.connect(address).sync();
|
||||||
boot.connect(address).addListener(new ChannelFutureListener() {
|
|
||||||
|
|
||||||
@Override
|
if (future.isSuccess()) {
|
||||||
public void operationComplete(ChannelFuture arg0) throws Exception {
|
future.channel().pipeline().get(BusinessHandler.class).latch.await();
|
||||||
if (arg0.isSuccess()) {
|
socket = chManager.get(future.channel());
|
||||||
socket.setChannel(arg0.channel());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
mutex.set(true);
|
if (null == socket) {
|
||||||
}
|
|
||||||
});
|
|
||||||
// wait for complete
|
|
||||||
mutex.get();
|
|
||||||
if (null == socket.getChannel()) {
|
|
||||||
throw new IOException("can't create socket!");
|
throw new IOException("can't create socket!");
|
||||||
}
|
}
|
||||||
chManager.put(socket.getChannel(), socket);
|
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class BusinessHandler extends ChannelInboundHandlerAdapter {
|
public static class BusinessHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
private SocketChannel socket = null;
|
private SocketChannel socket = null;
|
||||||
|
private final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
@@ -86,11 +80,22 @@ public abstract class SocketChannelPool {
|
|||||||
chManager.remove(ctx.channel());// 移除
|
chManager.remove(ctx.channel());// 移除
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
socket = new SocketChannel();
|
||||||
|
socket.setChannel(ctx.channel());
|
||||||
|
chManager.put(ctx.channel(), socket);
|
||||||
|
latch.countDown();
|
||||||
|
super.channelActive(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
if (null == socket) socket = chManager.get(ctx.channel());
|
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
socket.writeCache((ByteBuf) msg);
|
socket.writeCache((ByteBuf) msg);
|
||||||
|
} else {
|
||||||
|
//TODO: need graceful error handler.
|
||||||
|
logger.error("no socket available.");
|
||||||
}
|
}
|
||||||
ReferenceCountUtil.release(msg);// 添加防止内存泄漏的
|
ReferenceCountUtil.release(msg);// 添加防止内存泄漏的
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user