Parcourir la source

解决在重连过程中其他线程介入连接 导致延迟重连将已经连好的连接切断重连

xiuwei il y a 4 ans
Parent
commit
7da7d7bf1d

+ 0 - 1
protocol-core/src/main/java/wei/yigulu/netty/AbstractDelimiterHandler.java

@@ -128,7 +128,6 @@ public abstract class AbstractDelimiterHandler extends ChannelInboundHandlerAdap
 	 */
 	protected void mergeOrFlushByTimeSpan(ByteBuf byteBuf){
 		if (timeMark.plusMillis(maxTimeSpace).isBeforeNow()) {
-			System.out.println(cumulation);
 			log.warn("上一帧数据长度不足,但两帧时间间隔较长上一帧被舍弃 舍弃的数据帧为:" + DataConvertor.ByteBuf2String(cumulation));
 			while (!cumulation.release()) {
 			}

+ 1 - 1
protocol-core/src/main/java/wei/yigulu/netty/AbstractMasterBuilder.java

@@ -91,7 +91,7 @@ public abstract class AbstractMasterBuilder extends BaseProtocolBuilder {
 	/**
 	 * 创建Master 连接
 	 */
-	public abstract void create();
+	public  abstract void create();
 
 	/**
 	 * Create by un block

+ 17 - 13
protocol-core/src/main/java/wei/yigulu/netty/HSConnectionListener.java

@@ -42,20 +42,24 @@ public class HSConnectionListener implements ChannelFutureListener {
 		if (channelFuture == null || channelFuture.channel() == null || !channelFuture.channel().isActive()) {
 			this.masterBuilder.getOrCreateWorkGroup().schedule(() -> {
 				try {
-					if (this.retryTimes < 10) {
-						log.error("服务端{}:{}链接不上,开始重连操作,第{}次尝试", this.masterBuilder.getIp(), this.masterBuilder.getPort(), retryTimes);
-						masterBuilder.create();
-						log.warn("重试连接失败");
-						this.retryTimes++;
-					} else {
-						if (!StringUtil.isNullOrEmpty(this.masterBuilder.getSpareIp()) || (this.masterBuilder.getSparePort() != null && this.masterBuilder.getSparePort() != 0)) {
-							log.info("服务端{}:{}链接不上,切换主备机{}:{}", this.masterBuilder.getIp(), this.masterBuilder.getPort(), this.masterBuilder.getSpareIp(), this.masterBuilder.getSparePort());
-							this.masterBuilder.switchover();
+					if(masterBuilder.future==null ||!masterBuilder.future.channel().isActive()) {
+						if (this.retryTimes < 10) {
+							log.error("服务端{}:{}链接不上,开始重连操作,第{}次尝试", this.masterBuilder.getIp(), this.masterBuilder.getPort(), retryTimes);
+							masterBuilder.create();
+							log.warn("重试连接失败");
+							this.retryTimes++;
+						} else {
+							if (!StringUtil.isNullOrEmpty(this.masterBuilder.getSpareIp()) || (this.masterBuilder.getSparePort() != null && this.masterBuilder.getSparePort() != 0)) {
+								log.info("服务端{}:{}链接不上,切换主备机{}:{}", this.masterBuilder.getIp(), this.masterBuilder.getPort(), this.masterBuilder.getSpareIp(), this.masterBuilder.getSparePort());
+								this.masterBuilder.switchover();
+							}
+							this.masterBuilder.refreshLoopGroup();
+							this.retryTimes = 0;
+							masterBuilder.create();
+							log.info("重置重试次数=0");
 						}
-						this.masterBuilder.refreshLoopGroup();
-						this.retryTimes = 0;
-						masterBuilder.create();
-						log.info("重置重试次数=0");
+					}else {
+						log.warn("masterBuilder在延迟过程中已由其他线程连接成功,此处略过重连");
 					}
 				} catch (Exception e) {
 					log.error("ModbusMaster重试连接时发生异常", e);

+ 11 - 3
protocol-core/src/main/java/wei/yigulu/netty/SimpleTcpConnectionListener.java

@@ -22,6 +22,8 @@ public class SimpleTcpConnectionListener implements ChannelFutureListener {
 
 	private AbstractTcpMasterBuilder masterBuilder;
 
+	ScheduledFuture<?> future;
+
 	/**
 	 * Only host connection listener
 	 *
@@ -36,10 +38,14 @@ public class SimpleTcpConnectionListener implements ChannelFutureListener {
 	@Override
 	public void operationComplete(ChannelFuture channelFuture) throws Exception {
 		if (channelFuture == null || channelFuture.channel() == null || !channelFuture.channel().isActive()) {
-			 this.masterBuilder.getOrCreateWorkGroup().schedule(() -> {
+			this.future = this.masterBuilder.getOrCreateWorkGroup().schedule(() -> {
 				try {
-					log.error("服务端{}:{}链接不上,开始重连操作", this.masterBuilder.getIp(), this.masterBuilder.getPort());
-					masterBuilder.create();
+					if(masterBuilder.future==null ||!masterBuilder.future.channel().isActive()) {
+						log.error("服务端{}:{}链接不上,开始重连操作", this.masterBuilder.getIp(), this.masterBuilder.getPort());
+						masterBuilder.create();
+					}else{
+						log.warn("masterBuilder在延迟过程中已由其他线程连接成功,此处略过重连");
+					}
 				} catch (Exception e) {
 					log.error("TcpMaster重试连接时发生异常", e);
 					try {
@@ -49,6 +55,8 @@ public class SimpleTcpConnectionListener implements ChannelFutureListener {
 					}
 				}
 			}, 6L, TimeUnit.SECONDS);
+		}else{
+			log.warn("masterBuilder已经连接成功,不进行重连操作");
 		}
 	}
 }