加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Portlets,HttpSession和Thread-Safety

发布时间:2020-12-15 02:24:35 所属栏目:Java 来源:网络整理
导读:我们的portlet在HttpSession中保持状态,HttpSession由同一会话的所有请求处理线程共享. portlet规范(JSR-168)写道: PLT.5.2.4.3 Multithreading Issues During Request Handling The portlet container handles concurrent requests to the same portlet by
我们的portlet在HttpSession中保持状态,HttpSession由同一会话的所有请求处理线程共享.

portlet规范(JSR-168)写道:

PLT.5.2.4.3 Multithreading Issues During Request Handling

The portlet container handles concurrent requests to the same portlet by concurrent
execution of the request handling methods on different threads. Portlet developers must
design their portlets to handle concurrent execution from multiple threads from within the
processAction and render methods at any particular time.

我想知道我应该如何实现这一目标?当然,我可以使用同步在processAction和render期间实现互斥,但我不知道如何强制执行整个请求处理的原子性.特别是,我担心以下情况:

>线程1执行processAction,将数据加载到会话中以便以后呈现
>线程2执行processAction,从会话中丢弃该数据
>线程1执行渲染,读取要从会话渲染的数据,并抛出NullPointerException,因为准备好的数据不再存在…

通常如何阻止这种情况?特别是,当使用JBoss portlet桥来调整JSF到Portlet环境时?

解决方法

我会说如果有两个portlet在相同的数据上运行,特别是一个读取它而另一个删除它,那么很可能是设计中存在严重缺陷.

然后,您可能希望按portlet /线程存储数据,即如果portlet1读取某些数据,则应将其锁定,直到读取完成并使用唯一键将其放入会话中.

如果删除应呈现的数据是合法的,那么您应该考虑到这一点并在渲染期间再次检查.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读