私の Java EE 6 (Richfaces 4.1) ベースのアプリケーションでは、これを達成しようとしています: ユーザーはブラウザー ウィンドウを開き、特定の REST サービスが呼び出されるまで長いポーリングを行います (残りのサービスは、Facelets 名とリストを受け取ります)パラメーター)。Webservice 呼び出しの結果として、REST 呼び出しで指定されたパラメーターを使用して、ブラウザーで JSF ページがレンダリングされます。
懸念の証拠として、私は最初に AsyncContext と CDI Events を試してみました。ブラウザーで 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);
}
}
私はリッチフェイスを使用しているので、a4j:push でこれを行うほうがよいと考えました: http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=push&sample=pushCdi
私の質問は次のとおりです。
1)最良のアプローチは何だと思いますか(サービスが呼び出されるとすぐにAsyncContextが終了し、一種の「無限の」ロングポーリングが必要になるため、Richfacesと言えます)?
2)JSFページをプログラムで呼び出す方法を示す例を教えてください(これは私のコードで見ることができるTODOです)
どうもありがとう!