Adding AJAX mechanism on the Groovy controller? - grails

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.

Related

Inject custom tag(s) into HEAD/page for tests only

What would be the best way to inject custom script tag into head/page for tests only?
I'm writing feature specs with capybara, and I need to mock some 3-rd party api calls in the browser, I need to inject javascript code.
I tried to page.execute_script, but it was too late. 3-rd party api calls are executed immediately on DOM ready. So I need to mock things before DOM ready.
I have some ideas, but I wonder what would be the best way?
Similar questions without answers:
Using Capybara-Webkit, Inject Javascript before client code runs
How can I include javascript with javascript_include_tag at the top of the page for a capybara test
Really the only way to do exactly what you're asking would be conditional code in your html templates, based on being in the test_environment (Rails.env.test?). However a much better solution is to use a programmable proxy like puffing_billy to mock the responses the 3-rd party api would actually send. That way you are still testing the code that would actually be used in production.

MVC complex Project and code - process flow

IS there a service or third party product can help show me the code flow (start to end, including all function and class types) related to MVC 3&4 ?
I have a complex project and need to understand the underlaying framework process flow and functionality around that - any pointers appreciated!
this will depend greatly on your project as the flow in such a beast can vary dramatically. However, if we are talking about the framework itself, then that of course is well defined and follows the pattern below:
Receive first request for the application
Perform routing
Create MVC request handler
Create controller
Execute controller
Invoke action
Execute result
there are many examples of complex projects on codeplex (mvcstore for example), that will help you explore the implementation of the above.
Also, take a wander thro:
http://downloads.academy.telerik.com/svn/school-academy/Meeting-24-ASP.NET-MVC/ASP.NET%20MVC%20Pipeline.pdf
http://www.codeproject.com/Articles/43153/Beginners-Guide-s-to-ASP-NET-MVC-Framework-Part-1
I'll be honest, this stuff can be daunting at first but once you understand the pipeline, the project nuances all fit in far easier.
good luck

How to implement a Quartz.NET job in ASP.NET MVC with UnitOfWorkPattern on EF Code first

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/

How do I make Grails execute a controller method on startup?

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.

ASP.NET MVC Repeating Task

I need to run a task repeatedly on an ASP.NET MVC site; it needs to run every time interval (Interval doesn't matter).
I achieved this by setting a background thread from the Global.asax.cs Application_Start which runs the task, sleeps for time interval and then runs again....and again.
While this works it doesn't seem like the right way to do this, seems a bit hacky.
So then I replaced it with a Timer object, this doesn't work reliably - seems others have had problems with it as well - so I'm not going to use this.
This needs to run in the MVC site, it is used for pulling jobs from a queue and rendering them from the Views ready for emailing.
I thought about using the ThreadPool and getting the previous job to leave another job on it's way out but because these can be long running jobs I've read that this can end up not leaving enough threads to deal with web requests through starvation.
Before sticking with the original thread method I thought I'd ask if anyone else knows a better way of doing this.
I hope this makes sense. Effectively what I'm trying to achieve is a heartbeat. Something to act as the consumer part of the producer/consumer pattern in an MVC site.
Stackoverflow itself uses (or at least used to use) a cunning way of doing this. See here: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
In brief, you do this:
At startup, add an item to the HttpRuntime.Cache with a fixed
expiration.
When cache item expires, do your work, such as WebRequest or what have
you.
Re-add the item to the cache with a fixed expiration.
I know you've stated that:
This needs to run in the MVC site
However I think this is the wrong place for doing what you're trying to acheive.
Personally I'd set this up as a Windows Service and have the service check the database, compile the email as required, and add the email to a queueing service. The email service would then be seperate from this and would dispatch the emails at some interval.
What's stopping you at present from using another method than embedding this in your MVC site?

Resources