我正在尝试构建一种创建计划作业的方法。这些作业调用一个 URL。
public synchronized void scheduleNewJob(int jobNr, long newRate) throws NoSuchMethodException {
ScheduledFuture job = jobsMap.get(jobNr);
if (job != null) {// job was already scheduled, we have to cancel
// it
job.cancel(true);
}
// reschedule the same method with a new rate
final String methodName = "callApi";
Method method = new ApiCallerHelper().getClass().getMethod(methodName, String.class);
job = taskScheduler
.scheduleAtFixedRate(new ScheduledMethodRunnable(targetClass, method), newRate);
为此,我需要将 URI 参数传递给我的方法 (callApi)。
有没有可能做到这一点?或者更好的方法?
最佳答案
根据 Spring documentation在 ScheduledMethodRunnable 上:
...meant to be used for processing of no-arg scheduled methods.
我想您可以使用参数化构造函数创建类包装器。用预定的方法包装一个类。并且从预定方法引用参数,而方法本身仍然是无参数和返回无效的。
关于java - 如何将参数传递给 ScheduledMethodRunnable 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44046921/