2

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

lucke84
  • 4,516
  • 3
  • 37
  • 58

1 Answers1

8

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.

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
  • thanks for your answer, i'm agree with you and my controllers work exactly this way, just controlling the flow and calling services that actually do stuff. The problem is that i'm looking for a way to avoid duplicated code to call services in the correct flow. Probably the answer is to refactor and put all the functionality in a class in src. – lucke84 Feb 09 '11 at 10:26
  • 1
    Well, services won't be injected into POGO classes, so if it's about controlling another services, it would be a bit overkill to load all the beans. Why not moving that service-handling logic into another service? – Victor Sergienko Feb 09 '11 at 10:46