Skip to content

Instantly share code, notes, and snippets.

@afinlay5
Created June 26, 2018 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afinlay5/23fcd05353e2edd1dcaa89478ec994b8 to your computer and use it in GitHub Desktop.
Save afinlay5/23fcd05353e2edd1dcaa89478ec994b8 to your computer and use it in GitHub Desktop.
#!/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
Copy link
Author

12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment