简体   繁体   中英

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. 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. This below is my simple example:

I have a simple page that retrieve 2 list of "String" (Hazelcast Distributed List and Session List):

Index Page

The Bean behind the page has a custom scope.

@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:

Click for view Example

Only 2 Strings are shared in Session List, while all Strings with 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).

Can I fix the problem with "setAttribute" method?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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