Skip to content

Configuring Atmosphere as a Spring Bean

Seamus McMorrow edited this page Feb 8, 2016 · 13 revisions

Configuring Atmosphere as a Spring bean

First, you need to define in your pom.xml the atmosphere-spring. This jar contains the Spring related classes needed to make it work. You can also cut and paste either the xml config or the java config below from step 2, and add the Servlet to the web.xml in your application.

##1. Add to your pom.xml

     <dependency>
			<groupId>org.atmosphere</groupId>
			<artifactId>atmosphere-spring</artifactId>
			<version>${atmosphere-version}</version>
		</dependency>

##2. Define a Spring context:

Java Config

    @Configuration 
     public class AtmosphereConfig {

	@Bean
	public AtmosphereFramework atmosphereFramework() throws ServletException, InstantiationException, IllegalAccessException {
		AtmosphereFramework atmosphereFramework = new AtmosphereFramework(false, false);
		// atmosphereFramework.setBroadcasterCacheClassName(UUIDBroadcasterCache.class.getName());
		atmosphereFramework.addAtmosphereHandler("/chat/*", atmosphereChatHandler(), interceptors());
		return atmosphereFramework;
	}

	@Bean
	public AtmosphereChatHandler atmosphereChatHandler() {
		return new AtmosphereChatHandler();
	}

	private List<AtmosphereInterceptor> interceptors() {
		List<AtmosphereInterceptor> atmosphereInterceptors = new ArrayList<>();
		// atmosphereInterceptors.add(new TrackMessageSizeInterceptor());
		return atmosphereInterceptors;
	}

	@Bean
	public BroadcasterFactory broadcasterFactory() throws ServletException, InstantiationException, IllegalAccessException {
		return atmosphereFramework().getAtmosphereConfig().getBroadcasterFactory();
	}

	@Bean
	public AtmosphereSpringContext atmosphereSpringContext() {
		AtmosphereSpringContext atmosphereSpringContext = new AtmosphereSpringContext();
		Map<String, String> map = new HashMap<>();
		map.put("org.atmosphere.cpr.broadcasterClass", org.atmosphere.cpr.DefaultBroadcaster.class.getName());
		map.put(AtmosphereInterceptor.class.getName(), TrackMessageSizeInterceptor.class.getName());
		map.put(AnnotationProcessor.class.getName(), VoidAnnotationProcessor.class.getName());
		map.put("org.atmosphere.cpr.broadcasterLifeCyclePolicy", ATMOSPHERE_RESOURCE_POLICY.IDLE_DESTROY.toString());
		atmosphereSpringContext.setConfig(map);
		return atmosphereSpringContext;
	}
    }

XML Config

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- Bean creation using appropriate constructor -->
    <bean id="framework" class="org.atmosphere.cpr.AtmosphereFramework">
        <constructor-arg index="0" value="false"/>
        <constructor-arg index="1" value="false"/>
    </bean>

    <!-- Configure Atmosphere framework -->
    <bean class="org.atmosphere.spring.bean.AtmosphereSpringContext">
                    <property name="config">
                        <map>
                            <entry key="org.atmosphere.cpr.broadcasterClass" value="org.atmosphere.cpr.DefaultBroadcaster"/>
                            <entry key="org.atmosphere.cpr.AtmosphereInterceptor" value="__LIST_OF_ATMOSPHEREINTERCEPTOR__"/>
                            <!-- If you don't want Atmosphere to scan for annotation -->
                            <entry key="org.atmosphere.cpr.AnnotationProcessor" value="org.atmosphere.util.VoidAnnotationProcessor"/>
                             <!-- Add you Atmosphere's Properties here. See @org.atmosphere.cpr.AtmosphereConfig -->
                            <entry key="org.atmosphere.cpr.broadcasterLifeCyclePolicy" value="IDLE_DESTROY"/>
                        </map>
                    </property>
                </bean>

    <!-- Add an Atomsphere handler (if necessary) -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref ="framework"/>
        <property name="targetMethod" value="addAtmosphereHandler"/>
        <property name="arguments">
            <list>
                <value>__PATH__</value>
                <bean class="__YOUR_ATMOSPHERE_HANDLER__"/>
            </list>
        </property>
    </bean>

    <!-- Add an Atomsphere listener (if necessary) -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref ="framework"/>
        <property name="targetMethod" value="addBroadcasterListener"/>
        <property name="arguments">
            <list>
                <bean class="__YOUR_ATMOSPHERE_HANDLER__"/>
            </list>
        </property>
    </bean>

</beans>

##3. Define servlet in web.xml with no configuration:

    web.xml...
    <servlet>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.spring.bean.AtmosphereSpringServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>AtmosphereServlet</servlet-name>
        <url-pattern>__PATH__</url-pattern>
    </servlet-mapping>

Step by Step Tutorials

Concepts & Architecture

15 Minutes Tutorial

Advanced Topics

API

Known WebServer Issues

References

External Documentations

githalytics.com alpha

Clone this wiki locally