java Spring访问定时任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25017691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing scheduled tasks in Spring
提问by achingfingers
I have a couple of tasks scheduled within Spring's task scheduler:
我在Spring 的任务调度程序中安排了几个任务:
<task:scheduled-tasks>
<task:scheduled ref="task1" method="run"
cron="0 0 */0 * * *" />
<task:scheduled ref="task2" method="run"
cron="0 0 */30 * * *" />
</task:scheduled-tasks>
<task:scheduler id="scheduler" pool-size="10" />
How can I access a list of scheduled tasksand retrieve meta-information(e.g the next execution time) from within the application context?
如何访问计划任务列表并从应用程序上下文中检索元信息(例如下一次执行时间)?
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler)context.getBean("scheduler");
//... how to continue from here?
采纳答案by Sotirios Delimanolis
There is no public API in Spring to do this.
Spring 中没有公共 API 来执行此操作。
Related:
有关的:
回答by Randy Poznan
I just figured this out
我只是想通了这一点
start with this to get whats scheduled.
以此开始以获得预定的内容。
ThreadPoolTaskScheduler xScheduler = (ThreadPoolTaskScheduler)this.taskScheduler;
ScheduledThreadPoolExecutor xService = (ScheduledThreadPoolExecutor)xScheduler.getScheduledExecutor();
BlockingQueue<Runnable> queue = xService.getQueue();
Object[] scheduledJobs = queue.toArray();
If this array look at instance in debugger to find what you need out.
如果此数组在调试器中查看实例以找到您需要的内容。
Then write reflection code like this to get at the hidden API's in Spring and Java. See the set Accessible this is only way to get at these private items. You might need to use different public classes to get at certain private fields, look at api docs and view source on these classes in eclipse.
然后编写这样的反射代码来获取 Spring 和 Java 中隐藏的 API。请参阅 Set Accessible 这是获取这些私人物品的唯一方法。您可能需要使用不同的公共类来获取某些私有字段,查看 api 文档并在 eclipse 中查看这些类的源代码。
Method delayM = obj.getClass().getDeclaredMethod("getDelay", TimeUnit.class);
delayM.setAccessible(true);
// delayM = obj.getClass().getDeclaredMethod("getDelay", TimeUnit.class);
Long delay = (Long)delayM.invoke(obj, new Object[] { tu } );
The trigger and root runnable is in the callable field of this object , instance of ReschedulingRunnable which is not a public class, ask Spring why they did this. You can get the delegate out of DelegatingErrorHandlingRunnable with reflection.
触发器和根 runnable 在这个对象的 callable 字段中,ReschedulingRunnable 的实例不是公共类,请问 Spring 他们为什么这样做。您可以通过反射从 DelegatingErrorHandlingRunnable 中获取委托。