简体   繁体   English

将OSGi服务公开为Camel端点

[英]Expose an OSGi Service as Camel Endpoint

I don't even know if I formulated the question the right way around ;-) 我什至不知道我是否以正确的方式提出了这个问题;-)

What I basically want to achieve is something like this: 我基本上想要实现的是这样的:

<route >
    <from uri="osgi:serviceName"/>
    <!-- do some processing ->
    <to uri="activemq:queue:inbox"/>
</route>

So I'd like to have an OSGi Service as starting point of my route. 因此,我想将OSGi服务作为我的路线的起点。 This service can be referenced by some other bundles and fed with input data, that will be later on processed by the Route. 该服务可以被其他一些捆绑包引用,并提供输入数据,稍后将由Route处理。

How would I do this? 我该怎么做?

Simply create an OSGi service outside of camel and a route that starts with direct:anyname. 只需在骆驼之外创建OSGi服务,并创建一个以direct:anyname开头的路由。 Then you can inject a ProducerTemplate into your service an call the route from there. 然后,您可以将ProducerTemplate注入到服务中,从那里调用路由。

If you have really simple method signature, or typeConverter for the parameters you want to pass, you can use CamelProxy to link the service to your route in a simple XML configuration file. 如果您具有非常简单的方法签名,或者要传递的参数使用typeConverter ,则可以使用CamelProxy将服务链接到简单XML配置文件中的路由。

To extends the example of the doc, you would have something like : 为了扩展文档的示例,您将具有以下内容:

<osgi:service id="service" ref="myProxySender"                             (4)
      interface="org.apache.camel.spring.config.MyProxySender" />

<camelContext xmlns="http://camel.apache.org/schema/spring">

    <!-- create a proxy that will route to the direct:start endpoint when invoked -->
    <proxy id="myProxySender"
           serviceInterface="org.apache.camel.spring.config.MyProxySender"
           serviceUrl="direct:start"/>

    <!-- this is the route that our proxy will routed when invoked
         and the output from this route is returned as reply on the proxy -->
    <route>
        <from uri="direct:start"/>
        <transform>
            <simple>Bye ${body}</simple>
        </transform>
    </route>

</camelContext>

To use "osgi" as the URI scheme in a Camel route, you would need to create a custom Camel component to handle invoking the relevant OSGi commands. 要将“ osgi”用作Camel路由中的URI方案,您需要创建一个自定义的Camel组件来处理调用相关的OSGi命令。 For more information, please see http://camel.apache.org/creating-a-new-camel-component.html 有关更多信息,请参见http://camel.apache.org/creating-a-new-camel-component.html

A simpler alternative would be to write custom OSGi commands that used a ProducerTemplate to send messages to a Camel route. 一种更简单的选择是编写自定义OSGi命令,该命令使用ProducerTemplate将消息发送到Camel路由。 An example for Karaf can be found here: https://github.com/apache/karaf/tree/master/demos/command 可以在这里找到Karaf的示例: https : //github.com/apache/karaf/tree/master/demos/command

Injecting a ProducerTemplate can be done via standard Spring configuration. 可以通过标准的Spring配置来注入ProducerTemplate。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM