DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. PrimeFaces Push with Atmosphere on GlassFish 3.1.2.2

PrimeFaces Push with Atmosphere on GlassFish 3.1.2.2

Markus Eisele user avatar by
Markus Eisele
·
Sep. 11, 12 · Interview
Like (0)
Save
Tweet
Share
11.71K Views

Join the DZone community and get the full member experience.

Join For Free

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 3.1.2.2.

Preparations
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:
 <repository>
            <url>http://repository.primefaces.org/</url>
            <id>primefaces</id>
            <layout>default</layout>
            <name>Repository for library PrimeFaces 3.2</name>
        </repository>
 
 <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.4</version>
 </dependency>
Additionally we need the latest Atmosphere dependency (Contrats to JeanFrancois Arcand for this release)
<dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>1.0.0</version>
</dependency>
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:
 <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.6.6</version>
        </dependency>
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>
        <servlet-name>Push Servlet</servlet-name>
        <servlet-class>org.primefaces.push.PushServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>Push Servlet</servlet-name>
        <url-pattern>/primepush/*</url-pattern>
</servlet-mapping>
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
 */
@ManagedBean
@SessionScoped
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() {
        count++;
        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}" />
</h:form>
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>
<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) {
$('.display').html(data);
}
</script>
<p:socket onMessage="handleMessage" channel="/counter" />
That is all for now. Congratulations to your first PrimeFaces Push example. Have fun playing around with it! 
PrimeFaces push GlassFish Atmosphere (architecture and spatial design)

Published at DZone with permission of Markus Eisele, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • How and Why You Should Start Automating DevOps
  • PHP vs React

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: