Document configuration for spring boot application #590

Open
sbordet opened this Issue Mar 27, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@sbordet
Member

sbordet commented Mar 27, 2015

Describe how to configure cometd with spring boot application.

@configuration
@EnableAutoConfiguration
public class WebsocketConfiguration {

private static Logger logger = LoggerFactory.getLogger(WebsocketConfiguration.class);

@Bean
public JettyEmbeddedServletContainerFactory servletContainerFactory() {
    JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();

    factory.addServerCustomizers(new JettyServerCustomizer(){
        @Override
        public void customize(Server server) {
            try {
                WebSocketServerContainerInitializer.configureContext((WebAppContext)server.getHandler());
            } catch (ServletException e) {
                logger.error(e.getMessage());
            }
        }           
    });     
    return factory;
}

@Bean
public ServletRegistrationBean servletRegistrationBean(){
    CometDServlet cometdServlet = new CometDServlet();
    return new ServletRegistrationBean(cometdServlet,"/cometd/*");
}

@Bean
public FilterRegistrationBean filterRegistration() {
    FilterRegistrationBean filter = new FilterRegistrationBean();
    CrossOriginFilter crossOriginFilter = new CrossOriginFilter();
    filter.setFilter(crossOriginFilter);
    filter.setAsyncSupported(true);
    filter.setName("cross-origin");
    filter.addUrlPatterns("/cometd/*","/chat/template/*");
    return filter;
}

@Bean
public ServletContextInitializer initializer() {
    return new ServletContextInitializer() {
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer(servletContext));
        }
    };
}

@Bean
public BayeuxServer bayeuxServer(ServletContext servletContext)
{
    BayeuxServerImpl bean = new BayeuxServerImpl();
    bean.addExtension(new org.cometd.server.ext.AcknowledgedMessagesExtension());
    bean.setOption(ServletContext.class.getName(), servletContext);
    bean.setOption("ws.cometdURLMapping", "/cometd/*"); 
    return bean;
}

}

@sbordet

This comment has been minimized.

Show comment
Hide comment
@sbordet

sbordet Mar 27, 2015

Member

Originally reported by lubos08 on 2015-03-27T04:21:07Z

Member

sbordet commented Mar 27, 2015

Originally reported by lubos08 on 2015-03-27T04:21:07Z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment