Commit 905a535b authored by zengtianlai3's avatar zengtianlai3

优化tcp客户端线程

parent ee847bc3
package iot.sixiang.license.forward; package iot.sixiang.license.forward;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import iot.sixiang.license.net.TcpClient; import iot.sixiang.license.net.TcpClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component @Component
@Slf4j @Slf4j
public class ForwardClient { public class ForwardClient {
...@@ -12,6 +19,7 @@ public class ForwardClient { ...@@ -12,6 +19,7 @@ public class ForwardClient {
private TcpClient client = null; private TcpClient client = null;
private ForwardChannelInitializer channelInitializer; private ForwardChannelInitializer channelInitializer;
private Bootstrap bootstrap;
@Autowired @Autowired
ForwardClientHandler handler; ForwardClientHandler handler;
...@@ -20,15 +28,31 @@ public class ForwardClient { ...@@ -20,15 +28,31 @@ public class ForwardClient {
} }
@PostConstruct
public void init(){
channelInitializer = new ForwardChannelInitializer(handler);
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
bootstrap = new Bootstrap();
if (bootstrap != null) {
try {
bootstrap
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.group(eventLoopGroup)
.handler(channelInitializer);
} catch (IllegalStateException ex) {
log.error(ex.getMessage());
}
}
}
public void startTcp(String host, int port, String appId) { public void startTcp(String host, int port, String appId) {
log.debug("桥接客户端,开始连接桥接服务:{},{},{}", host, port, appId); log.debug("桥接客户端,开始连接桥接服务:{},{},{}", host, port, appId);
ForwardConnectionListener listener = new ForwardConnectionListener(); ForwardConnectionListener listener = new ForwardConnectionListener();
listener.setAppId(appId); listener.setAppId(appId);
listener.setHost(host); listener.setHost(host);
listener.setPort(port); listener.setPort(port);
client = new TcpClient(host, port, channelInitializer, listener, bootstrap);
channelInitializer = new ForwardChannelInitializer(handler);
client = new TcpClient(host, port, channelInitializer, listener);
client.start(); client.start();
} }
......
...@@ -2,10 +2,6 @@ package iot.sixiang.license.net; ...@@ -2,10 +2,6 @@ package iot.sixiang.license.net;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
...@@ -14,38 +10,22 @@ public class TcpClient { ...@@ -14,38 +10,22 @@ public class TcpClient {
private String host; private String host;
private BaseChannelInitializer channelInitializer; private BaseChannelInitializer channelInitializer;
private BaseConnectionListener connectionListener; private BaseConnectionListener connectionListener;
private Bootstrap bootstrap;
public TcpClient(String host, int port,BaseChannelInitializer channelInitializer,BaseConnectionListener connectionListener) { public TcpClient(String host, int port, BaseChannelInitializer channelInitializer, BaseConnectionListener connectionListener, Bootstrap bootstrap) {
this.host = host; this.host = host;
this.port = port; this.port = port;
this.channelInitializer = channelInitializer; this.channelInitializer = channelInitializer;
this.connectionListener = connectionListener; this.connectionListener = connectionListener;
this.bootstrap = bootstrap;
} }
public void start() { public void start() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.group(eventLoopGroup)
.remoteAddress(host, port)
.handler(channelInitializer);
} catch (NullPointerException e) {
log.error(e.getMessage());
}catch (IllegalStateException ex) {
log.error(ex.getMessage());
}
ChannelFuture future = bootstrap.connect(host, port); ChannelFuture future = bootstrap.connect(host, port);
future.addListener(connectionListener); future.addListener(connectionListener);
}
});
thread.start();
} }
......
package iot.sixiang.license.operate; package iot.sixiang.license.operate;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import iot.sixiang.license.net.TcpClient; import iot.sixiang.license.net.TcpClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component @Component
@Slf4j @Slf4j
public class OperateClient { public class OperateClient {
...@@ -12,7 +19,7 @@ public class OperateClient { ...@@ -12,7 +19,7 @@ public class OperateClient {
private TcpClient client = null; private TcpClient client = null;
private OperateChannelInitializer channelInitializer; private OperateChannelInitializer channelInitializer;
private Bootstrap bootstrap;
@Autowired @Autowired
OperateClientHandler handler; OperateClientHandler handler;
...@@ -21,13 +28,30 @@ public class OperateClient { ...@@ -21,13 +28,30 @@ public class OperateClient {
} }
@PostConstruct
public void init(){
channelInitializer = new OperateChannelInitializer(handler);
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
bootstrap = new Bootstrap();
if (bootstrap != null) {
try {
bootstrap
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.group(eventLoopGroup)
.handler(channelInitializer);
} catch (IllegalStateException ex) {
log.error(ex.getMessage());
}
}
}
public void startTcp(String host, int port) { public void startTcp(String host, int port) {
OperateConnectionListener listener = new OperateConnectionListener(); OperateConnectionListener listener = new OperateConnectionListener();
listener.setHost(host); listener.setHost(host);
listener.setPort(port); listener.setPort(port);
channelInitializer = new OperateChannelInitializer(handler); client = new TcpClient(host, port, channelInitializer, listener, bootstrap);
client = new TcpClient(host, port, channelInitializer, listener);
client.start(); client.start();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment