2017-05-27 68 views
0

你好嗎?我有一個推一個細節更新,其中包含我的網格選項卡之後,我使用vaadin 8.0.4 ,谷歌Chrome更新,我的例子在這裏基於https://github.com/vaadin/archetype-application-exampleVaadin - grid.getDataProvider()。refreshAll();更新瀏覽器後不起作用

我的應用程序由存儲在MongoDB中的數據,當我在數據庫中每隔30秒用一次推送就直接更改數據庫,它總是在單個選項卡上工作,當我更新選項卡或創建新選項卡時,出現問題,推送似乎斷開連接並且我的網格不再更新,奇怪的是我添加了@PreserveOnRefresh,並且在訪問應用程序的第一個選項卡中,即使更新非常奇怪,推送仍然可用。

在我的分貝此實例檢查變化

private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5); 

我用的是網格更新,與

grid.getDataProvider().refreshAll() 

我甚至嘗試播放選項卡,通過圖形的方式在書12.16.4中描述。向其他用戶廣播 ,因爲如果活動通知在所有選項卡中繼續存在,則但網格不存在,僅在原始選項卡中。


更新: 在這個示例應用程序的問題實際上是登錄,當我刪除它,如果一切完美,因爲它應該與推動。但是,只有當我刪除登錄

@Override 
public void receiveBroadcast() { 
    access(() -> { 
    //Notification.show(message); 
    //grid.getDataProvider().refreshAll(); 
    getNavigator().removeView(MonitorCrudView.VIEW_NAME); 
    getNavigator().addView(MonitorCrudView.VIEW_NAME,new MonitorCrudView(this)); 
    getNavigator().navigateTo(MonitorCrudView.VIEW_NAME); 
    Notification.show("Grid updated", Notification.Type.TRAY_NOTIFICATION); 
    }); 
} 

的細節是,當我啓用了AccessContro,我作爲管理員的你所看到的輸入,執行上述方法時,我得到類型的異常「沒有請求鏈接到當前線程「;從 「當前用戶」 類 https://github.com/vaadin/archetype-application-example/blob/master/mockapp-ui/src/main/java/org/vaadin/mockapp/samples/authentication/CurrentUser.java

但在這裏vaadin 8.0.4即將變化不大

public final class CurrentUser { 

    /** 
    * The attribute key used to store the username in the session. 
    */ 
    public static final String CURRENT_USER_SESSION_ATTRIBUTE_KEY = CurrentUser.class 
      .getCanonicalName(); 

    private CurrentUser() { 
    } 

    /** 
    * Returns the name of the current user stored in the current session, or an 
    * empty string if no user name is stored. 
    * 
    * @throws IllegalStateException 
    *    if the current session cannot be accessed. 
    */ 
    public static String get() { 
     String currentUser = (String) getCurrentRequest().getWrappedSession() 
       .getAttribute(CURRENT_USER_SESSION_ATTRIBUTE_KEY); 
     if (currentUser == null) { 
      return ""; 
     } else { 
      return currentUser; 
     } 
    } 

    /** 
    * Sets the name of the current user and stores it in the current session. 
    * Using a {@code null} username will remove the username from the session. 
    * 
    * @throws IllegalStateException 
    *    if the current session cannot be accessed. 
    */ 
    public static void set(String currentUser) { 
     if (currentUser == null) { 
      getCurrentRequest().getWrappedSession().removeAttribute(
        CURRENT_USER_SESSION_ATTRIBUTE_KEY); 
     } else { 
      getCurrentRequest().getWrappedSession().setAttribute(
        CURRENT_USER_SESSION_ATTRIBUTE_KEY, currentUser); 
     } 
    } 

    private static VaadinRequest getCurrentRequest() { 
     VaadinRequest request = VaadinService.getCurrentRequest(); 
     if (request == null) { 
      throw new IllegalStateException(
        "No request bound to current thread"); 
     } 
     return request; 
    } 
} 

UPDATE:

Https://github.com/rucko24/testView/blob/master/MyApp-ui/src/main/java/example/samples/crud/SampleCrudView.java

在這個類中,我添加了向所有UI廣播的按鈕。

  • 以管理員身份登錄
  • 單擊更新網格
  • 應該給出一個類型的異常

java.util.concurrent.ExecutionException: 的java.lang。IllegalStateException異常:無請求勢必當前線程

  • 不會繼續刷新UI 但在隱身標籤頁打開時,它總是在更新前拋出異常後一次扔exeption。

  • 隨着基礎項目和蒙戈分貝加上

私有靜態ScheduledExecutorService的調度= Executors.newScheduledThreadPool(5);

我總是從上面得到相同的異常,從不與推更改視圖

+1

任何機會,你可以分享[sscce](http:// sscce。org)在github或類似的(忘記數據庫只是使用一些靜態數據),以及一系列步驟來重現您的問題? – Morfic

+0

非常感謝,非常感謝,我試圖在沒有mongodb的情況下重現錯誤,在應用程序本身的例子8.0.4中,但我仍然沒有實現它。 @Morfic –

+0

你好@Morfic你在某種程度上是hahahaha,我不知道是不是問得太多了,但你能看到example github中的代碼嗎? –

回答

1

免責聲明:這是更適合作爲註釋,但它不適合分配的空間。


VaadinService.getCurrentRequest() API doc指出:

電流響應不能在例如使用後臺線程,因爲服務器實現重用響應實例的方式。

與此同時,該UI.access() javadoc有些模棱兩可指出:

請注意,可運行可能會在不同的線程或更高版本在當前線程中調用,這意味着自定義線程局部變量當執行

以上陳述的命令那種解釋爲什麼VaadinService.getCurrentRequest()爲空在getCurrentRequest()方法可能沒有預期值。

儘管如此,似乎UI.getCurrent()在後臺線程運行時,會返回一個實例,也this vaadin forum post和vaadin書建議:

您的代碼不是線程安全的,因爲它不鎖VaadinSession在訪問UI之前。首選模式是使用UI.access和VaadinSession.access方法,如Book of Vaadin section 11.16.3.中所述在訪問塊內除了正確處理會話鎖定之外,Vaadin還會自動設置相關的線程位置。

總之,我建議全部更換到getCurrentRequest().getWrappedSession()UI.getCurrent().getSession()來電;如:

UI.getCurrent().getSession().getAttribute(CURRENT_USER_SESSI‌​ON_ATTRIBUTE_KEY); 

UI.getCurrent().getSession().setAttribute(CURRENT_USER_SESSI‌​ON_ATTRIBUTE_KEY, currentUser); 

我與樣品測試這和它的工作精細。

+0

非常感謝您的幫助,如果瀏覽器完全正常工作,並且使用我的ScheduledExecutorService(無需按鈕即可完成更新,現在使用grid.getDataProvider()),則可以更改瀏覽器。 RefreshAll();不刷新所有的用戶界面,但真正的第一件作品 –

相關問題