I am want to distribute a jar script that needs to run periodically. I don't know which OS the end user will have.
Is there a cross OS solution that I can use to run a jar file at a specific time each day.
That is, in windows it adds a scheduled task and a cron task etc.
Any help appreciated !
You could use a task scheduling framework written in Java, for guaranteeing portability across OS - because each OS has its own scheduling commands, you won't find the same tools across different platforms.
For instance, take a look at Quartz. But be warned, you'll have to learn how to configure, setup and program tasks with it, IMHO it's not that simple. But then again, that's the price you have to pay for portability.
Yet another option would be to schedule a task using a Timer service (take a look at the tutorial), but for that you'll need a Java EE compliant server. Either way notice that "running a jar" really means executing the main method in the jar file, which is just a call to a Java program.
If you are looking for OS independent scheduler, such a thing does not exist. Quartz is a task scheduler for java environment. For example - you can use quartz to schedule tasks in your java program. You have to write a java program and keep running it all the time for using quartz for your requirement. In other words, you have to start your java program when the OS boots up so that it is ready to run your scheduled job. Too much complexity for the task at hand! You should consider using OS specific scheduler for your need.
Related
I am trying to do the following, call a method getSessionID() every thirty minutes from the start of execution. The method will be called from a long running service application.
I have been looking at threads in java but I am not clear on how to achieve this.
Does anyone have any sample or sudo code ?
I recommend you to use Quartz library:
Quartz is a richly featured, open source job scheduling library that
can be integrated within virtually any Java application - from the
smallest stand-alone application to the largest e-commerce system.
Quartz can be used to create simple or complex schedules for executing
tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks
are defined as standard Java components that may execute virtually
anything you may program them to do.
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.
Are there any frameworks available in java or .NET to execute long running tasks?
This framework should give me the flexibility to plug in my implementation to execute the job and also ability to control the run-time like the number of tasks that execute and load balancing of execution.
I would like to here different approaches to solve the problem.
in Java, you can try the Java 5 Executor framework, Spring Batch or Quartz depending on your need.
In Java:
ThreadPoolExecutor offers you a thread pool and lets you execute tasks in it. It also offers means to determine current active tasks, number of already executed tasks etc.
Windows Workflow Foundation might work for you in .NET depending on what exactly you mean by "Long Running Task". It has the capability of recording the task's state and restoring it at a later time, allowing the task to survive a reboot or other interuption.
TaskParallelLibrary in .Net 4.0 has facility to work with parallel, asynchronous and long-running tasks.
See http://msdn.microsoft.com/en-us/library/dd460717.aspx
In .Net for "long running tasks" it would be Windows Workflow Foundation. And for "ability to control the run-time like the number of tasks that execute and load balancing of execution" it would be Task Parallel Library. You might need to evaluate your problem statement and see if either one of these or combination of both of these frameworks can solve your problem.
How can I run a task daily at a specified time (say 11:00 am) using java.util.Timer? I'm using JDK 1.4.2, I know it's old, but it's what the project requires.
Quartz is the most well known solution to schedule processes in Java environments, but you have a lot of options. Check this list:
Quartz is an open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application. Quartz can be used to create simple or complex schedules.
Jcrontab is designed to be extended and integrated with any project. Reads and stores the tasks to execute in a file, a database or an EJB and provides a web UI and a basic swing GUI.
Essiembre J2EE Scheduler is a simple task scheduling mechanism for J2EE applications. This library can be considered a wrapper around the Timer and TimerTask classes found in the standard Java API. Configuration for tasks to be executed and their frequency is XML-based.
cron4j is a scheduler for the Java 2 platform which is very similar to the UNIX cron daemon.
Oddjob's goal is to provide some order and visibility to all the batch files and cron jobs that tie an enterprise's critical business processes together.
Fulcrum Scheduler provides a scheduler service. It is based on the TurbineScheduler provided with Turbine, but all older stuff has been removed. Currently ONLY the non persistent Scheduler is done. It loads scheduled jobs from the component config xml file.
Gos4j -Goal Oriented Scheduling for Java- is a way of organising processing priorities based on goals.
Job Scheduler is a batch program operating as a demon, and can be controlled using a graphical user interface. The Job Scheduler uses an XML configuration for the scheduled programs, scripts and for the timing and frequency of task processing. An API is available that hands control of events and logging to your jobs.
JDRing is a lightweight Java scheduling library that is simple and small, but still supports ringing alarms at specified intervals, as one-time events, or on complex schedules with full cron-like control.
jBatchEngine is a batch job spooler written in Java. In constrast to time driven schedulers like Cron, jBatchEngine is event driven.
MyBatchFramework is an open-source lightweight framework designed to create easily robust and manageable batch programs into the Java language.
Super with SuperScheduler and SuperWatchdog is a Java job scheduler with rich GUI for all applications. It is platform neutral. Especially good to be a job scheduler for Linux and Solaris. It provides a super set of functionalities of the Scheduler of Microsoft Windows. It provides event-triggered scheduling. It can schedule tasks in a distributed environment. A task will be executed once and only once among all machines in the network. All tasks are holiday adjustable. Even every job is a STANDBY job, the history will be a good trace for important tasks. It supports Internationalization.
source: Open Source Job Schedulers in Java
Look into TimerTask and Timer - both are in that version of the JDK.
Timer :
public void schedule(TimerTask task, Date firstTime, long period)
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Set it to run the first Date you want then the number of milliseconds in one day as your delay.
Is it possible for you to use a library such as cron4j? It will make your job much easier!
Java Timers can run an arbitrary job at intervals, pre-fixed times, etc.etc.
Quartz library
If you really want to be bare-bones about it, wrap it in a shell script and put in in cron.
You have to use Quartz
I have never know who launches Quartz in first place though.
If you have an application server or similar artifact you can configure the Quartz job there and have it run your task at the given time.
Maybe, that recent post helps you:
Will this pause my Java thread for a minute?
My response for that question is use a java built in implementation based on java.util.Time and java.util.TimerTask classes:
Will this pause my Java thread for a minute?
Or, you can use the crontab service for *nix platforms (available for Windows platforms too). That's the simplest and light-weight style to run a standalone job periodically.
[]'s,
And Past
I already asked a separate question on how to create time triggered event in Java. I was introduced to Quartz.
At the same time, I also google it online, and people are saying cron in Unix is a neat solution.
Which one is better? What's the cons and pros?
Some specification of the system:
* Unix OS
* program written in Java
* I have a task queue with 1000+ entries, for each timestamp, up to 500 tasks might be triggered.
Using cron seems to add another entry point into your application, while Quartz would integrate into it. So you would be forced to deal with some inter-process communication if you wanted to pass some information to/from the process invoked from cron. In Quartz you simply (hehe) run multiple threads.
cron is platform dependent, Quartz is not.
Quartz may allow you to reliably make sure a task is run at the given time or some time after if the server was down for some time. Pure cron wouldn't do it for you (unless you handle it manually).
Quartz has a more flexible language of expressing occurences (when the tasks should be fired).
Consider the memory footprint. If your single tasks share nothing or little, then it might be better to run them from the operating system as a separate process. If they share a lot of information, it's better to have them as threads within one process.
Not quite sure how you could handle the clustering in the cron approach. Quartz might be used with Terracotta following the scaling out pattern (I haven't tried it, but I believe it's doable).
The plus for cron is that any sysadmin knows how to use it and it's documented in many places. If cron will do the job then it would really be the preferred solution.