搜索
首页后端开发php教程Apache Mina 学习笔记(4)-Session

Apache Mina 学习笔记(4)-Session

Jan 18, 2017 am 09:59 AM
apacheMinasession

Session是Apache的核心,每当一个客户端连接到达时,就会有一个新的Session被创建,直到该连接关闭。Session被用来保存连接,以及各种信息。

Session有如下几种状态:

Connected : the session has been created and is available
Idle : the session hasn't processed any request for at least a period of time (this period is configurable)

Idle for read : no read has actually been made for a period of time
Idle for write : no write has actually been made for a period of time
Idle for both : no read nor write for a period of time

Closing : the session is being closed (the remaining messages are being flushed, cleaning up is not terminated)
Closed : The session is now closed, nothing else can be done to revive it.

下图表示Session的状态转换关系:

313.png

以下几个参数可以用来配置Session

receive buffer size
sending buffer size
Idle time
Write timeOut

管理用户自定义的属性:

例如,如果你想跟踪一个用户从会话被建立之后发送了多少个请求,那么它可以很容易存入这种映射:只要创建一个key关联到value就行。

...  
int counterValue = session.getAttribute( "counter" );  
session.setAttribute( "counter", counterValue + 1 );  
...

我们采用key/value 对的方式存储属性到会话中,这种key/value对可以通过session的容器读取,添加或删除。

定义container

正如我们所说,这个容器是一个key/value容器,默认是一种映射,当然也可以定义成其他的数据结构。当Session被创建时我们可以实现一个接口和一个factory用来创建容器。下面的代码片段示例了在Session初始化时如何创建容器(看不懂,这个到底什么意思?)

protected final void initSession(IoSession session,  
        IoFuture future, IoSessionInitializer sessionInitializer) {  
    ...  
    try {  
        ((AbstractIoSession) session).setAttributeMap(session.getService()  
                .getSessionDataStructureFactory().getAttributeMap(session));  
    } catch (IoSessionInitializationException e) {  
        throw e;  
    } catch (Exception e) {  
        throw new IoSessionInitializationException(  
                "Failed to initialize an attributeMap.", e);  
    }

and here is the factory interface we can implement if we want to define another kind of container :

public interface IoSessionDataStructureFactory {  
    /** 
     * Returns an {@link IoSessionAttributeMap} which is going to be associated 
     * with the specified <tt>session</tt>.  Please note that the returned 
     * implementation must be thread-safe. 
     */  
     IoSessionAttributeMap getAttributeMap(IoSession session) throws Exception;  
 }

过滤链

每个会话会关联一些过滤链,用来处理到来的请求或者出去的数据。每个会话都会指定单独的过滤链,大多数情况下,我们会用在会话中用很多相同的过滤链。

统计

Each session also keep a track of records about what has been done for the session :

number of bytes received/sent
number of messages received/sent
Idle status
throughput

and many other useful informations.

Handler

最后,同样重要的是,一个Handler要附着于一个Session上,用来处理程序的消息。这个Handler也会发送包作为回应,只要简单的调用write方法即可:

...  
session.write( <your message> );  
...

以上就是Apache Mina 学习笔记(4)-Session的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使PHP应用程序更快如何使PHP应用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能优化清单:立即提高速度PHP性能优化清单:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

PHP依赖注入:提高代码可检验性PHP依赖注入:提高代码可检验性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能优化:数据库查询优化PHP性能优化:数据库查询优化May 12, 2025 am 12:02 AM

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

简单指南:带有PHP脚本的电子邮件发送简单指南:带有PHP脚本的电子邮件发送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能:识别和修复瓶颈PHP性能:识别和修复瓶颈May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

PHP的依赖注入:快速摘要PHP的依赖注入:快速摘要May 11, 2025 am 12:09 AM

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

提高PHP性能:缓存策略和技术提高PHP性能:缓存策略和技术May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!