Running scheduled methods on tomcat - java

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.

Related

How to autorun a java file on WildFly

I'm developing a web app in JEE technology with WildFly as production server. Il must make some tasks autorun every day for performing somes operations in the databases.
But I've never do it before with JEE technology. If someone could help me.
Thanks.
If you want to create a task/job which needs to run on every day as per schedule. I would suggest you consider the following options.
Database Events / Triggers:
Seems you have highlighted that you need some database operations right so my option is Event / Triggers where you can define the operations and schedule that when do you want run the task.
Quartz:
My second option is quartz schedular where you can configure your existing java Class or Servlets to run as per the time which you set in the Quartz configuration.
Java Thread Executor:
We can achieve your spec by using java thread itself where you can use Java Thread Executor Framework which provides many options and flexibility than traditional java. Anyhow, I'm recommending to use either option 1 or 2 consider it as the last case since we should maintain at least a thread to be live forever.
Note: I have just given the redirection to proceed so please go ahead and explore the concepts and pick the right one based on the cons and props of the approaches also suite to your spec.
Refer: the points discussed here.There are few things related to your spec covered

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

Execute a task every year in a Java web application

How to execute a task every year in a web application automatically? Is the Timer class helpful for this? I also heard about Quartz? I registered in their page but it seems not to be free. Thank you for your help.
Quartz is definitely a good option, it is free and opensource and uses the Apache 2.0 License (you don't have to register to download). You can also simply use cron if you are in a *nix environment.
You can also use a java.util.Timer but I would only do so if your use case is extremely trivial and you make sure you restart your Timer correctly based on some type of persistence layer on startup of your web app.
I would suggest that you use something like cron. Your requirement is very similar to the UNIX cron daemon's activity. There is a java library that provide the same capability called cron4j (http://www.sauronsoftware.it/projects/cron4j/).
Below are some schedulers that you can use.
cron4j
Quartz
jcrontab
Essiembre J2EE Scheduler
JDRing
Consider checking with the business stakeholders if a job which runs only once a year should really be fully automated. From my experience, there's a good chance that there will be minor changes to the schedule (e.g. which day of the week should the job run this year?) or to the steps of the job after such a long time.
Also, will there be other scheduled jobs added to the systems in the future? If not - will it be worth adding the overhead (implementation, testing, monitoring etc) of an automated execution to the system if there's just one job per year? Something running this infrequently can easily be forgotten and nobody might notice when it's broken.
Consider this alternative: add a button somewhere in your app to kick off the job manually, maybe on an admin page. If your app has a dashboard or landing page, add some piece of information visible only for certain users, such as admins- for example: "The X job should has been running the last time on 11/23/2011- please initiate next run before 11/30/2012".

Strategies for handling repetitive background tasks in a Java web application?

I'm building a personal Web application using Java EE 6 Technologies (the container is an application server, Jboss AS 7). I'm starting from scratch to create
repetitive background tasks, I identified two possible scenarios :
Scheduled tasks (e.g, sending bulk mails every sunday night)
Trigger tasks based on web event (e.g,running some long background updates from a web action)
What I want to avoid (I don't know if is posible) is to have some background task scattered around my platformm (some of them using cron, others using TimerTask, db jobs, etc..) becoming difficult to maintain.
What are the different approaches to handling repetitive background tasks in a Java web application, taking into account the two previous requirements ?
Related:
Scheduled Tasks for Web Applications
Scheduled task in a web application?
With EE6 you can get rid of Quartz for almost all situations using the TimerService with #Timeout annotations.
And you dont need to write a line of XML to get it working.
There is a nice example in the EE Night Hacks book, also available as source here.
You can add a Timeout method to a bean processing your trigger web events. This way, they can be maintained in one place. You can also modify the timer settings by trigger events.
I'd still look at Quartz also. I can't comment on TimerService with EE6 as a substitute as I haven't used it, but I found Quartz to be quite useful.
When I used it (quite a few years ago now), it had a config file that closely resembled what you'd find for cron. You could use that to call whatever methods you need to perform your scheduled jobs, and then simply provide some other mechanism to call the method on demand.

How to use Quartz with EJB3?

I want to be able to :
define different jobs and triggers.
modify the expirations dates and intervals on demand
pause or cancel an execution (trigger)
the jobs would be ejbs or call ejbs and i would want to manage everything from the website (the user will have to define the executions)
So i looked at the timerservice, timerobjects, timer and timerhandle. But i think it can't answer to all my needs
Quartz, on the other hand, allows me to do every thing that i want, but i haven't the slightest clue on how to integrate this into my jboss.
I read that quartz uses its own threadpool, and i don't know how to handle all this.
I use Jboss Seam in my project, but the seam/quartz integration is very limited (or the documentation is) and not 100% safe (seen on their forum : 'run forever' tasks end after only a few weeks)
If someone managed to integrate a good scheduler into his application server (jboss is a plus) and could give me directions, advices, or even code snippets, i would be thrilled.
Thanks in advance.
I have some experience integrating Quartz into a Weblogic (no jboss experience, sorry) application server. Quartz has a built in listener class that will be called upon server startup (per J2EE specs) that automatically configure the Quartz scheduler. Then in another startup class you can retrieve that scheduler, add jobs and begin serving those jobs.
You generally don't need to worry about the threadpool, Quartz can handle all this itself if you want it too. It gets its information from a properties files on startup that you can define or use the default one that comes with quartz. I have been using the default because it works for my purposes.
As far as defining jobs, you create your job classes and call your ejbs from there. It is rather very simple.
For your reading pleasure:
All Quartz documentation
Quartz JavaDoc
Cookbook containing lots of code snippets
Hope that's enough to get you started!
Great news! JBoss has a built-in scheduler already.
Since the EJB 2.0 specification included running stateless session beans and MDBs at scheduled intervals, all application servers have included this capability for some time now.
Here is an example of configuring JBoss to run a class using its built-in scheduler:
http://www.jboss.org/community/wiki/Scheduler
The best part about JBoss' implementation is that it is based on the MBean specification, which means that you can create/update/delete scheduled tasks at runtime.
Ok, i am sorry, i found in the sources of Jboss Seam just what i needed :
QuartzDispatcher to create QuartzTriggerHandle wich fires seam event at specified time and date and is manually pausable, resumable and stoppable. I use an #observer on the method i wanted to execute.
It's simple, and it works so far.
As pointed out by Poindexter, the Quartz documentation has nice starting points: Tutorial for Developing with Quartz, Examples of Usage, Cook Book (Quick How-Tos in the form of code examples), etc.
The What Is Quartz article is really good too (even if a bit old now).
For integration with JBoss, maybe have a look at How to configure a Quartz service on JBoss Wiki.

Categories

Resources