简体   繁体   中英

java mysql application web

I'm currently trying to create a very basic job scheduler. I'm trying to create an app that will check the current local system time. If the time is 12 am, it will download from a link "http://javadl.sun.com/webapps/download/AutoDL?BundleId=58134" to my desktop. Does anyone has a clue to do this with Java? Currently, I'm only able to check current local system time.

package task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.net.URL;


class MyTask extends TimerTask {
 public static void main(String[] args)  {
 Timer timer = new Timer();
 timer.schedule(new MyTask(), 60 * 1000);

 public void run() {
   Date date = new Date();
   if(date.compareTo(midnight) == 0) {
  //Download code
   URL google = new URL("http://www.google.it");
   ReadableByteChannel rbc = Channels.newChannel(google.openStream());
   FileOutputStream fos = new FileOutputStream("google.html");
   fos.getChannel().transferFrom(rbc, 0, 1 << 24);
   }
 }
}
}

You can implement for example a Timer that fire every 60 seconds and if the time is 12 AM download the file .

    Timer timer = new Timer();
    timer.schedule(new MyTask(), 60 * 1000);


class MyTask extends TimerTask {
    public void run() {
       Date date = new Date();
       if(date.compareTo(midnight) == 0) {
      //Download code
     }
    }

Fully working example to get you going:

import java.util.Calendar;

public class BasicScheduler {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Calendar rightNow = Calendar.getInstance();
            Integer hour = rightNow.get(Calendar.HOUR_OF_DAY);

            if (hour==12) {
                // Do your thing
                // ...



                // Sleep for a long time, or somehow prevent two downloads this time
                // ...
                Thread.sleep(60*1000+1);
            }
            else {
                Thread.sleep(1000);
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM