netty连接池FixedChannelPool应用
来源:互联网 发布:app 源码 编辑:程序博客网 时间:2024/05/21 11:18
********************************************************************************************
Discription: 连接池DEMO
Written By: ZXZ
Date: 2017-04-21
Version: 1.0
********************************************************************************************
*/
package com.zny.common.netty.connectpool;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.pool.FixedChannelPool;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
public class ConnectPoolManager {
private final NioEventLoopGroup group = new NioEventLoopGroup();
private final Bootstrap bs = new Bootstrap();
private FixedChannelPool fixpool = null;
private InetSocketAddress remoteaddress = null;
public ConnectPoolManager(String host, int port, int maxconnect) {
bs.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true);
remoteaddress = InetSocketAddress.createUnresolved(host, port);
bs.remoteAddress(remoteaddress);
fixpool = new FixedChannelPool(bs, new DemoPoolHandler(), maxconnect);
//创建时,打开最多连接数
/*Channel[] chlist = new Channel[maxconnect];
for(int i=0; i<maxconnect; i++) {
chlist[i] = acquire(1);
}
for(int i=0; i<maxconnect; i++) {
release(chlist[i]);
}*/
}
//申请连接,没有申请到(或者网络断开),返回null
public Channel acquire(int seconds) {
try {
Future<Channel> fch = fixpool.acquire();
Channel ch = fch.get(seconds, TimeUnit.SECONDS);
return ch;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//释放连接
public void release(Channel channel) {
try {
if (channel != null) {
fixpool.release(channel);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public DemoChannelHandler getChannelHandler(Channel channel) {
ChannelPipeline pipeline = channel.pipeline();
DemoChannelHandler ret = (DemoChannelHandler)(pipeline.last());
return ret;
}
//单元测试
public static void main(String[] args) {
try {
int maxconnect = 10;
ConnectPoolManager pool = new ConnectPoolManager("127.0.0.1", 5001, maxconnect);
Channel ch = pool.acquire(5);
pool.release(ch);
Channel[] chlist = new Channel[maxconnect];
for(int i=0; i<maxconnect; i++) {
chlist[i] = pool.acquire(1);
}
for(int i=0; i<maxconnect; i++) {
pool.release(chlist[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
package ConnectPool;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.socket.nio.NioSocketChannel;
public class DemoPoolHandler implements ChannelPoolHandler {
@Override
public void channelReleased(Channel ch) throws Exception {
System.out.println("channelReleased");
}
@Override
public void channelAcquired(Channel ch) throws Exception {
System.out.println("channelAcquired");
}
@Override
public void channelCreated(Channel ch) throws Exception {
System.out.println("channelCreated");
NioSocketChannel channel = (NioSocketChannel) ch;
channel.config().setKeepAlive(true);
channel.config().setTcpNoDelay(true);
ChannelPipeline pipeline = channel.pipeline();
DemoChannelHandler handler = new DemoChannelHandler();
pipeline.addLast(handler);
}
}
package ConnectPool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@Sharable
public class DemoChannelHandler extends ChannelInboundHandlerAdapter {
private ChannelHandlerContext chctx;
private final ByteBuf firstMessage;
public DemoChannelHandler() {
firstMessage = Unpooled.buffer(50);
for (int i = 0; i < firstMessage.capacity(); i ++) {
firstMessage.writeByte((byte)(i+48));
}
}
public boolean isWritable() {
return this.chctx.channel().isWritable();
}
public void close() {
if (chctx != null) {
chctx.close();
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
this.chctx = ctx;
ctx.writeAndFlush(firstMessage);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
this.chctx = ctx;
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
}
- netty连接池FixedChannelPool应用
- Netty连接池ChannelPool,FixedChannelPool应用
- netty实战-netty client连接池设计
- netty实战-netty client连接池设计
- netty应用
- netty的http client连接池设计
- netty(七)netty入门应用
- netty4.x FixedChannelPool使用
- Netty 长连接服务
- Netty 长连接服务
- netty长连接实例
- Netty 长连接服务
- Netty 长连接服务
- Netty 长连接服务
- netty 服务端连接分析
- Netty入门应用
- 开发Netty应用
- Netty简单应用
- B1014. 福尔摩斯的约会 (20')
- Unity 3D
- FZU
- python paramiko模块实现远程文件上传文件, 执行命令
- ubuntu 安装SVN版本控制器使用
- netty连接池FixedChannelPool应用
- haproxy加密长度报警
- 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器(图形界面版)
- jQuery特性操作
- XP系统CreateMutex返回0,LastError=6
- 陈越何欣铭老师数据结构PTA08-图8 How Long Does It Take
- Mac下Docker学习笔记之定制Tomcat镜像
- RTP协议分析
- iOS推送