简体   繁体   中英

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. This service can be referenced by some other bundles and fed with input data, that will be later on processed by the Route.

How would I do this?

Simply create an OSGi service outside of camel and a route that starts with direct:anyname. Then you can inject a ProducerTemplate into your service an call the route from there.

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.

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. For more information, please see 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. An example for Karaf can be found here: https://github.com/apache/karaf/tree/master/demos/command

Injecting a ProducerTemplate can be done via standard Spring configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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