繁体   English   中英

使用session.setAttribute复制会话对象

[英]Session Object replication with session.setAttribute

我使用Hazelcast和Payara将群集作为独立实例,并使用HAProxy添加了会话复制和负载均衡器。 一切正常。

当我使用Session Bean时,所有全局变量都复制到集群的节点上,但是当我尝试在no-SessionBean中共享对象几次时,它将无法正常工作。 这是我的简单示例:

我有一个简单的页面,可检索2个“字符串”列表(Hazelcast分布式列表和会话列表):

索引页

页面后面的Bean具有自定义范围。

@Named
@RomeoScoped  //this is my custom scope
public class RomeoBean implements Serializable {

当我单击“添加”按钮时,将调用“增加”方法:

public void increase(){
    FacesContext currentInstance = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) currentInstance.getExternalContext().getRequest();
    HttpSession session = request.getSession();

    String example  = Math.random() + "";

    if(session != null){
       CopyOnWriteArrayList<String> list = (CopyOnWriteArrayList<String>) session.getAttribute("List");
       list.add(example);
       session.setAttribute("List", list);
    }

    try {
       Context ctx = new InitialContext();
       HazelcastInstance instance = (HazelcastInstance)    ctx.lookup("payara/Hazelcast");
       IList<String> list = instance.getList("list");
       list.add(example);
    } catch (NamingException ex) {
       Logger.getLogger(RomeoBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

在单击按钮4次之后,situazione是这样的:

点击查看示例

会话列表中仅共享2个字符串,而所有带有Hazelcast的字符串均共享。 我需要使用自定义范围,并且在相同情况下,对象只能在会话中共享,而不能在应用程序中共享(如Hazelcast Distributed List)。

我可以使用“ setAttribute”方法解决问题吗?

预先感谢您的支持。

为了使会话复制在Payara Server中的Hazelcast上正常工作,您需要在Hazelcast上启用Web容器可用性。 查看此屏幕截图

您还需要在应用程序的web.xml中包含<distributable/>元素,否则该会话将不会被分发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM