将 Spring bean 传递给 Camel 组件 [英] Passing a Spring bean to a Camel component

查看:17
本文介绍了将 Spring bean 传递给 Camel 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FooComponent 类型的自定义组件,它通过以下几行添加到路由中:

I have a custom component of type FooComponent which is added to the route by the following lines:

from("foo://bar?args=values&etc")
        .bean(DownstreamComponent.class)
        ...

FooComponent 创建一个端点和消费者(类型为 FooConsumer),然后发出消息到达 DownstreamComponent 和其余的管道.

FooComponent creates an endpoint and consumer (of type FooConsumer) which in turn emits messages which get to the DownstreamComponent and the rest of the pipeline.

为了监控,我需要 FooComponent 消费者调用非 Camel 对象的方法,我将其创建为 Spring bean.Camel 管道对性能非常敏感,因此我无法将 FooComponent 分成两半并将监视器调用作为 Camel 组件插入它们之间(我的首选解决方案,因为 FooComponent 不应该真的了解显示器).而且我不愿意将方法调用转换为 Camel 消息,稍后将在管道中被监控组件拾取,因为管道过滤稍后会变得复杂,我不想过多地干预它.

For monitoring, I need the FooComponent consumer to call a method on a non-Camel object, which I'm creating as a Spring bean. The Camel pipeline is very performance sensitive so I'm unable to divide the FooComponent into two halves and insert the monitor call as a Camel component between them (my preferred solution, since FooComponent shouldn't really have to know about the monitor). And I'm reluctant to turn the method call into a Camel Message that will be picked up by the monitoring component later in the pipeline, as the pipeline filtering becomes complicated later and I don't want to meddle with it more than necessary.

FooConsumer 的某个地方,我有:

Somewhere inside FooConsumer, I have:

// in the class
@Autowired
Monitor monitor;

// inside the consumer's run method
monitor.noticeSomething();

问题是 monitor 永远不会被设置为在应用程序的其余部分中创建的 Monitor bean.据我了解,这是因为 FooConsumer 本身对 Spring 不可见——该类型的对象通常在 FooComponent 内创建.

The problem is that monitor will never be set to the Monitor bean which is created in the rest of the application. As I understand it, it's because FooConsumer itself is not visible to Spring -- an object of that type is created normally inside FooComponent.

那么,如何让 FooComponent 找到它需要使用的 Monitor 实例?

So, how can I get FooComponent to find the Monitor instance that it needs to use?

  • 可以在创建路由的时候传入吗?这似乎很棘手,因为定义是一个虚假的 URL "foo://bar?args=values&etc";我还没有找到如何以这种方式传递 Java 对象.
  • 我可以让 Spring 在 FooConsumer 中找到那个 @Autowired 注释并以某种方式注入监视器对象吗?
  • Can I pass it in when the route is created? This seems tricky because the definition is a faux URL "foo://bar?args=values&etc"; I haven't found how to pass Java objects that way.
  • Can I get Spring to find that @Autowired annotation inside FooConsumer and inject the monitor object somehow?

推荐答案

如果你有一个 Monitor 的单例实例,你应该能够在 FooComponent 类中 @Autowire 它.因为 Camel 在创建 FooComponent 时会让 Spring 依赖注入.

If you have a singleton instance of Monitor you ought to be able to @Autowire it in the FooComponent class. As Camel will let Spring dependency inject when creating the FooComponent.

然后,当您从组件创建端点/消费者时,您可以传递监视器实例.

Then you can pass on the monitor instance when you create the endpoint / consumer from your component.

这篇关于将 Spring bean 传递给 Camel 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆