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.
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
:
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
</dependency>
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:
...
<context-param>
<param-name>org.atmosphere.useBlocking</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.push.handlerMapping</param-name>
<param-value>/__richfaces_push</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.push.jms.disable</param-name>
<param-value>true</param-value>
</context-param>
...
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.richfaces.webapp.PushServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/__richfaces_push</url-pattern>
</servlet-mapping>
....
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);
So as to retrieve each new notification fired from the below java class, we have to add the following tags in the xhtml file:
<h:form>
....
<a4j:push address="topicNotificationsJobs"
ondataavailable="alert('ACK recept')">
<a4j:ajax event="dataavailable" execute="@this" render="@none"/>
</a4j:push>
...
</h:form>
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