job scheduling with java and spring 3 - java

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/

Related

Best Way to Update Database Periodically in Java Web Application

I have a java web application which is running on Glassfish server. Using war file i use to deploy the application in various servers. Now to keep my application's database updated, i want to run some class (inside from application)periodically without any user interaction (should not depends on application is running or not/current users/session). i have seen that using some Timer and TimerTask class i can run any job periodically. But how to initialize it for the first time?
Please put your thoughts on how to complete this process.
Use a Job scheduler. Consider Quartz http://quartz-scheduler.org/ and start it when the program starts. The good part about using a scheduler is your program is more maintainable and you can easily create other new jobs
Create a servlet and make it load on startup. There you can initialize your task, I think.
Quartz is a good solution like already suggested. But if you need something more lite weight, I would have a look at the scheduled executor:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
It is less flexible than Quartz, but you don't need to add any dependency and it might be that it is good enough for your needs.
About starting up; I normally use Spring to wire up my application and its dependencies. So starting schedulers and running scheduled tasks is then a no brainer.
The answer changes depending on the version of Java EE you are using. In Java EE 5 and previous versions you would use a ServletContextListener to run code (call an EJB) at deployment time that used the Timer API. In Java EE 6+ you can use the #Schedule annotation which uses annotations and a cron-type syntax to schedule your task at deployment time.
Of course if you don't need automatic deployment time scheduling then you'd just create some web form that calls a EJB when submitted which in turn calls the Timer API programmatically.
For more see the Java EE 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/

Scheduling tasks in Java/Spring

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

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.

Categories

Resources