Skip to content

Instantly share code, notes, and snippets.

@smithh032772
Created July 11, 2013 16:31
Show Gist options
  • Save smithh032772/5976987 to your computer and use it in GitHub Desktop.
Save smithh032772/5976987 to your computer and use it in GitHub Desktop.
2013-07-11 Glassfish 4.0 and PrimeFaces Push (PrimeFaces 4.0 snapshot 2013-07-10)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.primepush.facesmessage;
import java.io.Serializable;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
@Named
@RequestScoped
public class GrowlBean implements Serializable {
private String summary, detail;
public GrowlBean() {
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public void send() {
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/notifications", new FacesMessage(summary, detail));
}
}
<?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:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>PrimePush - FacesMessage</title>
</h:head>
<h:body>
<p:growl widgetVar="growl" showDetail="true" />
<h:form>
<p:panel header="Growl">
<h:panelGrid columns="2">
<p:outputLabel for="summary" value="Summary: " />
<p:inputText id="summary" value="#{growlBean.summary}" required="true" />
<p:outputLabel for="detail" value="Detail: " />
<p:inputText id="detail" value="#{growlBean.detail}" required="true" />
</h:panelGrid>
<p:commandButton value="Send" actionListener="#{growlBean.send}" />
</p:panel>
</h:form>
<p:socket onMessage="handleMessage" channel="/notifications" />
<script type="text/javascript">
function handleMessage(facesMessage) {
facesMessage.severity = 'info';
PF('growl').show([facesMessage]);
}
</script>
</h:body>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.primepush.facesmessage;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.cpr.BroadcasterLifeCyclePolicyListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.atmosphere.cpr.BroadcasterLifeCyclePolicy.EMPTY_DESTROY;
import org.primefaces.push.PushRule;
/**
* The Default {@link PushRule} that creates channel based on the {@link org.atmosphere.cpr.AtmosphereRequest#getPathInfo()}.
*/
public class PrimePushRule implements PushRule {
/**
* Creates channel (named {@link Broadcaster} in Atmosphere) based on the {@link org.atmosphere.cpr.AtmosphereRequest#getPathInfo()}
* @param resource An {@link AtmosphereResource}
* @return
*/
public boolean apply(AtmosphereResource resource) {
/*
* Changes for Glassfish 3.1.2.2+ as per Aleksandar Nikolov
* http://forum.primefaces.org/viewtopic.php?f=10&t=25577&start=30#p85058
*
String pathInfo = resource.getRequest().getPathInfo();
if (pathInfo == null) {
resource.setBroadcaster(BroadcasterFactory.getDefault().lookup("/*"));
return true;
}
*/
AtmosphereRequest req = resource.getRequest();
String pathInfo = req.getPathInfo();
if(pathInfo==null){
String servletPath = req.getServletPath();
String contextPath = req.getContextPath();
pathInfo = req.getRequestURI()
.replaceFirst(contextPath, "")
.replaceFirst(servletPath, "");
}
if (pathInfo == null || pathInfo.isEmpty()) {
resource.setBroadcaster(BroadcasterFactory.getDefault().lookup("/*"));
return true;
}
// the rest of the code is the same as the DefaultPushRule in PrimeFaces
final Broadcaster b = BroadcasterFactory.getDefault().lookup(pathInfo, true);
b.setBroadcasterLifeCyclePolicy(EMPTY_DESTROY);
b.addBroadcasterLifeCyclePolicyListener(new BroadcasterLifeCyclePolicyListener() {
private final Logger logger = LoggerFactory.getLogger(BroadcasterLifeCyclePolicyListener.class);
public void onEmpty() {
logger.trace("onEmpty {}", b.getID());
}
public void onIdle() {
logger.trace("onIdle {}", b.getID());
}
public void onDestroy() {
logger.trace("onDestroy {}", b.getID());
}
});
resource.setBroadcaster(b);
return true;
}
}
INFO: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080
INFO: Grizzly Framework 2.3.1 started in: 3ms - bound to [/0.0.0.0:8,080]
INFO: Grizzly Framework 2.3.1 started in: 2ms - bound to [/0.0.0.0:7,676]
INFO: Registering WebSocket filter for url pattern /*
INFO: Initializing Mojarra 2.2.1 ( 20130709-1453 https://svn.java.net/svn/mojarra~svn/tags/2.2.1@12065) for context '/primePushFacesMessage'
INFO: Running on PrimeFaces 4.0-SNAPSHOT
INFO: Installing BroadcastFilter class(es) org.atmosphere.client.TrackMessageSizeFilter
INFO: Atmosphere is using org.atmosphere.cpr.DefaultAnnotationProcessor for processing annotation
INFO: Initializing Mojarra 2.2.1 ( 20130709-1453 https://svn.java.net/svn/mojarra~svn/tags/2.2.1@12065) for context ''
INFO: Auto detecting atmosphere handlers /WEB-INF/classes/
INFO: Auto detecting WebSocketHandler in /WEB-INF/classes/
INFO: Installed WebSocketProtocol org.atmosphere.websocket.protocol.SimpleHttpProtocol
INFO: Loading application [__admingui] at [/]
INFO: Loading application __admingui done in 3,737 ms
INFO: Atmosphere is using async support: org.atmosphere.container.JSR356AsyncSupport running under container: GlassFish Server Open Source Edition 4.0 using javax.servlet/3.0
INFO: Installed AtmosphereInterceptor Track Message Size Interceptor using |.
INFO: Installing Default AtmosphereInterceptor
INFO: org.atmosphere.interceptor.OnDisconnectInterceptor : Browser disconnection detection
INFO: org.atmosphere.interceptor.JavaScriptProtocol : Atmosphere JavaScript Protocol
INFO: org.atmosphere.interceptor.JSONPAtmosphereInterceptor : JSONP Interceptor Support
INFO: org.atmosphere.interceptor.SSEAtmosphereInterceptor : SSE Interceptor Support
INFO: org.atmosphere.interceptor.AndroidAtmosphereInterceptor : Android Interceptor Support
INFO: org.atmosphere.interceptor.PaddingAtmosphereInterceptor : Browser Padding Interceptor Support
INFO: org.atmosphere.interceptor.DefaultHeadersInterceptor : Default Response's Headers Interceptor
INFO: Set org.atmosphere.cpr.AtmosphereInterceptor.disableDefaults in your xml to disable them.
INFO: Using BroadcasterCache: org.atmosphere.cache.HeaderBroadcasterCache
INFO: Using BroadcastFilter: org.atmosphere.client.TrackMessageSizeFilter
INFO: Shared ExecutorService supported: true
INFO: HttpSession supported: false
INFO: Using BroadcasterFactory: org.atmosphere.cpr.DefaultBroadcasterFactory
INFO: Using WebSocketProcessor: org.atmosphere.websocket.DefaultWebSocketProcessor
INFO: Using Broadcaster: org.atmosphere.cpr.DefaultBroadcaster
INFO: Atmosphere Framework 2.0.0.RC1 started.
INFO:
For Atmosphere Framework Commercial Support, visit
http://www.async-io.org/ or send an email to support@async-io.org
INFO: Installed AtmosphereInterceptor Atmosphere LifeCycle.
INFO: PushRule com.primepush.facesmessage.PrimePushRule loaded
INFO: Installed AtmosphereHandler org.primefaces.push.PrimeAtmosphereHandler mapped to context-path: /*
INFO: Installed the following AtmosphereInterceptor mapped to AtmosphereHandler org.primefaces.push.PrimeAtmosphereHandler
INFO: Loading application [primePushFacesMessage] at [/primePushFacesMessage]
INFO: primePushFacesMessage was successfully deployed in 6,383 milliseconds.
WARNING: No Broadcaster match /notifications
INFO: Server shutdown initiated
When following is added to web.xml:
<init-param>
<param-name>org.atmosphere.cpr.sessionSupport</param-name>
<param-value>true</param-value>
</init-param>
the following is in server.log:
INFO: Running on PrimeFaces 4.0-SNAPSHOT
SEVERE: SessionSupport error
java.lang.IllegalStateException: Unable to call method addListener on servlet context /primePushFacesMessage, because this servlet context has already been initialized
at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:3299)
at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:3283)
at org.apache.catalina.core.StandardContext.addListener(StandardContext.java:3365)
at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:937)
at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:820)
at org.atmosphere.cpr.AtmosphereFramework.doInitParams(AtmosphereFramework.java:915)
at org.atmosphere.cpr.AtmosphereFramework.init(AtmosphereFramework.java:565)
at org.atmosphere.cpr.AtmosphereFramework.init(AtmosphereFramework.java:512)
at org.atmosphere.cpr.AtmosphereServlet.init(AtmosphereServlet.java:74)
at org.primefaces.push.PushServlet.init(PushServlet.java:46)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5670)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:5912)
at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:497)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)
INFO: Installing BroadcastFilter class(es) org.atmosphere.client.TrackMessageSizeFilter
INFO: Atmosphere is using org.atmosphere.cpr.DefaultAnnotationProcessor for processing annotation
INFO: Auto detecting atmosphere handlers /WEB-INF/classes/
INFO: Auto detecting WebSocketHandler in /WEB-INF/classes/
INFO: Installed WebSocketProtocol org.atmosphere.websocket.protocol.SimpleHttpProtocol
INFO: Atmosphere is using async support: org.atmosphere.container.JSR356AsyncSupport running under container: GlassFish Server Open Source Edition 4.0 using javax.servlet/3.0
INFO: Installing Default AtmosphereInterceptor
INFO: org.atmosphere.interceptor.OnDisconnectInterceptor : Browser disconnection detection
INFO: org.atmosphere.interceptor.JavaScriptProtocol : Atmosphere JavaScript Protocol
INFO: org.atmosphere.interceptor.JSONPAtmosphereInterceptor : JSONP Interceptor Support
INFO: org.atmosphere.interceptor.SSEAtmosphereInterceptor : SSE Interceptor Support
INFO: org.atmosphere.interceptor.AndroidAtmosphereInterceptor : Android Interceptor Support
INFO: org.atmosphere.interceptor.PaddingAtmosphereInterceptor : Browser Padding Interceptor Support
INFO: org.atmosphere.interceptor.DefaultHeadersInterceptor : Default Response's Headers Interceptor
INFO: Set org.atmosphere.cpr.AtmosphereInterceptor.disableDefaults in your xml to disable them.
INFO: Using BroadcasterCache: org.atmosphere.cache.HeaderBroadcasterCache
INFO: Using BroadcastFilter: org.atmosphere.client.TrackMessageSizeFilter
INFO: Shared ExecutorService supported: true
INFO: HttpSession supported: true
INFO: Using BroadcasterFactory: org.atmosphere.cpr.DefaultBroadcasterFactory
INFO: Using WebSocketProcessor: org.atmosphere.websocket.DefaultWebSocketProcessor
INFO: Using Broadcaster: org.atmosphere.cpr.DefaultBroadcaster
INFO: Atmosphere Framework 2.0.0.RC1 started.
INFO:
For Atmosphere Framework Commercial Support, visit
http://www.async-io.org/ or send an email to support@async-io.org
INFO: Installed AtmosphereInterceptor Atmosphere LifeCycle.
INFO: Initializing Mojarra 2.2.1 ( 20130709-1453 https://svn.java.net/svn/mojarra~svn/tags/2.2.1@12065) for context ''
INFO: Installed AtmosphereHandler org.primefaces.push.PrimeAtmosphereHandler mapped to context-path: /*
INFO: Installed the following AtmosphereInterceptor mapped to AtmosphereHandler org.primefaces.push.PrimeAtmosphereHandler
INFO: Loading application [primePushFacesMessage] at [/primePushFacesMessage]
INFO: primePushFacesMessage was successfully deployed in 7,360 milliseconds.
INFO: Loading application [__admingui] at [/]
INFO: Loading application __admingui done in 4,614 ms
WARNING: No Broadcaster match /notifications
INFO: Server shutdown initiated
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Push Servlet</servlet-name>
<servlet-class>org.primefaces.push.PushServlet</servlet-class>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
<param-value>org.atmosphere.cache.HeaderBroadcasterCache</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcasterClass</param-name>
<param-value>org.atmosphere.cpr.DefaultBroadcaster</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.cpr.broadcastFilterClasses</param-name>
<param-value>org.atmosphere.client.TrackMessageSizeFilter</param-value>
</init-param>
<!-- Tell Atmosphere to use TrackMessageSizeInterceptor - in order to avoid glued messages -->
<init-param>
<param-name>org.atmosphere.cpr.AtmosphereInterceptor</param-name>
<param-value>org.atmosphere.client.TrackMessageSizeInterceptor</param-value>
</init-param>
<!-- This one I have no idea what it does. Someone in the forums suggested it -->
<init-param>
<param-name>org.atmosphere.disableOnStateEvent</param-name>
<param-value>true</param-value>
</init-param>
<!-- hera I tell PrimeFaces to use my PushRule -->
<init-param>
<param-name>org.primefaces.push.rules</param-name>
<param-value>com.primepush.facesmessage.PrimePushRule</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Push Servlet</servlet-name>
<url-pattern>/primepush/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment