0

对于我的基于 Java EE 6 (Richfaces 4.1) 的应用程序,我试图实现这一点:用户打开一个浏览器窗口,该窗口进行长时间轮询,直到调用给定的 REST 服务(其余服务将收到一个 facelets 名称以及一个列表参数)。作为 Web 服务调用的结果,JSF 页面使用 REST 调用中指定的参数在浏览器中呈现。

作为关注的证明,我首先尝试了 AsyncContext 和 CDI 事件,我可以在浏览器中打印 REST 参数:

@WebServlet(name = "Notifier", urlPatterns = {"/Notifier"},asyncSupported = true)
public class Notifier extends HttpServlet {

    @Inject
    Event<NotificationReq> events;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            AsyncContext startAsync = request.startAsync();
            startAsync.setTimeout(0);
            events.fire(new NotificationReq(startAsync));
    }
}
-------------------------
    @ConcurrencyManagement(ConcurrencyManagementType.BEAN)
    @Singleton
    @Path("notify")
    public class RESTNotifier {

        @PostConstruct
        public void onInit() {
            this.browsers = new CopyOnWriteArrayList<NotificationReq>();
        }
        private CopyOnWriteArrayList<NotificationReq> browsers;

        public void onNewNotificationRequest(@Observes NotificationReq nr) {
            this.browsers.add(nr);
        }

        @GET
        @Path("{message}")
        public void specific(@PathParam("message") String message) {
            for (NotificationReq notificationRequest : browsers) {
                notificationRequest.sendMessage(message);
                this.browsers.remove(notificationRequest);
            }
        }
    }
    -----------------------------------------------------
        public class NotificationReq {

            private AsyncContext asyncContext;

            public NotificationReq(AsyncContext asyncContext) {
                this.asyncContext = asyncContext;
            }

            public void sendMessage(String message){
                try {
                    PrintWriter out = this.asyncContext.getResponse().getWriter();
                    out.println(message);//TODO: Invoke and Render JSF instead of printing message!
                    this.asyncContext.complete();
                } catch (IOException ex) {
                    Logger.getLogger(NotificationReq.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

当我使用richfaces时,我认为我可以更好地使用a4j:push:http ://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&sample=pushCdi

我的问题是:

1)您认为最好的方法是什么(我会说 Richfaces,因为 AsyncContext 在服务被调用后立即终止,我想要一种“无限”长轮询)?

2)您能否指出任何显示如何以编程方式调用 JSF 页面的示例(这是您可以在我的代码中看到的 TODO)

非常感谢!

4

1 回答 1

0

要让推送更新页面的某些部分,请执行以下操作:

<a4j:push ...>
  <a4j:ajax event="dataavailable" render="whatYouWantToRerender" />
</a4j:push>
于 2013-02-19T14:51:23.790 回答