小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java – 如何向線程池的舊線程提供當(dāng)前的vaadin會(huì)話?

 印度阿三17 2019-06-28

我在理解VaadinSession.getCurrent()的引用后面臨一個(gè)問(wèn)題

Gets the currently used session. The current session is automatically
defined when processing requests to the server and in threads started
at a point when the current session is defined (see
InheritableThreadLocal). In other cases, (e.g. from background threads
started in some other way), the current session is not automatically
defined.

具體以下幾點(diǎn)對(duì)我來(lái)說(shuō)很難理解,

… in threads started
at a point when the current session is defined (see
InheritableThreadLocal).

那是什么意思 ?

我的理解是,如果在定義新會(huì)話之前創(chuàng)建線程,或者在舊會(huì)話中創(chuàng)建線程,則它將不會(huì)引用當(dāng)前新創(chuàng)建的會(huì)話.

目前我有線程池,一些線程指的是現(xiàn)在關(guān)閉的舊會(huì)話然后我將面臨在這些線程中使用會(huì)話的問(wèn)題.

我正在使用帶vaadin的spring,特別是@Async方法調(diào)用.

@Async
public static methodX() {
    //I have used session inside it
}

問(wèn)題是,thread1已被用于執(zhí)行methodX,現(xiàn)在我已經(jīng)使用了session session1,在用戶注銷后,這個(gè)session1將被關(guān)閉.

現(xiàn)在,當(dāng)新用戶登錄到具有session2的系統(tǒng)并再次使用thread1執(zhí)行此方法時(shí),此方法仍然使用session1而不是session2,并且當(dāng)方法嘗試從關(guān)閉的session1獲取數(shù)據(jù)時(shí)會(huì)產(chǎn)生問(wèn)題.

我的問(wèn)題:

>為什么Vaadin無(wú)法提供或通知舊線程(即屬于舊(已關(guān)閉)會(huì)話的線程)有關(guān)新定義的會(huì)話?
>為這些線程提供會(huì)話的最佳方法是什么?

解決方法:

這意味著如果您在單獨(dú)的線程中處理數(shù)據(jù),則不會(huì)有當(dāng)前會(huì)話:

所以這段代碼:

Runtime runtime = Runtime.getRuntime();                 
final Process process = runtime.exec(action);
new Thread() {
    public void run() {
        try {
            System.out.println(VaadinSession.getCurrent());
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}.start();

在您的vaadin項(xiàng)目中將打印一個(gè)舊會(huì)話,即Thread啟動(dòng)時(shí)的會(huì)話.

編輯

我認(rèn)為Vaadin無(wú)法為舊線程提供新會(huì)話以避免數(shù)據(jù)損壞問(wèn)題.我的意思是如果5個(gè)Threads正在編輯同一個(gè)會(huì)話,你將遇到問(wèn)題……

您的解決方案是為池中的每個(gè)線程提供Session,但我真的不知道如何實(shí)現(xiàn)這一點(diǎn),Google到目前為止還沒(méi)有給我答案.

編輯:另一種可能的解決方

使用HashMap< int,VaadinSession>創(chuàng)建一個(gè)類來(lái)存儲(chǔ)每個(gè)Sessions(使用SessionListener).以靜態(tài)的方式.

創(chuàng)建線程時(shí),請(qǐng)為其提供需要使用的會(huì)話的ID(id是與HashMap中所需會(huì)話對(duì)應(yīng)的鍵).然后每次編輯,銷毀會(huì)話時(shí),只需在HashMap中編輯它.

由于此HashMap的靜態(tài)行為,您可以隨時(shí)從任何線程訪問(wèn)它,您唯一需要的是與線程中的會(huì)話相對(duì)應(yīng)的int id.

來(lái)源:https://www./content-1-276551.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多