How to set parameters on Groovy/Grails InvokeHelper.invokeMethod()

≯℡__Kan透↙ 提交于 2019-12-12 05:59:34

问题


I have a Grails webapp running a Spring Integration inbound-channel-adapter that is configured to receive emails and there is a service activator that processes the message. This service activator pulls out pieces and parts of the email based on business rules and then needs to update Grails domain objects and save those changes to the database.

Spring Integration service activator code snippet:

HashMap<String, Serializable> params = new HashMap<String, Serializable>();
params.put("notification", notification );
params.put("notificationType", notificationType );
params.put("sender", notificationSender );
InvokerHelper.invokeMethod(NotificationCreationService.class, "createNotification", params);

Grails NotificationCreationService action snippet:

def static createNotification() {
    if (params.notification != null && params.notificationType != null && params.sender != null) {
        String notification = params.get("notification") as String
        NotificationType notificationType = params.get("notificationType") as NotificationType
        String sender = params.get("sender") as String

        def NotificationMessage notificationMessage = null
        notificationMessage = new NotificationMessage()
        notificationMessage.notification = notification
        notificationMessage.save(flush: true)
        ...
    }
}

NotificationMessage is a standard Grails domain class

Error generated:

org.springframework.integration.MessageHandlingException: 
    groovy.lang.MissingMethodException: No signature of method: mycompany.pyproject.mypackage.NotificationMessage.save() is applicable for argument types: () values: []
    Possible solutions: save(), save(boolean), save(java.util.Map), wait(), any(), wait(long)

If I change createNotification() to not be static then I get the following:

org.springframework.integration.MessageHandlingException: 
    groovy.lang.MissingMethodException: No signature of method: static mycompany.pyproject.mypackage.NotificationCreationService.createNotification() is applicable for argument types: (java.util.HashMap) values: [...]       
    Possible solutions: createNotification(java.util.HashMap), createNotification(java.lang.String, napa.changedetection.alert.NotificationType, java.lang.String)

I've tried other combinations of InvokeHelper.invokeMethod() and the definitions of the Grails actions with similar results. InvokeHelper has several other methods for invoking methods as well as setting different properties, but I haven't found the magic combination to make this work and any internet searched have turned up very little as far as example code for InvokeHelper.

Ideally, the Grails action would not be static and it would use the standard params mechanism. This would allow me to call the same action directly from the Grails code as well as from the Spring Integration service activator via the InvokeMethod functionality.

Does anyone have any thought on how to tie this together?


回答1:


You should not use InvokeHelper now, Grails 2 injects signature at compile time for java purposes (except dynamic finders, but you can guess why).

In your Java bean I would inject notificationCreationService using :

  • resources.groovy/xml + setter

    OR

  • Constructor + applicationContext (implement ApplicationContextAware)

Using the service reference should work, if not there is a bug to analyse there.

Where did you declare your service activator ?



来源:https://stackoverflow.com/questions/11942774/how-to-set-parameters-on-groovy-grails-invokehelper-invokemethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!