Home » Java » Enterprise Java » PrimeFaces Push with Atmosphere on GlassFish

About Markus Eisele

Markus Eisele
Markus is a Developer Advocate at Red Hat and focuses on JBoss Middleware. He is working with Java EE servers from different vendors since more than 14 years and talks about his favorite topics around Java EE on conferences all over the world. He has been a principle consultant and worked with different customers on all kinds of Java EE related applications and solutions. Beside that he has always been a prolific blogger, writer and tech editor for different Java EE related books. He is an active member of the German DOAG e.V. and it's representative on the iJUG e.V. As a Java Champion and former ACE Director he is well known in the community. Follow him on Twitter @myfear.

PrimeFaces Push with Atmosphere on GlassFish

PrimeFaces 3.4 came out three days ago. Beside the usual awesomeness of new and updated components it also includes the new PrimeFaces Push framework. Based on Atmosphere this is providing easy push mechanisms to your applications. Here is how to configure and run it on latest GlassFish


As usual you should have some Java, Maven and GlassFish installed. If you need it out of one hand give NetBeans 7.2 a try. It is the latest and greatest and comes with all the things you need for this example. Install the parts or the whole to a location of your choice and start with creating a new GlassFish domain:

asadmin create-domain pf_push

accept the default values and start your domain

asadmin start-domain pf_push

Now you have to enable Comet support for your domain. Do this either by using the http://<host>:4848/ admin ui or with the following command:

asadmin set server-config.network-config.protocols.protocol.http-1.http.comet-support-enabled='true'

That is all you have to do to configure your domain.

The Maven Project Setup

Now switch to your IDE and create a new Maven based Java EE 6 project. Add the primefaces repository to the <repositories> section and add the primefaces dependency to your project <dependencies> section or your project’s pom.xml:

            <name>Repository for library PrimeFaces 3.2</name>


Additionally we need the latest Atmosphere dependency (Contrats to JeanFrancois Arcand for this release)


It is using Log4j and if you need to have some more output it is a good idea to also include the corresponding configuration or bridge it to JUL with slf4j. To do the later, simply include the following to your pom.xml:


There is only one thing left to do. The PrimePush component needs to have its servlet channel registered. So, open your web.xml and add the following to it:

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

That was it! On to the code!

The Code

I’m going to use the example referred to in the PrimeFaces users guide. A very simple example which has a global counter which could be incremented.

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;

 * Counter is a global counter where each button click increments the count
 * value and new value is pushed to all subscribers.
 * @author eiselem
public class GlobalCounterBean implements Serializable {

    private int count;

    public int getCount() {
        return count;

    public void setCount(int count) {
        this.count = count;

    public synchronized void increment() {
        PushContext pushContext = PushContextFactory.getDefault().getPushContext(;
        pushContext.push('/counter', String.valueOf(count));

The PushContext contains the whole magic here. It is mainly used to publish and schedule messages and manage listeners and more. It is called from your facelet. This looks simple and familiar:

<h:form id='counter'>
<h:outputText id='out' value='#{globalCounterBean.count}' styleClass='display' />
<p:commandButton value='Click' actionListener='#{globalCounterBean.increment}' />

This basically does nothing, except incrementing the counter. So you have to add some more magic for connecting to the push channel. Add the following below the form:

<p:socket channel='/counter' >
<p:ajax event='message' update='counter:out' />

<p:socket /> is the PrimeFaces component that handles the connection between the server and the browser. It does it by defining a communication channel and a callback to handle the broadcasts. The contained <p:ajax /> component listens to the message event and updates the counter field in the form. This however requires and additional server round-trip. You could also shortcut this by using a little java-script and binding the onMessage attribute to it to update the output field:

<script type='text/javascript'>
function handleMessage(data) {
<p:socket onMessage='handleMessage' channel='/counter' />

That is all for now. Congratulations to your first PrimeFaces Push example.

Happy coding and don’t forget to share!

Reference: PrimeFaces Push with Atmosphere on GlassFish from our JCG partner Markus Eisele at the Enterprise Software Development with Java blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.