سؤال

I am trying to implement prime push counter in my project. I am using PrimeFaces3.5, Jboss7.0 and Eclipse Indigo version.

I have added jars related to prime push:

  1. atmosphere-annotations-1.0.1.jar
  2. atmosphere-compat-jbossweb-1.0.1.jar
  3. atmosphere-compat-tomcat-1.0.1.jar
  4. atmosphere-compat-tomcat7-1.0.1.jar
  5. atmosphere-runtime-1.0.1.jar
  6. primefaces-3.5.jar and jsf and slf4j jars

My xhtml code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
    <h:form id="form">
        <h:outputText id="out" value="#{pushBean.count}" />
        <p:commandButton value="Click" actionListener="#{pushBean.increment}" />
    </h:form>
    <p:socket onMessage="handleMessage" channel="/counter" />
    <script type="text/javascript">
         function handleMessage(data) {
           $('.display').html(data);
         }
    </script>
</h:body>
</html>

My managed bean:

@ManagedBean(name = "pushBean")
@ApplicationScoped
public class PushBean {

    public PushBean() {
    }

    private int count;

    public int getCount() {
        return this.count;
    }

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

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

}

When I click the button in UI, the count is incremented on the server, but it is not reflected in UI automatically, because it is not updated. But when I refresh the page, the count is incremented as expected.


Exception I am getting is:

13:00:02,298 ERROR [stderr] (http--0.0.0.0-8080-5) [http--0.0.0.0-8080-5] ERROR org.atmosphere.cpr.AtmosphereFramework - AtmosphereFramework exception

13:00:02,298 ERROR [stderr] (http--0.0.0.0-8080-5) java.lang.IllegalStateException: The servlet or filters that are being used by this request do not support async operation
هل كانت مفيدة؟

المحلول

This exception happens when your web app is run in Servlet 3 compliant containers. The correct web.xml must have the async-supported element set to true, for example:

    <servlet> 
        <servlet-name>Push Servlet</servlet-name> 
        <servlet-class>org.primefaces.push.PushServlet</servlet-class> 
        <async-supported>true</async-supported> 
    </servlet>

Reference: Installing Atmosphere

نصائح أخرى

I'm not very sure about push framework but i think the problem is with your javascript. you are trying to refresh ".display" control but there is nothing like .display.

Try '.out' instead of .display

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top