Network 客户端 出现线程重复创建怎么处理?

kakaj666 2018-09-28 09:29:28
代码逻辑 一个ribbitmq消费者 一收到消息后就将消息发送给某个Network Server端 问题是 出现重复创建了很多线程 运行10小时左右就程序挂掉了 java.net.SocketException: No buffer space available (maximum connections reached?): connect 必须重启后就可以继续跑

消费者类
	@RabbitListener(queues="SlzrDatatransferModel.Bj")// 
public void receive(byte[]strMsg) throws UnsupportedEncodingException{
log.info("------监听消息开始---------");

final NettyClient nettyClient = new NettyClient("122.27.11.160",6856);
nettyClient.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

try {
nettyClient.sendMsg(strMsg);
Thread.sleep(3000);
} catch (SendException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

}


NettyClient 类
public class NettyClient extends Thread{
/**
* 服务端地址
*/
private String ipAdress;
/**
* 服务端端口
*/
private Integer port;


private ChannelFuture channelFuture;

public NettyClient(String ipAdress, Integer port) {
this.ipAdress = ipAdress;
this.port = port;
if (port == null || "".equals(ipAdress)){
throw new RuntimeException("");
}

}

@Override
public void run() {
try {
init();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void init() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>(){
@Override
public void initChannel(SocketChannel ch) throws Exception{
ch.pipeline().addLast("decoder", new ClientFilter());
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("handler", new BinaryHandler());
}
});
channelFuture = b.connect(ipAdress, port).sync();
channelFuture.channel().closeFuture().sync();
}



public void sendMsg(byte[] msg) throws SendException {
if (channelFuture != null && channelFuture.channel().isOpen()){
// try {
ChannelFuture sync = channelFuture.channel().
writeAndFlush(Unpooled.copiedBuffer(msg));
// } catch (InterruptedException e) {
// e.printStackTrace();
// }


}
// throw new SendException("send is error ! ");
}


}


感觉是EventLoopGroup 是一直被重复创建了 大佬们怎么改???
...全文
618 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
koftt 2018-10-09
  • 打赏
  • 举报
回复
定义在外面 静态常量
yql1986 2018-09-28
  • 打赏
  • 举报
回复
你这逻辑有问题啊,每次收到消息,就要创建一个NettyClient 实例,然后发送消息 应该是NettyClient 实例只需要实例化一次就可以了吧,然后跟服务器保持长连接即可 每次收到ribbitmq消息,就调用这个NettyClient 实例发送消息就可以了。
kakaj666 2018-09-28
  • 打赏
  • 举报
回复
引用 1 楼 yql1986 的回复:
你这逻辑有问题啊,每次收到消息,就要创建一个NettyClient 实例,然后发送消息
应该是NettyClient 实例只需要实例化一次就可以了吧,然后跟服务器保持长连接即可

每次收到ribbitmq消息,就调用这个NettyClient 实例发送消息就可以了。
NettyClient确是应该只实例化一次 ,单独建立了个启动就运行的类 这个类里面去new NettyClient创建, 但是在监听消息的类里 nettyClient.sendMsg null异常 获取不到新建启动类里的实例, 不知道还有没有其他办法 只实例化一次 监听类里面去直接调用sendMsg 这个方法 怎么改...?

50,331

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧