Grails Invoke a controller method from a scheduled job

筅森魡賤 提交于 2019-12-11 03:23:27

问题


in my grails application i'd like to invoke from a scheduled job class a method contained in a controller.

Reading this [http://www.grails.org/Job+Scheduling+(Quartz)], I can see that datasources and services are auto-wired by name in job classes. It seems this is not possible for controllers by default, probably 'cause controllers aren't supposed to do this kind of stuff.

BTW, is there a way to get a controller method called from a job in grails? And could this be such a bad practice to you (and why)?

Thanks in advance, Luca


回答1:


It's bad practice because Controller is intended to handle web requests - with user session and everything.

There is no user session in Quartz job.

Second, keeping functionality in Controller is bad thing on its own - Controller should better only "control" invocations to other business logic method.

I'd recommend you to move all the functionality to either a service, domain class or a POGO class in src.

Of course, you can call new MyController().method(), but no beans will be injected into controller by default.



来源:https://stackoverflow.com/questions/4943154/grails-invoke-a-controller-method-from-a-scheduled-job

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