Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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."); };
}
}
@afinlay5

This comment has been minimized.

Copy link
Owner Author

afinlay5 commented Jun 26, 2018

12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.