Scheduling tasks in Java/Spring - java

There is a requirement in the project that will have a scheduled task that will do some job.
The project is Spring based and the scheduled job will be part of the application war.I have
never implemented this kind of functionality before.
I have heard of Quartz. Also, I read somewhere that Spring provides some functionality to schedule tasks. So, I was thinking if I am already using Spring then why to go for some other API(Quartz).
I am not sure which one to use? what will be the pros/cons of one over another?
Please suggest what will be the best way to approach my requirement.

I have used Spring's Task execution and scheduling - http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html

Related

job scheduling with java and spring 3

Hi I have one requirement to schedule task using java. I can use threads and timer task of java but if we restart application or server that threads will get stop and after starting application there is no schedule task. so please help me is there any way for that i am using java with spring 3.1
Spring batch is the solution for your case. you can find documentation here , documentation is pretty good with sample programs which are extensively used. It has the feature to restart the job.
You could try with quartz-scheduler, Its easy to use with spring.
For your reference : http://www.mkyong.com/tutorials/quartz-scheduler-tutorial/

Running Multiple Jobs using Scheduler in Real time application

Can anyone help me out to choose which scheduler between "Quartz" or "Quartz+Spring" to run multiple jobs simultaneously in real time application.
Quartz can run multiple jobs without Spring. Use Spring if you want the features that Spring brings to the table. I would use Spring, because Spring is a great integration framework and makes it easy to integrate technologies like Quartz into an application.
I prefer TimerTask class.
Refer this tutorial
http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/

Running scheduled methods on tomcat

I am trying to set up a method that will be automatically run by the server at a specific time. For instance, a method that sends out emails to contacts every Friday at 9.00 am. I have seen methods that are run when the server is first started and was wondering if what I want to do is possible. If it is possible, can someone point me to where I can start reading up how to do this. Any help will be highly appreciated.
There is an excellent library quartz which can help you create scheduled tasks within your application. See e.g., the Job Scheduling in Java guide by o'reilly.
If you really want to do it manually (and not use specific tools like Quartz), you could use a Timer, which would be created when the application is deployed and canceled when the application is destroyed, using a ServletContextListener declared in your web.xml.
Be prepared for additional complexity if your application is clustered on multiple servers, though.
I also recommend using Quartz as Johan already suggested, it is a well-established solution for job scheduling in Java applications and also allows for central job storage in a database and clustering of multiple Tomcat instances.
In case your web application uses the Spring Framework, you could instead use the built-in scheduling support instead.

How to do background tasks in struts2?

i had a look on the struts plugins list here and wasn't able to find a plugin to do background/scheduled tasks.
What i want to do, is run a daily task that pulls files from a few servers. I'd like this task to be run from within the web app, so that my importer gets access to all the data classes, also it would be less complicated IMO.
Any common way to go about this?
Thanks
Your best bet is probably Quartz which provides a way to define jobs, and a number of triggers, including cron like expressions. It can be embedded in your app.
Together with Struts2 I use the Spring Plugin for this Issue.
With Spring you can easy define Timer Tasks or Quartz Jobs to execute
Background Jobs.

Java – Create a workflow in Quartz

I am considering using the Quartz framework to schedule the run of several hundred jobs.
According to their API, jobs can be scheduled to run at certain moments in time but not to run one after the other (and stop a chain of jobs if one fails).
The only recommended methods I was able to find are:
Using a listener which notices the completion of a job and schedule the next trigger to fire (how to coordinate this?)
Each job will receive a parameter containing the next job to run and, after completing the actual work, schedule its run. (Cooperative)
Do you know a better method to create a workflow of jobs in Quartz?
Can you recommend other methods/framework for implementing a workflow in Java ?
EDITED: In the meantime I found out about OSWorkflow which appears to be a good match for what I need. It appears that what I need to implement is a "Sequence Pattern".
When Quartz documentation talks about "Job", it is referring to a class implementing the "Job" Interface, which is really just any class with an "execute" method that takes in the Quartz Context object. When creating this implementation you can really do whatever you want.
You could create an implementation of the Quartz Job Interface which simply calls all the jobs in your workflow in series, and throws a JobExecutionException exception on failure.
It sounds to me like you want Quartz to schedule the first job, and chain everything off that.
Have you looked at encapsulating each task using the Command Pattern, and linking them together ?
I've worked on a project called Dynamic Task Scheduler that use Quartz to execute job chains implementing a simple workflow in a fault-tolerant way (definied in XML format).
Take a look at http://sourceforge.net/projects/dynatasksched/
The project is beta, but I think it can gives you some ideas to start..
Hope it's useful!
For job chaining support for Quartz, you may want to check the QuartzDesk project that I have been involved in. In version 2.0. we have added a powerful job chaining engine that enables you to orchestrate your Quartz jobs without the need to modify your application code.
The engine takes care of propagating the job execution result and other parameters from the source job to the chained target job.
QuartzDesk comes with a GUI that allows you to dynamically update your job chains without disrupting your application.

Categories

Resources