如何确保 Camel 的 ProducerTemplate 等待 CamelContext 启动

How to ensure that Camel's ProducerTemplate waits for CamelContext to start

我正在尝试使用 @EndpointInject 注释来创建 ProducerTemplate 以将我的 POJO 桥接至 CamelContext(如此处所述http://camel.apache.org/pojo-producing.html)。

我 运行 遇到的问题是,在启动 camel 上下文中的所有路由之前,ProducerTemplate 被注入到我的 POJO 中。所以当我调用 producerTemplate.send(...) 时,我得到一个 DirectConsumerNotAvailableException: No consumers available on endpoint... 错误。

我需要做些什么来确保在尝试发送到路由之前启动 CamelContext 吗?

您可以在直接端点上使用 block=true 作为选项,它会一直等到消费者处于活动状态并且 运行。这应该有所帮助。

否则您需要自己编写一些代码来等待 CamelContext 处于启动状态。您可以从具有 getCamelContext.

的注入 ProducerTemplate 访问它

另一种选择是使用依赖注入框架,如果可能的话,在 after Camel 之后设置你的 bean。如果您使用 spring xml,那么它具有 depends-on 属性,您可以在 <bean> 标签上设置。

我不明白克劳斯·易卜生的回答,问题是关于生产者而不是消费者。 我的建议是使用 spring 事件

@EventListener(ApplicationReadyEvent.class)
public void onApplicationEvent() {
    producerTemplate.sendBody("demo");

}