對於我的基於Java EE 6(Richfaces 4.1)的應用程序,我試圖做到這一點:用戶打開一個瀏覽器窗口,該窗口進行長輪詢,直到調用給定的REST服務(其餘服務將接收一個facelets名字和參數列表)。作爲Webservice調用的結果,JSF頁面使用REST調用中指定的參數在瀏覽器中呈現。服務器推送JSF頁面到客戶端
至於大家關心的證明,我先用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:推:http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&sample=pushCdi
我的問題是:
1)你認爲什麼是最好的方法(我會說RichFaces的,因爲AsyncContext只要調用服務已結束,我想有一種「無限」長投票)?
2)能否請您指出我的任何例子說明如何編程調用JSF頁面(這是你可以在我的代碼中看到)
感謝很多的TODO!
如果服務器已經有事件使用推,這是網上很多相關博客,閱讀這一個:HTTP://裏克-ansikter.blogspot.com/2012/02/configuring-richfaces-push-with-42.html – 2013-02-19 13:44:58
嗨,感謝您的回答!我認爲集成a4j:push是沒有問題的,我在這裏問的是如何讓服務器推送一個完整的jsf而不是(在你發佈的例子中)一個「新的Date()」...... – Federico 2013-02-19 13:55:44