Access WebSocketConfigurer with Grails 3 spring-websocket - grails

I'm trying to register a WebsocketHandler in a Grails 3.2.x app with the spring-websocket plugin.
I tried creating a custom websocketConfig with the grails create-web-socket-config command, but the resulting class does not implement org.springframework.web.socket.config.annotation.WebSocketConfigurer to be able to add handlers to with the registerWebSocketHandlers(WebSocketHandlerRegistry registry) event.
How would I be able to do this within an AbstractWebSocketMessageBrokerConfigurer?

within an AbstractWebSocketMessageBrokerConfigurer, you wont be able to do that.
but you should be able to just supply another #EnableWebSocket annotated #Configuration bean implementing WebSocketConfigurer that is picked up by component scan or you register it manually in resources.groovy.
ref. http://docs.spring.io/spring/docs/4.3.4.RELEASE/spring-framework-reference/html/websocket.html#websocket-server-handler

Related

Grails: Register job from external library

I need to separate a grails service into a dedicated lilbrary in order to use this service across many applications. This works fine for the service itself as I am able to register the service bean in the resources.groovy (see https://docs.grails.org/latest/guide/spring.html).
This service happens to use a quartz job to get some functionality triggered regularly. So naturally I would move this job into the library and need to register it in the main application.
How can that be achieved? Thanks for you time!
Create grails plugin using this guide:
https://docs.grails.org/latest/guide/plugins.html
Then in src/main/groovy/YOUR_PACKAGE/YourPluginGrailsPlugin.groovy you can add bean configuration in doWithSpring() method (in same as resources.groovy way).

How do I create a grails 3 plugin which exposes a JSON view?

In my custom Grails plugin, how can I set up a JSON view for a non-domain class and get client apps to use it by default?
I have a view file in the plugin:
/views/com/mycompany/myplugin/myclass/_myClass.gson
When I do grails install, I can see that this .gson file is in the generated JAR. However, the client app is not using it.
What can I do to make it work?
Are there any settings or steps that can make troubleshooting easier?
I am using Grails 3.2.4.
Update:
When I copy the view into a client app, using the exact same path, the view is getting invoked. It's only when the view is defined in the plugin that the view cannot be found.
The framework seems to be trying to look up the plugin as a class from the classloader:
myclientproject_com_mycompany_myplugin_myclass__myClass_gson
How do I get my plugin to add this class to the classpath?
For my use case, what I actually needed was a custom converter.
See:
In JSON views, how do I flatten an object out as a single string?
This obviated the need for my plugin to publish a view.

Connect to Grails Datasource from Grails Plugin Command

I'm attempting to write a plugin for Grails that will automatically generate my domain classes based on special views that we're designing in our legacy database. I basically just want to save myself some time manually writing all the mapping stuff required to make the domain classes work.
Do I have access to the dataSource defined in the application.yml of the project from a custom ApplicationCommand implementation? If so, how do I pull it so I can open my connection to the database?
I found the answer here. The dataSource bean that is injected into regular artefacts can be accessed through the ApplicationContext like this:
def dataSource = applicationContext.getBean('dataSource')

Grails: Intercept the creation of a service from a plugin?

I have a plugin that's shared between multiple applications and I need to set some application specific options that will be used in a service in the plugin. It's an in house written plugin, however, the plugin has to be unaware of which application it's running in.
The best way I can think of doing this is to have some code run in the application right after the service is created in the plugin and call a method on the service to set the options. Is this possible?
If it's not possible, what other design could I implement to pass options to the plugin from the application.
Btw, these options need to be set when the application starts as well as throughout the running of the application.
It's best to use Bootstrap.groovy to to call a method(s) on a Grails service when an application starts. This service can be provided by the application or plugins in the application. Here is a quick example of how to do so:
Bootstrap.groovy
class BootStrap {
def myExampleService
def init = { servletContext ->
myExampleService.someMethodOnTheService()
}
}

grails mybatis-plugin validation location

I cite from mybatis plugin documentation:
"When working with MyBatis plugin your "Domain" classes should be
located in src/groovy and not in grails-app/domain. This is necessary
to avoid conflict with GROM since MyBatis plugin can coexist with
existing GORM Domain classes."
So where should the validation and constraints be located, when I want to use grails with MyBatis plugin ?
You can add a #Validateable annotation to any Groovy class in Grails, and you will be able to validate it... The Plugin currently doesn't check for any validation errors so you will have to implement that code yourself.
From official Grails documentation:
Classes which define the static constraints property and are annotated
with #Validateable can be made validateable by the framework
http://grails.org/doc/2.1.0/guide/validation.html#validationNonDomainAndCommandObjectClasses
You could even write a custom MyBatis Interceptor (see https://github.com/fzilic/Grails-MyBatis/blob/master/src/groovy/org/grails/plugins/mybatis/locking/OptimisticLockingInterceptor.groovy) and register it after the SqlSession is created...
Currently the MyBatis plugin doesn't support registering custom Interceptors in it's configuration, but they could be added to the interceptor chain
def factory = GrailsApplication.mainContext.getBean("sqlSessionFactoryBean_dataSource")
factory.configuration.interceptorChain.addInterceptor(Interceptor)
Support for this might be added in future versions.

Resources