2

I am trying to get further into grails3 and I am unsure about plugin descriptor and doWithWebDescriptor:

src/main/groovy/grails/plugin/plugin/PluginGrailsPlugin.groovy

    def doWithWebDescriptor = { xml ->
            def listenerNode = xml.'listener'
            listenerNode[listenerNode.size() - 1] + {
                    listener {
                            'listener-class'(someClass.name)
                    }
            }
    }

I tried grails install-templates under grails 3 and no web.xml was generated... I also had a look at the default generated plugin descriptor which did not appear to have doWithWebDescriptor...

Was wondering if this has changed - is it no longer producing a web.xml or if it is what should I be doing to register a listener under grails 3 .

V H
  • 8,382
  • 2
  • 28
  • 48

2 Answers2

0

I have managed to get default tomcat websocket listener to work via a spring boot grails app:

It is documented here:

https://github.com/vahidhedayati/testwebsocket-grails3

I have decided to update this post and include all my findings so far on this matter.

More specifically the application.groovy inside your application grails-app/init folder:

This bean initiates default tomcat websocket listener:

@Bean
    public ServletListenerRegistrationBean<AnotherWebSocketHandler> httpSessionEventPublisher() {
        return new ServletListenerRegistrationBean<AnotherWebSocketHandler>(new AnotherWebSocketHandler());
    }

Whilst messing around to reuse in plugin, the findings are:

The above project is a basic grails application which does 2 things, a basic spring socket as well java 1.X Websocket:

Here is how to use Default websocket in a grails 3 plugin

In you plugin descriptor you have something like this:

Closure doWithSpring() {
        {->
            wsChatConfig DefaultWsChatConfig
        }
    }

In this plugin I have left both methods of initiating the listener:

@Bean
    public ServletContextInitializer myInitializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.addListener(WsCamEndpoint)
                servletContext.addListener(WsChatFileEndpoint)

            }
        }
    }

    // Alternative way
    @Bean
    public ServletListenerRegistrationBean<WsChatEndpoint>  httpSessionEventPublisher() {
        return new ServletListenerRegistrationBean<WsChatEndpoint>(new WsChatEndpoint())

    }

The top method came in very handy since you can only initialise 1 ServletListenerRegistrationBean and I had to resort to the top method to enable other listeners... I could have just used the top primary for all the calls. Left in for future reference..

With this in place, spring boot now emulates the same as web.xml would when registering a listener. The actual groovy classes that load the websockets from there are as they were i.e. using default websocket calls such as onOpen onMessage etc..

V H
  • 8,382
  • 2
  • 28
  • 48
0

From Grails 3 there's a new way of adding runtime configuration using spring registration beans in the Plugin method doWithSpring. doWithWebDescriptor is not used anymore.

This should work for the Servlet Listeners:

Closure doWithSpring() {{ ->
    MyListener(ServletListenerRegistrationBean) {
        listener = bean(someClass)
        order = Ordered.HIGHEST_PRECEDENCE
    }
}}

Disclaimer: I didn't test this code.

Refer to the Gails documentation.

Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29