[英]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. 如果时间是上午12点,它将从链接“ http://javadl.sun.com/webapps/download/AutoDL?BundleId=58134”下载到我的桌面。 Does anyone has a clue to do this with Java? 有谁有线索使用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 . 例如,您可以实现一个计时器 ,该计时器每60秒触发一次,如果时间是12 AM,则下载文件。
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.