Looking for interceptors for grails service class - grails

Is there any interceptor I can use to validate incoming requests to the Grails service classes/Endpoints?
I know interceptors for controllers but I want to do it for Service/endpoint.

This sounds like what you are looking for:
Intercepting Service Methods

In the link mentioned above(Kelly's reply), interceptor logic for Service has been added in BootStrap class. That might suffice in most of the cases but not all. In case you are using Quartz scheduler then job might get triggered even before BootStrap has made required modifications.
The best place to modify a service method would be via custom plugin. You can decorate service method in doWithApplicationContext or doWithDynamicMethod available in Plugin class. These methods are triggered at first and guarantee that changes made will be available to all other classes. Please refer Grails custom plugin documentation for more information.

Related

Grails - restarting Rabbitmq plugin consumers by calling method on plugin class

I am using the Grails Rabbitmq Native plugin. When I launch the application, I don't want the RMQ consumers to be automatically started, so in my Config.groovy I have defined:
rabbitmq.enabled == false
The code within doWithSpring() (https://github.com/budjb/grails-rabbitmq-native/blob/master/RabbitmqNativeGrailsPlugin.groovy#L114) means that certain wiring isn't carried out if this flag is false.
At some point, I want to be able to start the RMQ system up. I'd like to call a method defined within the plugin class, such as restartRabbitContext() (https://github.com/budjb/grails-rabbitmq-native/blob/master/RabbitmqNativeGrailsPlugin.groovy#L231) to start up the RMQ consumers. I think I will need to carry out some of the wiring myself.
Is there a way to do this? What is the import required to be able to access the plugin class's methods?
Your best bet is to use the GrailsPluginManager to access your plugin by name using getGrailsPlugin. From there you should be able to access the plugin as a GrailsPlugin and access the public methods defined in the plugin itself.
The GrailsPluginManager can be obtained though the grailsApplication such as: grailsApplication.pluginManager. In the very rare event you can't use DI you can always fall back to Holders to get to the GrailsPluginManager (though this is a very rare case).

Are there existing gems or services for web hook implementations?

Most major services like github provide Webhooks functionality.
So, with github - you can set hooks to notify you on every commit.
In the same time web hooks are not that easy.
Each web hook has to be ran asynchronously to not block web server at the time of communicating with destination. And it can take a good time (10-15 seconds). There should be implemented repeating functionality (in case if destination is not responding).
So, I think that there for sure should be some service or library which will do this for you.
Do you know any of these?
I need to send data to lots of endpoints and to receive a response from them..
You need a gem providing background job functionality. Sidekiq and Delayed Job are ones of most frequently used.
Idea is that after request (in ruby on rails you can use after_action hook or just do it in controller action) you create a job which will be executed asynchronously. Put logic you need in the job class
Both sidekiq and delayed job have repeating functionality, just pick gem that looks simpler to use
There is a gem called ActiveHook but it does not appear to be maintained anymore.
Benedikt Deicke wrote a good article on sending webhooks with Rails, you should check it.

jpa repository websocket #MessageMapping

I found out, that spring does not create a transaction for websocket requests that are going to a action annotated with #MessageMapping. Even if i annotate the action as #Transactional nothing happens.
I also tried the way with an action that is #RequestMapping annotated. This works as usual.
Has someone an idea how i can make this work? How to make an websocket request transactional?
You shoould show your code, because it really works, unless you have some async internal logic, which doesn fit for single-threaded transactional boundries.
I fixed this issue by my self:
I just had to rename my entity manager factory to entityManagerFactory... Thats it.

Capture outgoing HTTP request from Controller / Service

So I have the following scenario (it's a Grails 2.1 app):
I have a Controller that can be accessed via //localhost:8080/myController
This controller in turn executes a call to another URL opening a connection using new URL("https://my.other.url").openConnection()
I want to capture the request so I can log the information
I have a Filter present in my web.xml already which does the job well for controllers mapped in my app. But as soon as a request is fired to an external URL, I don't get anything.
I understand that my filter will only be invoked to URLs inside my app, and that depends on my filter mapping which is fine.
I'm struggling to see how a solution inside the app is actually viable. I'm thinking of using a mixed approach with the DevOps team to capture such outgoing calls from the container and then log them into a separate file.
I guess my questions are:
Is there a way to do it inside the app itself?
Is the approach I'm planning a sensible one?
Cheers!
Any reason why you don't want to use http-builder? There a Grails plugin for it, and it makes remote XML calls much easier than handling the plumbing yourself. At the bottom of the linked page they describe how you can enable request logging via log4j configuration.

NServicebus Message Handler Lifecycle in IOC StructureMap

What is the required IOC instance lifecycle I need to use in conjuction with a NServiceBus message handler if I want an instance per message handled.
PerRequest won't work, since given the numerous constructor dependenices and dependency graph I have, it gives me many instances when I require only one per Handle(MessageX msg) call. I can't or don't want to inject it directly into the message handler since it is required further down the object graph. E.g. inject IPersonService, depends on IPersonRepository, they can be per request (Default), but IPersonDBContext need to be per message call.
PerThread won't work since NServiceBus uses the same worker threads over and over.
Singleton, HttpContext, etc.. are obviously not applicable.
Has anyone came across this with either StructureMap or Castle?
I might be missing something here, but PerRequest will give you a new instance for each MessageHandler ( the messagehandlers are them selves registered as PerRequest). I have just committed a fix for a bug that caused messagehandlers to fire multiple times for each message. I wonder if that bug has been misleading you (try to get the lastest 2.0 build from CI and see if that does it for you)
http://teamcity.codebetter.com/viewLog.html?buildId=7087&tab=artifacts&buildTypeId=bt96
Hope this helps!

Resources