2

我正在尝试进一步了解 grails3,但我不确定插件描述符和 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)
                    }
            }
    }

我在 grails 3 下尝试了 grails install-templates 并且没有生成 web.xml ...我还查看了默认生成的插件描述符,它似乎没有 doWithWebDescriptor ...

想知道这是否已经改变 - 它不再产生 web.xml 还是我应该做些什么来在 grails 3 下注册一个监听器。

4

2 回答 2

0

我已经设法让默认的tomcat websocket监听器通过spring boot grails应用程序工作:

它记录在这里:

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

我决定更新这篇文章,并包括到目前为止我在这个问题上的所有发现。

更具体地说,应用程序 grails-app/init 文件夹中的 application.groovy:

这个 bean 启动默认的 tomcat websocket 监听器:

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

在搞乱插件重用的同时,发现是:

上面的项目是一个基本的 grails 应用程序,它做了两件事,一个基本的 spring socket 以及 java 1.X Websocket:

这是在 grails 3 插件中使用默认 websocket 的方法

在你的插件描述符中,你有这样的东西:

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

在这个插件中,我留下了两种启动监听器的方法:

@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())

    }

top 方法非常方便,因为您只能初始化 1 个 ServletListenerRegistrationBean,而我不得不求助于 top 方法来启用其他侦听器……我本可以将 top primary 用于所有调用。留作日后参考。。

有了这个,spring boot 现在可以模拟 web.xml 在注册监听器时的模拟。从那里加载 websockets 的实际 groovy 类就像它们一样使用默认 websocket 调用,例如 onOpen onMessage 等。

于 2015-05-05T11:02:46.723 回答
0

从 Grails 3 开始,有一种在 Plugin 方法中使用 spring 注册 bean 添加运行时配置的新方法doWithSpringdoWithWebDescriptor不再使用。

这应该适用于Servlet 侦听器

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

免责声明:我没有测试此代码。

请参阅Gails 文档

于 2021-08-09T19:32:44.303 回答