Netty自带连接池的使用
一、类介绍
1.ChannelPool——连接池接口
2.SimpleChannelPool——实现ChannelPool接口,简单的连接池实现
3.FixedChannelPool——继承SimpleChannelPool,有大小限制的连接池实现
4.ChannelPoolMap——管理host与连接池映射的接口
5.AbstractChannelPoolMap——抽象类,实现ChannelPoolMap接口
二、具体使用
a、MyNettyPool——Netty自带连接池的用法
- package com.dxfx.netty.demo;
- import com.alibaba.fastjson.JSONObject;
- import com.dxfx.netty.framework.Constants;
- import com.dxfx.netty.framework.DefaultFuture;
- import com.dxfx.netty.framework.NettyClientHandler;
- import com.dxfx.netty.param.RequestParam;
- import com.dxfx.netty.param.Response;
- import io.netty.bootstrap.Bootstrap;
- import io.netty.channel.Channel;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.pool.AbstractChannelPoolMap;
- import io.netty.channel.pool.ChannelPoolHandler;
- import io.netty.channel.pool.ChannelPoolMap;
- import io.netty.channel.pool.FixedChannelPool;
- import io.netty.channel.socket.nio.NioSocketChannel;
- import io.netty.handler.codec.DelimiterBasedFrameDecoder;
- import io.netty.handler.codec.Delimiters;
- import io.netty.handler.codec.string.StringDecoder;
- import io.netty.handler.codec.string.StringEncoder;
- import io.netty.util.concurrent.Future;
- import io.netty.util.concurrent.FutureListener;
- /**
- * Netty自带连接池的用法
- *
- * @author Administrator
- *
- */
- public class MyNettyPool {
- // key为目标host,value为目标host的连接池
- public static ChannelPoolMap<String, FixedChannelPool> poolMap;
- private static final Bootstrap bootstrap = new Bootstrap();
- static {
- bootstrap.group(new NioEventLoopGroup());
- bootstrap.channel(NioSocketChannel.class);
- bootstrap.remoteAddress("localhost", 8080);
- }
- public MyNettyPool() {
- init();
- }
- /**
- * netty连接池使用
- *
- */
- public void init() {
- poolMap = new AbstractChannelPoolMap<String, FixedChannelPool>() {
- @Override
- protected FixedChannelPool newPool(String key) {
- ChannelPoolHandler handler = new ChannelPoolHandler() {
- /**
- * 使用完channel需要释放才能放入连接池
- *
- */
- @Override
- public void channelReleased(Channel ch) throws Exception {
- // 刷新管道里的数据
- // ch.writeAndFlush(Unpooled.EMPTY_BUFFER); // flush掉所有写回的数据
- System.out.println("channelReleased......");
- }
- /**
- * 当链接创建的时候添加channelhandler,只有当channel不足时会创建,但不会超过限制的最大channel数
- *
- */
- @Override
- public void channelCreated(Channel ch) throws Exception {
- ch.pipeline().addLast(new StringEncoder());
- ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
- ch.pipeline().addLast(new StringDecoder());
- // 绑定channel的handler
- ch.pipeline().addLast(new NettyClientHandler());
- }
- /**
- * 获取连接池中的channel
- *
- */
- @Override
- public void channelAcquired(Channel ch) throws Exception {
- System.out.println("channelAcquired......");
- }
- };
- return new FixedChannelPool(bootstrap, handler, 50); //单个host连接池大小
- }
- };
- }
- /**
- * 发送请求
- *
- * @param msg
- * 请求参数
- * @param command
- * 请求方法
- * @return
- */
- public Response send(final Object msg, final String command) {
- //封装请求数据
- final RequestParam request = new RequestParam();
- request.setCommand(command);
- request.setContent(msg);
- //从连接池中获取连接
- final FixedChannelPool pool = poolMap.get("localhost");
- //申请连接,没有申请到或者网络断开,返回null
- Future<Channel> future = pool.acquire();
- future.addListener(new FutureListener<Channel>() {
- @Override
- public void operationComplete(Future<Channel> future) throws Exception {
- //给服务端发送数据
- Channel channel = future.getNow();
- channel.write(JSONObject.toJSONString(request));
- channel.writeAndFlush(Constants.DELIMITER);
- System.out.println(channel.id());
- // 连接放回连接池,这里一定记得放回去
- pool.release(channel);
- }
- });
- //获取服务端返回的数据
- DefaultFuture defaultFuture = new DefaultFuture(request);
- return defaultFuture.get();
- }
- }
b、MyNettyPoolTest——Netty自带连接池测试类,SpringServer为连接池启动类
- package com.dxfx.netty.demo;
- import com.dxfx.netty.param.Response;
- import com.dxfx.user.model.User;
- /**
- * Netty自带连接池测试类,SpringServer为连接池启动类
- *
- * @author Administrator
- *
- */
- public class MyNettyPoolTest {
- public static void main(String[] args) {
- User user = new User();
- user.setAge(12);
- user.setId(23);
- user.setName("client");
- String command = "login";
- MyNettyPool pool = new MyNettyPool();
- new MyThread(pool, user, command).start();
- new MyThread(pool, user, command).start();
- new MyThread(pool, user, command).start();
- new MyThread(pool, user, command).start();
- for (int i = 0; i < 50000; i++) {
- new MyThread(pool, user, command).start();
- }
- }
- }
- class MyThread extends Thread {
- public MyNettyPool pool;
- public Object msg;
- public String command;
- public MyThread(MyNettyPool pool, Object msg, String command) {
- super();
- this.pool = pool;
- this.msg = msg;
- this.command = command;
- }
- @Override
- public void run() {
- Response response = pool.send(msg, command);
- //System.out.println(response);
- }
- }
Netty自带连接池的使用的更多相关文章
- 带连接池的netty客户端核心功能实现剖解
带连接池的netty客户端核心功能实现剖析 带连接池的netty的客户端核心功能实现剖析 本文为原创,转载请注明出处 源码地址: https://github.com/zhangxianwu/ligh ...
- springboot使用自带连接池连接postgre
Application配置spring.datasource.url=jdbc:postgresql://***:5432/postgresspring.datasource.username=pos ...
- Hibernate查询、连接池、二级缓存
Hibernate第三天: 1. 对象状态 2. session缓存 3. lazy懒加载 4. 映射 一对一对映射 组件/继承映射 目标: 一.hibernate查询 二.hibernate对连接池 ...
- Hibernate的查询,二级缓存,连接池
Hibernate的查询,二级缓存,连接池 1.Hibernate查询数据 Hibernate中的查询方法有5中: 1.1.Get/Load主键查询 使用get或者load方法来查询,两者之间的区别在 ...
- Hibernate【查询、连接池、逆向工程】
前言 在Hibernate的第二篇中只是简单地说了Hibernate的几种查询方式....到目前为止,我们都是使用一些简单的主键查询阿...使用HQL查询所有的数据....本博文主要讲解Hiberna ...
- OpenResty 高阶实战之————Redis授权登录使用短连接(5000)和长连接(500W) 使用连接池AB压力测试结果
一.短连接开始测试 ab -n 5000 -c 100 -k 127.0.0.1/test_redis_short #demo1 Concurrency Level: Time taken for t ...
- php连接池 php–cp
原文地址:http://blog.sina.com.cn/s/blog_9eaa0f400102v9fd.html 数据库连接池php-cp介绍时间 2015-01-23 11:53:05 数据库连接 ...
- 非常老的话题 SQLSERVER连接池
原文:非常老的话题 SQLSERVER连接池 非常老的话题 SQLSERVER连接池 写这篇文章不是说要炒冷饭,因为园子里有非常非常多关于SQLSERVER连接池的文章,但是他们说的都是引用MSDN里 ...
- 带有连接池的Http客户端工具类HttpClientUtil
一.背景 业务开发中,经常会遇到通过http/https向下游服务发送请求.每次都要重复造轮子写HttpClient的逻辑,而且性能.功能参差不齐.这里分享一个高性能的.带连接池的通用Http客户端工 ...
随机推荐
- vector容器用法详解
vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...
- windows平台下的oracle ORA-01031的解决方法
今天下午遇到一个很怪异的问题,在windows平台下sqlplus / as sysdba登陆数据库,提示权限不足, 当时就纳闷了,sys用户登陆数据库还能权限不足,问题出现了,就开始寻找解决方法呗 ...
- jenkins可选插件为空的解决方式
我是安装的jenkins,文件名字是这个jenkins.msi,点finish安装完成,然后浏览器就自动打开了jenkins页面,输入密码后,便进入了如下页面 之后我是选择的跳过插件安装,在可选插件里 ...
- 20175316盛茂淞 迭代和JDB
迭代和JDB 题目 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图(至少三张:正常如c( ...
- js中加“var”和不加“var”的区别
JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: var x // x 为 undefined var x = 6; // x 为数字 var x = "Bill&q ...
- pycharm 如何进行全部搜索
界面里面先按ctrl F 弹出搜索页面 在搜索框内连续按两次shift shift可以搜索全文
- Word中的段落
Word文档中的块级内容的最基本单位是段落,段落用<p>元素进行存储.段落定义在新行中开始,段落可以包含三方面的信息:可选的段落属性.内嵌的内容(通常为文本)和用于比较两个文档的内容的一组 ...
- js-function作用域
你能猜出先弹出什么吗? <!DOCTYPE html> <html lang="en"><head> <meta charset=&quo ...
- 有关C++模板inline的高性能在lambda与function的体现
前两天在群里跟人讨论到写库时对于lambda和function的取舍,跑了写测试查了些资料后基本得出结论: 如果没有自由变量的情况下,一般不要用function. 如果有自由变量的话,C++中的lam ...
- Linux-程序包管理
Linux上的软件安装有2种形式:源码.二进制文件,源码需要在编译环境下编译安装,二进制可以直接安装. 1.程序包管理器 rpm 程序包管理器能够将目标二进制格式(也就是从源码编译好的二进制文件,包括 ...