Daemon in Java: simple schedule application? - java

This app must perform connection to a web service, grab data, save it in the database.
Every hour 24/7.
What's the most effective way to create such an app in java?
How should it be run - as a system application or as a web application?

Keep it simple: use cron (or task scheduler)
If that's all what you want to do, namely to probe some web service once an hour, do it as a console app and run it with cron.
An app that starts and stops every hour
cannot leak resources
cannot hang (may be you lose one cycle)
consumes 0 resources 99% of the time

look at quartz, its a scheduling library in java. they have sample code to get you started.
you'd need that and the JDBC driver to your database of choice.
no web container required - this can be easily done using a stand alone application

Try the ScheduledExecutorService.

Why not use cron to start the Java application every hour? No need to soak up server resources keeping the Java application active if it's not doing anything the rest of the time, just start it when needed,

If you are intent on doing it in java a simple Timer would be more than sufficient.

Create a web page and schedule its execution with one of many online scheduling services. The majority of them are free, very simple to use and very reliable. Some allows you to create schedules of any complexity just like in cron, SqlServer job UI, etc. Saves you a LOT of headache creating/debugging/maintaining your own scheduling engine, even if it's based on some framework like Ncron, Quartz, etc. I'm speaking from my own experience.

Related

quarts or simple pojo

I'm writing an java based app (not web app) and it should be able to run standalone without any container the task it carries are below:
windows scheduler fires off either quartz or simple POKO
pick up file(s) during midnight
import the data into DB
move the files over from original destination to another drive
Now, the dilemma I'm having is I've been reading around and it appears quartz need web container to function.
Is that correct AND what would be most simple and durable solution?
According your question: Quartz does not need a web container, it can be run in any java application. See Quartz Quickstart Guide for how to configure Quartz.
If you use Quartz the windows scheduler shouldn't be necessary, but this implies that your java application is running constantly.
I think Quartz has the advantage, that you can configure your application in one place and do not need to consider os specific scheduling. Further more Quartz makes you independent of the os specific scheduling mechanism.
But: All this advantages are not relevant if your application is not running all the time.
On the other hand if you want it to be a fire and forget like application, that runs, does its work and then quits again, you will be on the safe side to delegate the task of scheduling to the operation system your application runs on.
So, for this specific context I think using the operation system's scheduling mechanism is the better option.

Is it possible to run a cron job in a web application?

In a java web application (servlets/spring mvc), using tomcat, is it possible to run a cron job type service?
e.g. every 15 minutes, purge the log database.
Can you do this in a way that is container independent, or it has to be run using tomcat or some other container?
Please specify if the method is guaranteed to run at a specific time or one that runs every 15 minutes, but may be reset etc. if the application recycles (that's how it is in .net if you use timers)
As documented in Chapter 23. Scheduling and Thread Pooling, Spring has scheduling support through integration classes for the Timer and the Quartz Scheduler (http://www.quartz-scheduler.org/). For simple needs, I'd recommend to go with the JDK Timer.
Note that Java schedulers are usually used to trigger Java business oriented jobs. For sysadmin tasks (like the example you gave), you should really prefer cron and traditional admin tools (bash, etc).
If you're using Spring, you can use the built-in Quartz or Timer hooks. See http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html
It will be container-specific. You can do it in Java with Quartz or just using Java's scheduling concurrent utils (ScheduledExecutorService) or as an OS-level cron job.
Every 15 minutes seems extreme. Generally I'd also advise you only to truncate/delete log files that are no longer being written to (and they're generally rolled over overnight).
Jobs are batch oriented. Either by manual trigger or cron-style (as you seem to want).
Still I don't get your relation between webapp and cron-style job? The only webapp use-case I could think of is, that you want to have a HTTP endpoint to trigger a job (but this opposes your statement about being 'cron-style').
Generally use a dedicated framework, which solves the problem-area 'batch-jobs'. I can recommend quartz.

Call function periodically in Java

we need run one function periodically in Java web application .
How to call function of some class periodically ?
Is there any way that call function when some event occured like high load in server and so on . what is crontab ? Is that work periodically ?
To call something periodically, see TimerTask
If you need something more robust you can use Quartz
As for crontab is the scheduling tool on Unix machines.
For calling methods when server has high load, you have at least two possible approaches. Your App Server might have management hooks that would you allow to monitor its behaviour and take progrommatic action. Alterntaively you have some system monitoring capability (Eg. Tivoli or OpenView) and it generates "events", it should not be too hard to deliver such events as (for example) JMS messages and have your server pick them up.
However, you might want to explain a bit more about what you want to achieve. Adaptive application beahviour might be quite tricky to get right.
If you want to run something periodically, don't do it in the webserver. That would be a very wrong approach IMO. It's better to use cron instead, if you are on a Unix-like operating system. Windows servers offer similar functionality.
we need run one function periodically
in Java web application
(1) So look in your deployment descriptor (web.xml) define a listener to startup at startup time.
How to call function of some class
periodically ?
(2) Create a Timer in the listener.
Is there any way that call function
when some event occured like high load
in server and so on
(3) and run some Threads to check for system conditions that are accesible with Java, even run system progs (uptime, etc) and parse the output.
crontab could be a way but the execution of Java will start another JVM and that is really the hot thing in servlet containers: all runs in the same JVM.
Don't forget about java.util.concurrent - it has a lot of classes for scheduling, e.g. ScheduledThreadPoolExecutor, if you need more than a simple Timer.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html
There is also a backport of it to Java 1.4: http://backport-jsr166.sourceforge.net.
If you already use Spring, you might want to have a look at Spring's task execution framework - using #Scheduled and #Async for annotating methods as tasks and implementing the functionality in a Processor that delegates the actual work to a Worker, as described in:
http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/
The advantage is that you can define timers using a cron-like syntax in your spring context and you don't need anything special to set up tasks, it is also well integrated into Java EE applications and should play well with web servers (which custom Threads tend not to do always).
How to call function of some class periodically?
There are several solutions: a Timer, a Java cron implementation like cron4j, Quartz, or even the EJB Timer API. Choosing one or the other highly depends on the context: the type of application, the technologies used, the number of jobs, etc.
Is there any way that call function when some event occurred like high load in server and so on
You could maybe use JMX in your jobs to access and monitor informations and trigger an action under some specific condition. But this is more a pull mode, not event based.

Oracle scheduled tasks?

I'm drawing a design for a system to do daily business functions for my company. It will consist of a Oracle 10g database with Pl/SQL packages and a Java-based web application. All of this is running on a Solaris 10 server. Aside from handling transactions from the web interface, scheduled tasks need to run on the database to run calculations and load data etc.
This is a redesign of a legacy system that currently controls everything with a plethora of cron jobs. Given the task of redesigning it, would you do it differently? I know Oracle has its own task scheduler, but the DBA argues that he would rethink using it because if the database is down or offline for some reason, it can't send alerts or log errors of any kind. The cron jobs currently have the ability to send SMS messages or emails should one of the tasks fail. Another option would be to have the web application do it somehow.
What do you suggest?
Are all the scheduled tasks related to the database? If so, then your DBA's objection is irrelevant: you don't want to run the jobs when the database is offline for planned downtime, and the DBA ought to have something in place to alert them if the database is down for unplanned reasons, rather than relying on a signal from a failing cron job.
If you have jobs which run on other parts of the architecture without touching the database then certainly an external scheduler makes sense. There are plenty of commercial products, but if you want to go for FOSS then you probably ought to look at Quartz.
Having used both cron and the Oracle job scheuler - I have always found it a lot more reliable and easier to user and understand cron. It has more things that it can do (interface with the entire OS, not just Oracle). I would choose cron.
My rule of thumb for scheduled jobs is consistency. Since you've already got a lot of infrastructure in place like alerting I'd stick with cron.

Help with Cron Jobs for Java Google App Engine

I am developing an application on google app engine and was just checking out cron jobs.
Looking at this document it seems pretty easy to schedule the jobs with config files and so on. My question is related to what I should actually put in the url the scheduled task triggers.
I was thinking a jsp which triggers a servlet that does whatever I need done, but not having a lot of experience with this technology I was wondering if there is a standard/better way of achieving this.
How are people doing things such as this?
Any help, pointers appreciated!
Your approach is just fine, and in line with Google's App Engine Cron Service documentation.
The Google App Engine is somewhat unusual place to run code, with all of its restrictions and such. The other 'normal' approaches for scheduling tasks in Java (external cron system, jcrontab, never ending loop with Thread.sleep(), etc.) are not possible.

Categories