Grails: Register job from external library - grails

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).

Related

Access WebSocketConfigurer with Grails 3 spring-websocket

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

Add behavior (relationships) to grails plugin domain classes from main application?

I have a Grails plugin I've created which is intended to support a number of applications. This plugin has an Employee domain object. The problem is that, when used in the main application, domain objects from that application need to refer back to the Employee object. So, my main application might have an Address which belongsTo the Employee class from the plugin.
How can one handle this properly in Grails 2.5.0?
Thanks in advance.
It looks like that your main and plugin depend on each other. In this case you should add the plugin location of one to another:
grails.plugin.location.<PLUGIN-NAME> = "<PATH TO YOUR PLUGIN>" Ex: "../myPlugin" assuming it is located on the same folder structure as the plugin
and in your plugin
grails.plugin.location.<APP-NAME>= "<PATH TO YOUR APP>" EX: "../myApp"

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

What is the best way to work with services in grails

I've just started a project on grails and didn't find how to work with services using dependency injection and interfaces.
As I've seen so far in the documentation when you create a service - it's just a groovy class which can be auto wired wherever I want.
But what if I want to have an interface for a service and to inject one of its implementation like I did in Java using spring?
eg I want to have a service interface. let it be MyService.groovy
it will have 1 method doSmth()
and I'll have 2 implementations - MyServiceImpl1.groovy and MyServiceImpl2.groovy
I have a quartz job doing something like this
def myService
myService.doSmth()
Where should I put groovy interface (folder)? Shall I create a package for that in src/groovy?
How to configure resources.groovy to wire "myService" with 1 of the service implementation?
Any thoughts are appreciated
Thanks in advance!
Running grails create-service [name] is a convenient way of get a service deployed, but it doesn't create an interface with implementation, as you're looking for.
I'd suggest putting your interface and implementations into src/groovy and using resources.groovy to wire them up (you can access the environment, if you want to deploy a different implementation by environment).
Take a look at the 'Using the Spring DSL' section in chapter 14.2 of the user guide for how to wire up your service in resources.groovy. You also have the option of using resources.xml if you want to wire with XML, but I'd definitely recommend the Groovy DSL.
Just run grails create-service [name]

Is it possible to add Grails MVC classes at deployment time?

I'm writing a Grails app which I'd like 3rd parties to augment at runtime. Ideally they would be able to add a JAR/WAR to the webapp directory which contains new domain, controller and service classes, new views, and other content.
Is there a simple way to do this within grails? Would it be simplest to create a startup script which copies the new classes etc. into the relevant directories and then updates grails.xml and web.xml?
You will be able to do this in version 2 of grails in which plugins will be also OSGI plugins http://jira.codehaus.org/browse/GRAILS/fixforversion/15421
It seems that the Grails plugins will actually fit quite well for this: http://www.grails.org/Understanding+Plugins
A plugin can do just about anything... One thing a plugin cannot do though is modify the web-app/WEB-INF/web.xml or web-app/WEB-INF/applicationContext.xml files. A plugin can participate in web.xml generation, but not modify the file or provide a replacement. A plugin can NEVER change the applicationContext.xml file, but can provide runtime bean definitions

Resources