问题描述
我正在使用 Camel & 开发一个 Java 项目.春天.我们想在 Spring 完成它的事情并且 Camel 完成所有路由的构建后,在单例 bean 上触发一个 initialize 方法.
I am working on a Java project using Camel & Spring. We would like to trigger an initialize method on a singleton bean after Spring finished doing its thing and Camel has finished building all routes.
我们不能在类创建时调用该方法,因为它动态链接到它从@Component spring 注释中获取的其他类,我们不知道这些类何时/是否已加载以实际运行 init 方法作为构造函数的一部分.
We cant call the method at class creation time as it has dynamic linkings to other classes that it picks up from the @Component spring annotation and we dont know when/if these classes have been loaded yet to actually run the init method as part of a constructor.
如何在 Camel 启动完成后立即调用一个或多个方法只运行一次?
How can I go about invoking a method or methods to run only once right after Camel startup is complete?
谢谢!
推荐答案
如果在 CamelContext 启动所有路由等之后必须调用 bean,那么您可以像 Ben 建议的那样使用带有计时器.
If the bean must be invoked after CamelContext has started all the routes etc, then you can as Ben suggest use a route with a timer.
一个可能更好的替代方法是使用 Camel 的 EventNotifier API.然后在被触发的 CamelContextStartedEvent 上调用逻辑.关于 EventNotifier API 的一些细节在这里:http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html
A possible better alternative is to use the EventNotifier API from Camel. And then invoke the logic on the CamelContextStartedEvent being fired.Some details on the EventNotifier API here: http://camel.apache.org/eventnotifier-to-log-details-about-all-sent-exchanges.html
这篇关于Apache Camel - 在启动时触发任务仅运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!