Java cron4j scheduled files - java

I am using the cron4j library for scheduling a program. Here is my code:
public class Main {
public static void main(String[] args) {
// Declares the file.
File file = new File("cron4j.txt");
// Creates the scheduler.
Scheduler scheduler = new Scheduler();
// Schedules the file.
scheduler.scheduleFile(file);
// Starts the scheduler.
scheduler.start();
// Stays alive for five minutes.
try {
Thread.sleep(5L * 60L * 1000L);
} catch (InterruptedException e) {
;
}
// Stops the scheduler.
scheduler.stop();
}
}
Inside the "cron4j.txt" file I have set my program to run every minute.
Must this file (class Main) with the object scheduler be running in order for the program in the file to be executed every minute?
Or once I run this once will the scheduler pass off this job to the operating system?

The program must be continuously running. Cron4j is just hiding the scheduling for you but in reality is a bunch of threads sleeping and waiting for the time to come for execution. The operating system just sees your program as a normal one continuously running.
In order to use the operating system's scheduling mechanisms, you do not use Cron4j but use crontab (on linux) or the task scheduler on Windows.
One more sophisticated scheduler for Java, which is more considered the Industry standard is Quartz Scheduler. However the concept is the same, your program needs to be running for the scheduled tasks to happen.

Related

How can I build a cron-like job executor in Java?

I'd like to develop a Java program that executes tasks registered in a database. The tasks have their own cron-like schedule, which is an object of CronExpression of Quartz Scheduler, and saved in the database after being serialized.
Tasks should be executed anytime according to its schedule, so I think the program should be daemonized, and may be able to be restarted or stopped outside the program (like an usual service beneath /etc/init.d/)
I'm studying the examples of Quartz
and saw the program running continuously even if there's no sleep and shutdown method. This seems nice to achieve my purpose, but I'm not sure if this way can generate a daemon process.
// TODO: Retrieve cron format from the database
Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("* * * ? * MON-FRI"))
.startNow()
.build();
try {
sched.scheduleJob(job, trigger);
sched.start();
// Thread.sleep(90L * 1000L);
// sched.shutdown(true);
} catch (SchedulerException e) {
...
My question is
What is the best way to build a cron job scheduler which runs continuously on a server?
Thank you in advance, and any opinions or questions would be appreciated.

ThreadPool Executor not executing threads in GAE

I am trying to use executor framework in Google App engine. Bellow is the code that I am trying to run.
Thread thread = ThreadManager.createBackgroundThread(new Runnable(){
public void run(){
try{
LOGGER.info( "Checking background thread");
Thread.sleep(10);
}
catch (InterruptedException ex){
throw new RuntimeException("Exception:", ex);
}
}
});
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(10, ThreadManager.backgroundThreadFactory());
executor.scheduleAtFixedRate(thread, 0, 30, TimeUnit.MINUTES);
But this doesn't start the thread. But if I use thread.start() it works properly. I have checked Whitelisted Classes and it does provide Executor classes. So where I am doing it wrong ?
Saikat,
You should always try to avoid creating threads on App Engine, because of it's distributed and dynamic nature it tends to have really bad/unexpected results.
In your case multiple instances will spawn multiple (local) threads sending many times the same notification. Also, bear in mind GAE front end instances have a 1 minute request limit, so after that time the server will kill that request.
Fortunately App Engine provides the Cron service for exactly this situations.
The Cron Service will allow you to schedule a job to run at a given time or every given period. When the cron is triggered GAE will call a configured URL so you can do your process, in your case send notifications.
Eg:(from the link provided)
<cron>
<url>/weeklyreport</url>
<description>Mail out a weekly report</description>
<schedule>every monday 08:30</schedule>
<timezone>America/New_York</timezone>
</cron>
will make an HTTP request to /weeklyreport every monday #8:30.

64 bit Centos Java JVM unable to create a native thread

I am using the Java Executor Service to create a singlethread.
Code:-
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
executor.submit(new Runnable() {
#Override
public void run() {
Iterator<FileObject> itr = mysortedList.iterator();
while (itr.hasNext()) {
myWebFunction(itr.next();
}
};
}).get(Timeout * mysortedList.size() - 10, TimeUnit.SECONDS);
} catch (Exception ex) {
} finally {
executor.shutdownNow();
}
Details: myWebfunction processes files of different size and content.Processing involves extracting the entire content and applying further actions on the file content.
The program runs in 64bit Centos.
Problem: When the myWebfunction gets file of size greater than some threshold, say 10MB, the executor service is unable to create a native thread. I tried various -Xmx and -Xms settings, but still the executor service throws the same error.
My guess is you calling this many times, and you are not waiting for the thread which has timed out, leaving lots of threads lying around. When you run out of stack space, or you reach about 32K threads, you cannot create any more.
I suggest using a different approach which doesn't use so many threads or kills them off when you know you don't need them any more. E.g. have the while loop check for interrupts and call Future.cancel(true) to interrupt it.

How to schedule tasks without Quartz

Can anyone tell,advice how to run a scheduler without Quartz in java. I want to implement such features that if application server remain stop , my scheduler will run. So I thought the executable class to place outside of the war file. So can anyone give me suggestion ? Thanks.
Regarding scheduling tasks without Quartz, you can use Java's ScheduledExecutorService:
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
// Beeps every 10 seconds
scheduler.scheduleAtFixedRate(beeper, 0, 10, TimeUnit.SECONDS);
Now, regarding doing it when your application is not running, I see three options:
Develop a stand-alone application with your scheduled tasks, decoupled from your webapp
Look up for Application Server features for that purpose
OS scheduled tasks, as Linux's cron job

putting a timeout for each of executes in java.​util.​concurrent.ExecutorService

How can I create a timeout for each command that is running in parallel using java.​util.​concurrent.ExecutorService?
My code is something like this:
For example in the code below I need obj1 run for maximum 1 min, and obj2 for 2 mins and others 5 mins.
ExecutorService exService;
exService = Executors.newCachedThreadPool();
exService.execute(obj1);
exService.execute(obj2);
exService.execute(obj3);
exService.execute(obj4);
exService.shutdown();
boolean finshed = exService.awaitTermination(5, TimeUnit.MINUTES);
if (finshed) {
//Doing something
}
EDIT:
Unfortunately the class of obj1 - obj4 is scraping some web pages using WebHarvest that uses jakarta HttpClient for reading web pages and HttpClient (And neither WebHarvest itself) doesn't have any feature for timeout on entire page reading and/or scraping job.
This is my time consuming task and I thought about killing ExecutorService thread after a timeout to handle this problem.
In general, there is no reliable way to make a separate thread quit. In particular, there is no reliable way to interrupt and stop your task after a timeout from outside that task. What you need to do is make the tasks themselves responsible for stopping after their time runs out. Depending on what they do, you might be able to abstract this behaviour into a superclass something like:
public abstract class TimeoutRunnable implements Runnable {
private final long timeLimitMillis;
private long startTimeMillis;
public TimeoutRunnable(long timeLimitMillis) {
this.timeLimitMillis = timeLimitMillis;
}
public final void run() {
startTimeMillis = System.currentTimeMillis();
while (System.currentTimeMillis() - startTimeMillis < timeLimitMillis) {
runIteration();
}
}
protected abstract void runIteration();
}
Then in your subclass override, runIteration() and perform a single "step" of the task.
The only reasonably reliable way to kill a task is to run it in a separate process and kill that process if it times out. Using any other approach with a library which does not support timeouts is likely to be error prone at best.
from my point of view I think that such stuff requires some more robust foundations than plain Java standard classes , that 's why I would suggest to use any scheduler infrastructure (Quartz or any other project) which may gives you handles (job identifiers) to kill your time consuming tasks .
You may have something like this :
Main Thread launches the Quartz Scheduler , receive subscriptions from different jobs
saying : Job1,Job 2 and TimeCheckerJob
TimeCheckerJob would be a forever job ,notified to any new job and would check for living time for each any new job... In this job you would have to deal with start time of each job, beware of the OS clocks and don't try to setup too hard constraints (nanoseconds is pure fiction).
HTH
My 2 cents
Jerome

Categories

Resources