How to use the native async messaging system of RF4

03 Nov 2013 » {[{categoryPostCurrent}]}

This article show how to use the native async messaging system of Richfaces 4 under a servlet container (tomcat 7 in this example) and without the use of JMS nor CDI. This example come from a larger project using RF 4.3/JSF2 and spring 4.

I- Technicals requirements

  • Tomcat 7
  • Richfaces 4.3
  • Project mavenized


1) configuration of pom.xml

Long-polling without HTML 5 sockets or flash require the use of the atmosphere framework. In order to get this framework working in our mavenized project we must add the following dependency in the pom.xml:


2) Configuration of web.xml

Now, we must configure atmosphere and richfaces in order to disable JMS (JMS is enabled because it is the default support for messaging in RF). To achieve that we modify the deployment descriptor web.xml just as follow:

    <servlet-name>Push Servlet</servlet-name>
    <servlet-name>Push Servlet</servlet-name>

III-Pushing a notification from a JAVA class

We must add the 3 following lines of code where we want to fire a new notification:

   TopicKey topicKey = new TopicKey("topicNotificationsJobs");
    TopicsContext topicsContext = TopicsContext.lookup();
    topicsContext.publish("topicNotificationsJobs", null); 

IV-Configuration of the listener in the XHTML file

So as to retrieve each new notification fired from the below java class, we have to add the following tags in the xhtml file:

    <a4j:push address="topicNotificationsJobs"
    ondataavailable="alert('ACK recept')">
    <a4j:ajax event="dataavailable" execute="@this" render="@none"/>

If all goes well, as soon as a notification is pushed from a java class, a dialog box popup and display 'ACK recept' on your screen.

Note that the <a4j:push> must be wrapped into a <h:form> tag: if you miss that point the application does not trigger any error message but the messaging system will fail silently

Voir aussi...

comments powered by Disqus