I'm a java developer and got to work with a legacy Grails application. I have a controller class and a number of java classes. The controller has methods to start/stop tasks described in java classes. I'm using a ScheduledExecutorService for that.
The problem is that each time application is redeployed I have to call http://application/controller/start to make it initialize the scheduler. I want this controller.start() method to be called once when te app is launched. How can I achieve that or is there a better way to do this?
I would suggest to put this method in a service. You can call the service both from controller and from Bootstrap.groovy after application starts. You can also use standard #PostConstruct on service to start the process.
Your business methods should be located in services not in controllers.
If you really need to use controller, try this from Bootstrap.groovy:
new Controller().start()
I don't have env to test it but there shouldn't be a problem with it.
Related
I want to create a dynamic web application where the user has the opportunity to prepare its interfaces. This is equivalent to OpenERP but specific to a single domain.
I searched if grails allows me to do that.
I have tried to make calls to grails commands in code but I have concluded that this it is not possible to invoke a new class at runtime.
So, if someone has an idea, help me please.
I have a scenario where I use JS(KNOCKOUT) to call Groovy controller and that indeed invokes a service call to the backend. The groovy controller calls about 4 different services when this process is invoked from frontend. Groovy controller currently works in the order how its been coded and waits for each services to complete its task before the other, which is correct. I need to re-factor code so that the service calls should run parallel(not wait for one another, kind of AJAX nature). I understand I can use Threads to achieve it. Is there any other library or Groovy functionality that I can make use of?
I cannot use groovy.Async because we use grails 2.2.2 in our project. Any help is greatly appreciated.
Take a look at GPARS. It's got a very good reputation as a parallelism API for Groovy.
I have ASP.NET MVC 3.0 application, with EF Code First for data layer. I have implemented a Unit of work pattern, I’m binding context of the unit of work on HttpContext.Current.Items[SomeKey] collection. Unit of work is created and committed in OnActionExecuting/Executed events on controller. I’m instantiating repositories using Windsor Castle.
Now I need to use Quartz.net to run a job periodically in my app, this job need also use few repositories. The problem is, that in a SchedulerJob implementation, there is no HttpContext available (indeed). How can I instantiate a repository (which takes UnitOfWorkFactory as constructor parameter) from a Quartz.net Job in that case? How can I substitute missing HttpContext? I will probably need to implement another UnitOfWorkFactory, but I’m not sure where I can bind my context and how to register different factory just for Quartz.net thread. Can you please show me a way or pattern? Thank you.
You might want to consider writing your own job factory and injecting your repositories there. I wrote a post on how to do this here. Windsor also has a facitlity for integrating Quartz.net directly.
One last comment... you shouldn't host Quartz.Net in your web app if you are going to schedule long running jobs or if you are going to schedule jobs to run periodically. IIS process recycling will not let your scheduler run properly. A windows service is the way to go.
The unit of work implementation belongs to the business logic layer and should not depend on a specific presentation layer such as MVC.
I made a custom UnitOfWorkScope that I've used in a couple of projects: http://coding.abel.nu/2012/10/make-the-dbcontext-ambient-with-unitofworkscope/
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
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.
Documentation says:
The Grails team discourages the
embedding of core application logic
inside controllers, as it does not
promote re-use and a clean separation
of concerns.
I have one API controller and a few Groovy classes in src/groovy folder. Those classes just implements my application logic so actions in API controller works in this way:
//index page
def index = {
render new IndexApi().index(params) as JSON
}
I'm curious - is there any reason to move my application logic from plain groovy classes into services ?
Actually services are not just about transactions. Services are great for zero-config injectable singleton components, and they can can be reloaded without restarting the whole grails environment, AND they can be discovered as artefacts and hence automatically exposed with remoting plugins.
If you want transactional behavior you should put your logic in the Services. Else you would have to take care about it yourself, which is not in the spirit of using Grails.
Being not a grails expert myself, I put my 'not transactional' classes outside the service layer, like builder classes, helpers, and other logic that is not transactional but used from the service layer.
There are three reasons:
It makes the controller smaller -> easier to understand and maintain
It makes you logic easier to test.
You really don't want to manage your transactions manually.
If you would put everything in the controller, you would need to create the Web runtime to be able to run any test. If your logic is outside, you can copy the data you need from the HTTP request and all the other sources and just call the code. So the logic isn't depending on HTTP sessions, requests or anything else you don't want to.
For example, to test JSPs, you need a HTTPRequest. For a request, you need a HTTPSession and a JSPWriter. Those need a session context. So just to be able to run a single test, you need to set up and initialize a whole bunch of classes. And all of those are interfaces and the implementations are private. So you must implement the actual methods (all 300 of them) yourself. And you better get this right or your tests won't test what you want.