异步任务完成时打开p:对话框

时间:2015-09-23 05:22:59

标签: jsf asynchronous jsf-2 primefaces dialog

我需要一种技术,我可以调用java方法从DB检索信息并在对话框中显示消息,然后按"下一步"按钮,如果有任何下一条消息,它将出现。该java方法在特定时间后自动调用,第一次用户登录时说,然后是3分钟后。这是我想要的JSF& PrimeFaces。

我使用ScheduleExecutarService进行调度,我的运行方法如下所示

public void run() {
  try {
       System.out.println("beep");
       notificationList = retrieveNotificationsFromDB(userId, groupCode, functionCode, chooseNotificationType());

       if(notificationList != null && !notificationList.isEmpty()){                                 
            showPopup();
       }
    } catch(Exception e) {
          LOGGER.error(e.getMessage());
     }
}

public void showPopup() {
    if (notificationList != null && !notificationList.isEmpty()) {
        setNotificatiomMessage(notificationList.get(0).getMessage());
        notificationList.remove(0);
        RequestContext.getCurrentInstance().execute("PF('dialogVar').show()");
    }
}

showPopup()我无法获得RequestContext的对象

其他方式我试过<p:poll>标签,我读到关于标签是否有停止它的问题。

请指导我应该使用的内容。

3 个答案:

答案 0 :(得分:1)

  

在showPopup()中,我无法获取RequestContext的对象

因为此时没有活动的HTTP请求(从客户端到服务器)。

<强>解决方案:

让Scheduler触发CDI事件,并在客户端bean中使用CDI观察者捕获此事件。

现在您必须选择:让JSF视图轮询您的客户端bean以请求新消息,或者您创建一个套接字,客户端bean可以通过该套接字向JSF视图通知新消息。您可以通过JavaScript处理JSF视图上的传入消息,并随意使用它。

有关套接字的一个非常基本的示例,请参见此处:
http://www.primefaces.org/showcase/push/counter.xhtml

另见:
https://docs.oracle.com/javaee/6/tutorial/doc/gkhic.html

答案 1 :(得分:0)

最后,我得到了这两行代码的简单解决方案

<p:poll id="initNotificationAlert" interval="1" oncomplete="PF('initNotificationVar').stop()"  listener="#{notificationView.retrieveAlertNotification()}" widgetVar="initNotificationVar"></p:poll>
<p:poll id="notificationAlert" autoStart="false" interval="180"  listener="#{notificationView.retrieveAlertNotification()}" widgetVar="notificationVar"></p:poll>

第一个<p:pol>表示初始讯息,第二个表示<p:pol>表示休息。 如果在此解决方案中有任何更好的改进解决方案需要改进它。

谢谢

答案 2 :(得分:-2)

您可以像这样手动打开对话框:

<强>计划 有许多学生如QuartzWebApplication

<强>对话

public String showDialog(String dialogName){
        Map<String, Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("closable", true);
        options.put("draggable", true);
        options.put("resizable", true);
        options.put("contentHeight", 386);
        options.put("contentWidth", 1266);

        RequestContext.getCurrentInstance().openDialog(dialogName, options, null);

        return "";
    }

虽然dialogName是您尝试显示的.xhtml页面。您只需要一个调度程序来调用此方法,就会出现对话框。

相关问题