简体   繁体   English

使用session.setAttribute复制会话对象

[英]Session Object replication with session.setAttribute

I have a Cluster as Standalone Instances with Hazelcast and Payara, I added a Session Replication and a Load Balancer with HAProxy. 我使用Hazelcast和Payara将群集作为独立实例,并使用HAProxy添加了会话复制和负载均衡器。 Everything works fine. 一切正常。

When I use a Session Bean all global variables are replicated to the nodes of the cluster but when I try to share an object in a no-SessionBean a few times it doesn't work. 当我使用Session Bean时,所有全局变量都复制到集群的节点上,但是当我尝试在no-SessionBean中共享对象几次时,它将无法正常工作。 This below is my simple example: 这是我的简单示例:

I have a simple page that retrieve 2 list of "String" (Hazelcast Distributed List and Session List): 我有一个简单的页面,可检索2个“字符串”列表(Hazelcast分布式列表和会话列表):

Index Page 索引页

The Bean behind the page has a custom scope. 页面后面的Bean具有自定义范围。

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

The "increase" method is called when I click on the "add" button: 当我单击“添加”按钮时,将调用“增加”方法:

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);
    }
}

After 4 clicks on the button, the situazione is this: 在单击按钮4次之后,situazione是这样的:

Click for view Example 点击查看示例

Only 2 Strings are shared in Session List, while all Strings with Hazelcast. 会话列表中仅共享2个字符串,而所有带有Hazelcast的字符串均共享。 I need to use my custom scope and in same case the objects must be shared only in the session and not in application (as Hazelcast Distributed List). 我需要使用自定义范围,并且在相同情况下,对象只能在会话中共享,而不能在应用程序中共享(如Hazelcast Distributed List)。

Can I fix the problem with "setAttribute" method? 我可以使用“ setAttribute”方法解决问题吗?

Thanks in advance for the support. 预先感谢您的支持。

In order to make session replication working over Hazelcast in Payara Server, you need to enable Web Container Availability over Hazelcast. 为了使会话复制在Payara Server中的Hazelcast上正常工作,您需要在Hazelcast上启用Web容器可用性。 See this screenshot . 查看此屏幕截图

You also need to include the <distributable/> element into the web.xml in your application, otherwise the session will not be distributed. 您还需要在应用程序的web.xml中包含<distributable/>元素,否则该会话将不会被分发。

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

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