#!/home/adrian/java/openjdk-11/bin/java --source 11 | |
//Simple CRON Job reimplemented using Java APIs! | |
import java.io.File; | |
import java.util.Date; | |
import java.util.Timer; | |
import java.util.Calendar; | |
import java.util.TimerTask; | |
import java.time.LocalDateTime; | |
public class Countdown { | |
private static class DeleteClassFiles extends TimerTask { | |
public void run () { | |
System.out.println(".....running imitation CRON JOB......"); | |
try { | |
//CRON JOB - Create File | |
if (new File("JavaIsTheBest.blah").isFile()) | |
System.out.println("File already exists."); | |
else | |
if (new File("JavaIsTheBest.blah").createNewFile()) | |
System.out.println("File successfully created."); | |
else | |
System.out.println("File was not successfully created"); | |
} catch (java.io.IOException ioe) { }; | |
System.out.println("Ending countdown for task......" + LocalDateTime.now()); | |
System.exit(0); | |
} | |
} | |
public static void main (String[] args) { | |
//It's a shame that Timer/TimerTask doesn't work with the Java8 DateTime APIs! | |
try { | |
//Grab Time, Add Time | |
var dt = Calendar.getInstance(); | |
dt.setTime(new Date()); | |
dt.add(Calendar.SECOND, Integer.valueOf(args[0])); | |
//Schedule job | |
var timer = new Timer(); | |
System.out.println("Beginning countdown for task......" + LocalDateTime.now()); | |
timer.schedule(new Countdown.DeleteClassFiles(), dt.getTime()); | |
} catch (NumberFormatException nfe) { System.out.println("Malformed input."); }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
afinlay5 commentedJun 26, 2018