Created
September 12, 2011 18:09
-
-
Save agentgt/1211957 to your computer and use it in GitHub Desktop.
Tomcat Spring Atomsphere Servlet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <groupId>org.atmosphere</groupId> | |
| <artifactId>atmosphere-jersey</artifactId> | |
| <version>0.7.2</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey</groupId> | |
| <artifactId>jersey-server</artifactId> | |
| <version>${jersey.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey</groupId> | |
| <artifactId>jersey-json</artifactId> | |
| <version>${jersey.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.sun.jersey.contribs</groupId> | |
| <artifactId>jersey-spring</artifactId> | |
| <version>${jersey.version}</version> | |
| <exclusions> | |
| <exclusion> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-core</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-beans</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-web</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-context</artifactId> | |
| </exclusion> | |
| <exclusion> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring</artifactId> | |
| </exclusion> | |
| </exclusions> | |
| </dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.snaphop.spring; | |
| import java.lang.reflect.InvocationTargetException; | |
| import javax.servlet.ServletConfig; | |
| import org.atmosphere.cpr.AtmosphereServlet; | |
| import org.atmosphere.cpr.Broadcaster; | |
| import org.atmosphere.cpr.BroadcasterFactory; | |
| import org.atmosphere.handler.ReflectorServletProcessor; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| @SuppressWarnings("serial") | |
| public class SpringAtmosphereServlet extends AtmosphereServlet { | |
| private static final Logger logger = LoggerFactory.getLogger(AtmosphereServlet.class); | |
| public final static String JERSEY_CONTAINER = "com.sun.jersey.spi.spring.container.servlet.SpringServlet"; | |
| /** | |
| * Auto-detect Jersey when no atmosphere.xml file are specified. | |
| * | |
| * @param sc {@link ServletConfig} | |
| * @return true if Jersey classes are detected | |
| * @throws ClassNotFoundException | |
| */ | |
| protected boolean detectSupportedFramework(ServletConfig sc) throws ClassNotFoundException, IllegalAccessException, | |
| InstantiationException, NoSuchMethodException, InvocationTargetException { | |
| // If Primefaces is detected, never start Jersey. | |
| // TODO: Remove this hack once properly implemented in PrimeFaces | |
| ClassLoader cl = Thread.currentThread().getContextClassLoader(); | |
| try { | |
| cl.loadClass(PRIMEFACES_SERVLET); | |
| return false; | |
| } catch (Throwable ignored) { | |
| } | |
| try { | |
| cl.loadClass(JERSEY_CONTAINER); | |
| useStreamForFlushingComments = true; | |
| } catch (Throwable t) { | |
| return false; | |
| } | |
| logger.warn("Missing META-INF/atmosphere.xml but found the Jersey runtime. Starting Jersey"); | |
| ReflectorServletProcessor rsp = new ReflectorServletProcessor(); | |
| if (!isBroadcasterSpecified) broadcasterClassName = lookupDefaultBroadcasterType(); | |
| rsp.setServletClassName(JERSEY_CONTAINER); | |
| sessionSupport(false); | |
| addInitParameter(DISABLE_ONSTATE_EVENT, "true"); | |
| String mapping = sc.getInitParameter(PROPERTY_SERVLET_MAPPING); | |
| if (mapping == null) { | |
| mapping = "/*"; | |
| } | |
| @SuppressWarnings("unchecked") | |
| Class<? extends Broadcaster> bc = (Class<? extends Broadcaster>) cl.loadClass(broadcasterClassName); | |
| Broadcaster b = BroadcasterFactory.getDefault().get(bc, mapping); | |
| addAtmosphereHandler(mapping, rsp, b); | |
| return true; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" | |
| xmlns:j2ee="http://java.sun.com/xml/ns/javaee" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2.5.xsd"> | |
| <description>Atmosphere JQuery PubSub</description> | |
| <display-name>Atmosphere JQuery PubSub</display-name> | |
| <!-- Spring listener and dispatcher servlet here --> | |
| <servlet> | |
| <description>AtmosphereServlet</description> | |
| <servlet-name>AtmosphereServlet</servlet-name> | |
| <servlet-class>com.snaphop.spring.SpringAtmosphereServlet</servlet-class> | |
| <init-param> | |
| <param-name>grizzly.application.path</param-name> | |
| <param-value>/pubsub/chat</param-value> | |
| </init-param> | |
| <init-param> | |
| <param-name>org.atmosphere.useWebSocket</param-name> | |
| <param-value>true</param-value> | |
| </init-param> | |
| <init-param> | |
| <param-name>org.atmosphere.useNative</param-name> | |
| <param-value>true</param-value> | |
| </init-param> | |
| <init-param> | |
| <param-name>org.atmosphere.cpr.broadcastFilterClasses</param-name> | |
| <param-value>org.atmosphere.client.FormParamFilter,org.atmosphere.client.JavascriptClientFilter | |
| </param-value> | |
| </init-param> | |
| <init-param> | |
| <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> | |
| <param-value>true</param-value> | |
| </init-param> | |
| <load-on-startup>0</load-on-startup> | |
| </servlet> | |
| <servlet-mapping> | |
| <servlet-name>AtmosphereServlet</servlet-name> | |
| <url-pattern>/*</url-pattern> | |
| </servlet-mapping> | |
| </web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment