Ben*_*fez 8 java cron service scheduled-tasks
我想在我的Java桌面应用程序上生成警报:
我正在使用Java,Eclipse,SWT进行开发,我的应用程序是使用Java Web Start从我的服务器部署的.我正在使用Mac OS X.6进行开发.
我想我有几个选择:
我真的不喜欢这个解决方案,我希望有更优雅的东西.
参考:我想在Mac OS/Windows上的System Startup上运行我的Java程序.我怎样才能做到这一点?
如果我将它作为系统服务运行,我可以从中受益,因为操作系统将确保我的软件:
我研究了一些我可以使用的资源:
我在系统服务选项中的问题是:
我的计划实施是否正确:
因此,在第一次运行时,应用程序将安装该服务并启动它.当应用程序关闭时,服务仍在运行,并且不再需要应用程序,除非它未注册.
但是,我想我仍然会错过"启动时运行"功能.
我对吗?我错过了什么吗?
在Unix上,我可以轻松使用cron表而无需应用程序将用户升级为root.我不需要处理重启,系统日期更改等.看起来不错.
在Windows上,我可以使用任务计划程序,即使在使用At或SchTasks的命令行中也是如此.这看起来不错,但是我需要这个兼容从XP到7,我不能轻易测试这个.
那你会怎么做?我错过了什么?您有什么建议可以帮助我选择最好,最优雅的解决方案吗?
这是我最终实现的:
public class AlarmManager {
public static final String ALARM_CLI_FORMAT = "startalarm:";
public static SupportedOS currentOS = SupportedOS.UNSUPPORTED_OS;
public enum SupportedOS {
UNSUPPORTED_OS,
MAC_OS,
WINDOWS,
}
public AlarmManager() {
final String osName = System.getProperty("os.name");
if (osName == null) {
L.e("Unable to retrieve OS!");
} else if ("Mac OS X".equals(osName)) {
currentOS = SupportedOS.MAC_OS;
} else if (osName.contains("Windows")) {
currentOS = SupportedOS.WINDOWS;
} else {
L.e("Unsupported OS: "+osName);
}
}
/**
* Windows only: name of the scheduled task
*/
private String getAlarmName(final long alarmId) {
return new StringBuilder("My_Alarm_").append(alarmId).toString();
}
/**
* Gets the command line to trigger an alarm
* @param alarmId
* @return
*/
private String getAlarmCommandLine(final long alarmId) {
return new StringBuilder("javaws -open ").append(ALARM_CLI_FORMAT).append(alarmId).append(" ").append(G.JNLP_URL).toString();
}
/**
* Adds an alarm to the system list of scheduled tasks
* @param when
*/
public void createAlarm(final Calendar when) {
// Create alarm
// ... stuff here
final long alarmId = 42;
// Schedule alarm
String[] commandLine;
Process child;
final String alarmCL = getAlarmCommandLine(alarmId);
try {
switch (currentOS) {
case MAC_OS:
final String cron = new SimpleDateFormat("mm HH d M '*' ").format(when.getTime()) + alarmCL;
commandLine = new String[] {
"/bin/sh", "-c",
"crontab -l | (cat; echo \"" + cron + "\") | crontab"
};
child = Runtime.getRuntime().exec(commandLine);
break;
case WINDOWS:
commandLine = new String[] {
"schtasks",
"/Create",
"/ST "+when.get(Calendar.HOUR_OF_DAY) + ":" + when.get(Calendar.MINUTE),
"/SC ONCE",
"/SD "+new SimpleDateFormat("dd/MM/yyyy").format(when.getTime()), // careful with locale here! dd/MM/yyyy or MM/dd/yyyy? I'm French! :)
"/TR \""+alarmCL+"\"",
"/TN \""+getAlarmName(alarmId)+"\"",
"/F",
};
L.d("create command: "+Util.join(commandLine, " "));
child = Runtime.getRuntime().exec(commandLine);
break;
}
} catch (final IOException e) {
L.e("Unable to schedule alarm #"+alarmId, e);
return;
}
L.i("Created alarm #"+alarmId);
}
/**
* Removes an alarm from the system list of scheduled tasks
* @param alarmId
*/
public void removeAlarm(final long alarmId) {
L.i("Removing alarm #"+alarmId);
String[] commandLine;
Process child;
try {
switch (currentOS) {
case MAC_OS:
commandLine = new String[] {
"/bin/sh", "-c",
"crontab -l | (grep -v \""+ALARM_CLI_FORMAT+"\") | crontab"
};
child = Runtime.getRuntime().exec(commandLine);
break;
case WINDOWS:
commandLine = new String[] {
"schtasks",
"/Delete",
"/TN \""+getAlarmName(alarmId)+"\"",
"/F",
};
child = Runtime.getRuntime().exec(commandLine);
break;
}
} catch (final IOException e) {
L.e("Unable to remove alarm #"+alarmId, e);
}
}
public void triggerAlarm(final long alarmId) {
// Do stuff
//...
L.i("Hi! I'm alarm #"+alarmId);
// Remove alarm
removeAlarm(alarmId);
}
}
Run Code Online (Sandbox Code Playgroud)
使用方法很简单。使用以下方法安排新警报:
final AlarmManager m = new AlarmManager();
final Calendar cal = new GregorianCalendar();
cal.add(Calendar.MINUTE, 1);
m.createAlarm(cal);
Run Code Online (Sandbox Code Playgroud)
像这样触发警报:
public static void main(final String[] args) {
if (args.length >= 2 && args[1] != null && args[1].contains(AlarmManager.ALARM_CLI_FORMAT)) {
try {
final long alarmId = Long.parseLong(args[1].replace(AlarmManager.ALARM_CLI_FORMAT, ""));
final AlarmManager m = new AlarmManager();
m.triggerAlarm(alarmId);
} catch (final NumberFormatException e) {
L.e("Unable to parse alarm !", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 Mac OS X.6 和 Windows Vista 上测试。该类L是一个帮助程序System.out.println并G保存我的全局常量(这里,我的服务器上的 JNLP 文件用于启动我的应用程序)。
| 归档时间: |
|
| 查看次数: |
3123 次 |
| 最近记录: |