将参数数组传递给 Quartz Scheduler
Passing array of arguments to Quartz Scheduler
我正在尝试将 Quartz Scheduler 添加到现有 Java class。
class 使用 String[]
参数作为主函数的输入。
public static void main(String[] args) {
//validate(args);
summarizeData(args);
}
但是 Java class 实现了 Job
,它必须使用只接受一个参数的 execute
方法。
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// How to get the String[] args and pass it to this execute method?
// Then I can pass it to the next helper functions, etc.
// summarizeData(args);
}
有什么建议吗?
编辑 1:args 应该是 String
中的命令行参数
在这种情况下,根据传入的参数类型(以及您正在创建的 class),创建新的静态 class 变量并设置它们可能更有利在 main
内。如果您需要非静态变量,您可能需要修改 class 构造函数和 accessor/mutator (getter/setter) 方法来设置它们。
选项 I - 静态变量?
只有当变量在 class 的所有实例中都相同时才有效!!
此外,我不完全了解 Quartz 是如何工作的,所以请注意我在代码注释中的警告。
public class YourClass extends Job
{
// Your other class variables
// New static variables
private static String someVariable;
private static String someOtherVariable;
public static void main(String[] args) {
someVariable = args[0]; //<---- Set class variables
someOtherVariable = args[1];
// Do other stuff
}
// WARNING: If main isn't called (from any instance) before execute
// the class variables will still be null
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// Do stuff with someVariable and someOtherVariable
System.out.println(someVariable + " " + someOtherVariable);
}
}
选项 II - 非静态变量
根据您的实施,您可能希望在 main
之外设置变量,这样它们就不是静态的。在那种情况下,只需创建非静态 class 变量并修改构造函数以在创建对象时设置这些变量 (and/or 添加 accessor/mutator (getter/setter) 方法来修改初始化后的变量)。
因为我不知道你是如何使用它的class,我不知道上面两个选项中哪个最有效。
问题已解决。
我必须将命令行参数映射到调度程序 class 中的 JobDataMap
。然后在 .execute
方法中使用 getJobDataMap()
检索参数并将它们放回 String[]
.
您可以将对象添加到调度程序上下文中
scheduler.getContext().put("arg1", "value1"); //or loop through your args
然后检索它:
public void execute(JobExecutionContext arg0) throws JobExecutionException {
String arg1 = null;
try {
arg1 = (String)jobExecutionContext.getScheduler().getContext().get("arg1");
//Do something
} catch (SchedulerException e) {
//Error management
}
}
我正在尝试将 Quartz Scheduler 添加到现有 Java class。
class 使用 String[]
参数作为主函数的输入。
public static void main(String[] args) {
//validate(args);
summarizeData(args);
}
但是 Java class 实现了 Job
,它必须使用只接受一个参数的 execute
方法。
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// How to get the String[] args and pass it to this execute method?
// Then I can pass it to the next helper functions, etc.
// summarizeData(args);
}
有什么建议吗?
编辑 1:args 应该是 String
在这种情况下,根据传入的参数类型(以及您正在创建的 class),创建新的静态 class 变量并设置它们可能更有利在 main
内。如果您需要非静态变量,您可能需要修改 class 构造函数和 accessor/mutator (getter/setter) 方法来设置它们。
选项 I - 静态变量?
只有当变量在 class 的所有实例中都相同时才有效!!
此外,我不完全了解 Quartz 是如何工作的,所以请注意我在代码注释中的警告。
public class YourClass extends Job
{
// Your other class variables
// New static variables
private static String someVariable;
private static String someOtherVariable;
public static void main(String[] args) {
someVariable = args[0]; //<---- Set class variables
someOtherVariable = args[1];
// Do other stuff
}
// WARNING: If main isn't called (from any instance) before execute
// the class variables will still be null
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// Do stuff with someVariable and someOtherVariable
System.out.println(someVariable + " " + someOtherVariable);
}
}
选项 II - 非静态变量
根据您的实施,您可能希望在 main
之外设置变量,这样它们就不是静态的。在那种情况下,只需创建非静态 class 变量并修改构造函数以在创建对象时设置这些变量 (and/or 添加 accessor/mutator (getter/setter) 方法来修改初始化后的变量)。
因为我不知道你是如何使用它的class,我不知道上面两个选项中哪个最有效。
问题已解决。
我必须将命令行参数映射到调度程序 class 中的 JobDataMap
。然后在 .execute
方法中使用 getJobDataMap()
检索参数并将它们放回 String[]
.
您可以将对象添加到调度程序上下文中
scheduler.getContext().put("arg1", "value1"); //or loop through your args
然后检索它:
public void execute(JobExecutionContext arg0) throws JobExecutionException {
String arg1 = null;
try {
arg1 = (String)jobExecutionContext.getScheduler().getContext().get("arg1");
//Do something
} catch (SchedulerException e) {
//Error management
}
}