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

Jsf 异步任务完成时打开p:对话框,jsf,asynchronous,jsf-2,primefaces,dialog,Jsf,Asynchronous,Jsf 2,Primefaces,Dialog,我需要一种技术,在这种技术中,我可以调用java方法从DB检索信息,并在对话框中显示消息,如果有任何下一条消息,则按下“Next”(下一步)按钮将显示该消息。该java方法在特定时间后自动调用,即用户首次登录时,然后在3分钟后调用。这是我希望在JSF&PrimeFaces中使用的 我使用了ScheduleExecutarService进行调度,我的运行方法如下所示 public void run() { try { System.out.println("beep");

我需要一种技术,在这种技术中,我可以调用java方法从
DB
检索信息,并在对话框中显示消息,如果有任何下一条消息,则按下“Next”(下一步)按钮将显示该消息。该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的对象

另一方面,我尝试了
标记,我读到关于该标记的信息是,停止它有问题


请指导我应该使用什么。

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

调度程序 有许多类似或的调度器

对话框

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 "";
    }
公共字符串显示对话框(字符串对话框名称){
Map options=newhashmap();
期权。看跌期权(“模态”,真);
期权。看跌期权(“可关闭”,真);
期权。看跌期权(“可拖动”,真);
选项。看跌期权(“可调整大小”,真);
期权。看跌期权(“内容高度”,386);
期权。看跌期权(“contentWidth”,1266);
RequestContext.getCurrentInstance().openDialog(dialogName,options,null);
返回“”;
}
dialogName是您试图显示的
.xhtml
页面。您只需要一个调度程序来调用这个方法,就会出现一个对话框

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

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

解决方案:

让调度程序触发一个CDI事件,并在客户机bean中使用CDI观察器捕捉该事件

现在您必须选择:要么让JSF视图轮询您的客户机bean以请求新消息,要么创建一个套接字,客户机bean可以通过该套接字向JSF视图通知新消息。您可以通过JavaScript在JSF视图上处理传入的消息,并对其进行任何处理

有关套接字的基本示例,请参见此处:

另请参见:

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

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

第一个
表示初始消息,第二个
表示剩余消息。 如果有更好的解决方案需要改进,请在此解决方案中进行改进


谢谢

谢谢。。。。。。。但是我应该从哪里调用这个方法??从run()方法??如果是,您确定我不会从
RequestContext.getCurrentInstance()
中获取null吗?因为我从run方法得到null