簡體   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