|
@@ -1,15 +1,20 @@
|
|
package com.warewms.hailiang.connect;
|
|
package com.warewms.hailiang.connect;
|
|
|
|
|
|
|
|
+import com.github.rholder.retry.*;
|
|
import com.warewms.hailiang.connect.handler.InkjetPrintersHandler;
|
|
import com.warewms.hailiang.connect.handler.InkjetPrintersHandler;
|
|
import io.netty.bootstrap.Bootstrap;
|
|
import io.netty.bootstrap.Bootstrap;
|
|
-import io.netty.channel.ChannelFuture;
|
|
|
|
-import io.netty.channel.ChannelInitializer;
|
|
|
|
-import io.netty.channel.ChannelOption;
|
|
|
|
-import io.netty.channel.EventLoopGroup;
|
|
|
|
|
|
+import io.netty.channel.*;
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
import io.netty.channel.socket.SocketChannel;
|
|
import io.netty.channel.socket.SocketChannel;
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
|
+import lombok.CustomLog;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.util.concurrent.Callable;
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created with IntelliJ IDEA.
|
|
* Created with IntelliJ IDEA.
|
|
@@ -33,6 +38,8 @@ public class InkjetPrintersConnect implements TCPConnectBase {
|
|
|
|
|
|
private EventLoopGroup group;
|
|
private EventLoopGroup group;
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void init() throws InterruptedException {
|
|
public void init() throws InterruptedException {
|
|
log.info("喷码机正在进行连接");
|
|
log.info("喷码机正在进行连接");
|
|
@@ -47,14 +54,51 @@ public class InkjetPrintersConnect implements TCPConnectBase {
|
|
socketChannel.pipeline().addLast(new InkjetPrintersHandler());
|
|
socketChannel.pipeline().addLast(new InkjetPrintersHandler());
|
|
}
|
|
}
|
|
});
|
|
});
|
|
- //发起异步连接操作
|
|
|
|
future = bootstrap.connect(IP_ADDR, PORT).sync();
|
|
future = bootstrap.connect(IP_ADDR, PORT).sync();
|
|
- future.channel().closeFuture().sync();
|
|
|
|
- bootstrap.clone();
|
|
|
|
- } finally {
|
|
|
|
- group.shutdownGracefully();
|
|
|
|
|
|
+ future.addListener((channelFuture) -> {
|
|
|
|
+ if (channelFuture.isSuccess()) {
|
|
|
|
+ log.info("喷码机连接成功");
|
|
|
|
+ }else {
|
|
|
|
+ log.info("连接失败等待重试!");
|
|
|
|
+ retry();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("喷码设备连接异常,{},准备重试", e.getMessage());
|
|
|
|
+ retry();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void retry() {
|
|
|
|
+ Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
|
|
|
|
+ .retryIfResult(Boolean.FALSE::equals)
|
|
|
|
+ .retryIfExceptionOfType(Exception.class)
|
|
|
|
+ .withStopStrategy(StopStrategies.neverStop())
|
|
|
|
+ .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
|
|
|
|
+ .build();
|
|
|
|
+ try {
|
|
|
|
+ retryer.call(new Callable<Boolean>() {
|
|
|
|
+ AtomicBoolean isSuccess = new AtomicBoolean(false);
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean call() throws Exception {
|
|
|
|
+ log.info("喷码机连接重试");
|
|
|
|
+ future = bootstrap.connect(IP_ADDR, PORT).sync();
|
|
|
|
+ future.addListener((channelFuture) -> {
|
|
|
|
+ isSuccess.set(channelFuture.isSuccess());
|
|
|
|
+ if (channelFuture.isSuccess()) {
|
|
|
|
+ log.info("喷码机连接成功");
|
|
|
|
+ }else {
|
|
|
|
+ log.info("连接失败等待重试!");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ Thread.sleep(3000);
|
|
|
|
+ return isSuccess.get();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } catch (RetryException | ExecutionException e) {
|
|
|
|
+ e.printStackTrace();
|
|
}
|
|
}
|
|
- log.info("喷码机连接成功");
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|