教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 建筑文档 >

xsocket使用指南官方文档翻译(3)

来源:网络收集 时间:2026-08-03
导读: @Override public void run() { try { connection.write(\ } catch (Exception e) { //... } } } Server server = new Server(new ServerHandler()); server.start(); 21、连接池 客户端连接池可以通过避免连接重复

@Override public void run() { try {

connection.write(\ } catch (Exception e) { //... } } }

Server server = new Server(new ServerHandler()); server.start(); 21、连接池

客户端连接池可以通过避免连接重复创建来提高性能,典型的,连接池会在客户端使用,服务器会在一定时间内以一定的方式创建连接,通过缓存连接,可以避免重建连接的开销。

BlockingConnection和NonBlockingConnection都可以使用连接池,并且都支持SSL。

// create a limited connection pool

BlockingConnectionPool pool = new BlockingConnectionPool(); pool.setMaxActive(10);

IBlockingConnectionbc = null; try {

// retrieve a connection (if no connection // is in pool, a new one will be created) bc = pool.getBlockingConnection(host, port); bc.write(\ //...

// always close the connection! (the connection will be // returned into the connection pool) bc.close();

} catch (IOExceptionioe) { if (bc != null) { try {

// if the connection is invalid -> destroy it // (it will not return to the pool) pool.destroy(bc);

} catch (Exception ignore) { } } }

22、Flushing

默认情况下,autoflush是开启的,在这种方式下写方法会直接将要写的数据发送到下层子系统中。通过调用连接的setAutoFlush方法来改变连接行为。

autoflush示例: //client-side

IBlockingConnectionbc = new BlockingConnection(host, port); bc.write(text + \

String response = bc.readStringByDelimiter(\

//server-side

classServerHandler implements IDataHandler {

publicbooleanonData(INonBlockingConnectionnbc) throws IOException { String text = nbc.readStringByDelimiter(\nbc.write(text); nbc.write(\return true; } }

用户管理的flush示例: //client-side

IBlockingConnectionbc = new BlockingConnection(host, port); bc.setAutoflush(false);

bc.write(text + \bc.flush();

String response = bc.readStringByDelimiter(\

//server-side

classServerHandler implements IConnectHandler, IDataHandler {

// set autoflush with false (this behaviour can also // be controlled by server settings)

publicbooleanonConnect(INonBlockingConnectionnbc) throws IOException { nbc.setAutoflush(false); return true; }

publicbooleanonData(INonBlockingConnectionnbc) throws IOException { String text = nbc.readStringByDelimiter(\ //... nbc.write(text); nbc.write(\ //...

nbc.flush(); return true; } }

Autoflush自动将写方法的数据刷入到子系统,flushmode控制的flush的行为。如果设置为ASYNC,那么数据就会以异步的方式传递到下层系统。

因为使用了WriteByteChannel接口,write(ByteBuffer)和Write(ByteBuffer[])会在某些条件下退出。使用ASYNC调用这样的方法会通过内部I/O进程异步读取。吐过字节缓存在调用写方法之后被操作,那么竞争条件会发生。除非在字节缓存不会在写方法之后被操作的情况下,write才可以使用ASYNC调用。比如下面的情况,字节缓存会被重用被多次来复制数据,在这种情况下ASYNC是不可以使用的。一旦使用,竞争条件就会发生。

...

File file = new File(filename);

RandomAccessFileraf = new RandomAccessFile(file, \ReadableByteChannel fc = raf.getChannel();

IBlockingConnection connection = new BlockingConnection(host, port); // using a copy buffer (which will be reused for the read operations) // requires FlushMode SYNC which is default (for writing)! ByteBuffercopyBuffer = ByteBuffer.allocate(4096);

int read = 0;

while (read >= 0) { // read channel

read = fc.read(copyBuffer); copyBuffer.flip();

if (read > 0) {

// write channel

connection.write(copyBuffer); if (copyBuffer.hasRemaining()) { copyBuffer.compact(); } else { copyBuffer.clear(); } } }

26、性能推荐

阻塞和非阻塞通用:使用直接写的ByteBuffer来读取数据,使用设置系统属性usedirect为true,可以激活直接写。

org.xsocket.connection.client.readbuffer.usedirect=true org.xsocket.connection.server.readbuffer.usedirect=true 默认为false,因为有些Java虚拟机实现中存在问题。

阻塞和非阻塞通用:通过设置autoflush为false,可以手工控制数据冲洗。因为flush操作在下层系统上执行写和同步操作,flush操作是代价很大的。尤其是在一个事务中执行多个写时,用户执行的flush操作可以提高性能。

IBlockingConnectionbc= ... bc.setAutoflush(false);

bc.write(header); bc.write(fragment1); bc.write(fragement2); //...

bc.flush();

27、配置服务器

调用适当的设置器来动态配置参数。

// define the timeout, in which data have to be received srv.setIdleTimeoutMillis(2 * 60 * 1000);

// define the timeout for the connection

srv.setConnectionTimeoutMillis(30 * 60 * 1000);

// set the default autoflushbehaviour of the // server-side created connections srv.setAutoflush(false);

同样可配置work pool。Pool可以执行Handler的回调方法(比如onData,onConnect),在从套接字读取数据之后,xSocket的内部Dispatcher开始一个工作者线程池。Dispatcher线程负责套接字读写和函数回调。

通过合适的设置方法设置pool,通过java.util.concurrent.Executor接口实现。如果没有被设置,可以使用固定的线程池。

srv.setWorkerpool(Executors.newFixedThreadPool(20)); 28、同步调用Handler方法

Handler的回调方法会在一个同步的上下文中调用也就是说对于同一个连接而言,回调方法的调用都是串行的。默认的额工作者线程执行回调方法,通过设置Handler执行模式为NONTHREADED,Handler的回调会被xSocket的内部线程池调用。这将提高性能和规模,因为线程切换的开销会被避免。

…… 此处隐藏:2241字,全部文档内容请下载后查看。喜欢就下载吧 ……
xsocket使用指南官方文档翻译(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/681746.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)