Hi Raymond, thanks for pointing out that the Camel context needs to be started first. That definitely got me started. In order for the routes to load, they must have the Spring namespace. Here is the solution:
private void loadRoute(File scriptFile, OsgiDefaultCamelContext camelContext) { try { byte[] content = Files.readAllBytes(scriptFile.toPath()); Resource resource = ResourceHelper.fromBytes(scriptFile.getAbsolutePath(), content); XmlRoutesBuilderLoader xmlRoutesBuilderLoader = new XmlRoutesBuilderLoader(); RouteBuilder routeBuilder = xmlRoutesBuilderLoader.doLoadRouteBuilder(resource); routeBuilder.setResource(resource); routeBuilder.addRoutesToCamelContext(camelContext); } catch (Exception e) { throw new RuntimeException(e); } } <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <route id="xml-route" autoStartup="true"> <from uri="direct:start"/> <log message="Hello XML!"/> </route> </routes> Thanks Alex -----Ursprüngliche Nachricht----- Von: ski n [mailto:raymondmees...@gmail.com] Gesendet: Dienstag, 27. September 2022 08:15 An: users@camel.apache.org Betreff: Re: Load a route from Blueprint XML Hi Alex, I seem you are sending the message to the direct endpoint before the xml-route is loaded. Couple of things you may try: 1) Start CamelContext before loading routes 2) Load the camel xml-route with the Camel context (multiple routes can be loaded in the CamelContext, so you just add it to other routes). 3) I personally, when using the Karaf, just drop the files in the deploy folder as explained here: http://liquid-reality.de/Karaf-Tutorial/05/ Raymond On Mon, Sep 26, 2022 at 3:51 PM Domke, Alexander <a.do...@cenit.com.invalid> wrote: > Hello all, > > I am trying to load an external route from a blueprint XML file and it > is not working. Has anyone done this before and has a tip for me? I am > using camel 3.18.2 and Karaf 4.4.1. > > My Test Implementation: > > package testbundle; > > import java.io.File; > import java.nio.file.Files; > > import org.apache.camel.CamelContext; > import org.apache.camel.Exchange; > import org.apache.camel.ExtendedCamelContext; > import org.apache.camel.ProducerTemplate; > import org.apache.camel.blueprint.BlueprintCamelContext; > import org.apache.camel.spi.Resource; > import org.apache.camel.support.DefaultExchange; > import org.apache.camel.support.ResourceHelper; > import org.osgi.service.component.annotations.Component; > import org.osgi.service.component.annotations.Reference; > > @Component( > immediate = true, > service = TestBundle.class, > property = {"osgi.command.scope=test", > "osgi.command.function=loadRoutes"}) > public class TestBundle { > > public BlueprintCamelContext camelContext; > > @Reference(target = "(camel.context.name=dummyCamelContext)") > public void bindCamelContext(CamelContext camelContext) { > this.camelContext = (BlueprintCamelContext) camelContext; > } > > public void unbindCamelContext() { > camelContext = null; > } > > public void loadRoutes() throws Exception { > loadRoute("C:\\tmp\\before_create.xml", camelContext); > camelContext.start(); > Exchange exchange = new DefaultExchange(camelContext); > ProducerTemplate producerTemplate = > exchange.getContext().createProducerTemplate(); > Exchange resultExchange = producerTemplate.send("direct:start", > exchange); > System.out.println("resultExchange: " + resultExchange); > > if (resultExchange.getException() != null) { > throw resultExchange.getException(); > } > } > > private void loadRoute(String name, BlueprintCamelContext camelContext) { > ExtendedCamelContext ecc = > camelContext.adapt(ExtendedCamelContext.class); > try { > File file = new File(name); > byte[] content = Files.readAllBytes(file.toPath()); > Resource resource = > ResourceHelper.fromBytes(file.getAbsolutePath(), > content); > ecc.getRoutesLoader().loadRoutes(resource); > System.out.println("ecc routes: " + ecc.getRoutes()); > System.out.println("camelContext routes: " + > camelContext.getRouteDefinitions()); > > } catch (Exception e) { > throw new RuntimeException(e); > } > } > } > > My Blueprint with camel context: > > > <?xml version="1.0" encoding="UTF-8"?> > > <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi=" > http://www.w3.org/2001/XMLSchema-instance" xmlns:cm=" > http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0" xmlns:ext=" > http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0" xmlns:enc=" > http://karaf.apache.org/xmlns/jasypt/v1.0.0" xsi:schemaLocation=" > > http://www.osgi.org/xmlns/blueprint/v1.0.0 > http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> > > <camelContext xmlns="http://camel.apache.org/schema/blueprint" > id="dummyCamelContext"> > > <route id="create" autoStartup="true"> > > <from uri="direct:dummyStart" /> > > <to uri="mock:dummyEnd" /> > > </route> > > </camelContext> > > </blueprint> > > > My Route to load: > > > <?xml version="1.0" encoding="UTF-8"?> > > <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> > > <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns=" > http://camel.apache.org/schema/blueprint" xsi:schemaLocation=" > > http://camel.apache.org/schema/blueprint > > > http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> > > > > <route id="xml-route" autoStartup="true"> > > <from uri="direct:start" /> > > <log message="Hello XML!" /> > > </route> > > </routes> > > </blueprint> > > > Error executing command: No consumers available on endpoint: > direct://start. Exchange[] > > I am at a loss, do you have any idea what I am doing wrong? > > Greeting > Alex > > CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 > 7825-30, > Fax: +49 711 7825-4000, Internet: www.cenit.com Geschaeftsstellen > (Branch Offices): Berlin, Frankfurt, Hamburg, Hannover, Muenchen, > Oelsnitz, Ratingen, Saarbruecken Vorstandsmitglieder (Members of the > Board): Peter Schneck, Dr. Markus Wesel Aufsichtsratsmitglieder > (Supervisory Board Members): Rainer Koppitz (Vorsitzender des > Aufsichtsrats / Chairman of the Supervisory Board), Prof. > Dr. Isabell Welpe, Ricardo Malta > Bankverbindungen (Bank Accounts): > Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 > 0166 > 1040 00 SWIFT-CODE : DEUTDESS, > Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 > 0532 > 0155 00 SWIFT-Code : COBADEFF600, > Registergericht (Registry court): Amtsgericht Stuttgart > Handelsregister (Commercial Register): HRB Nr. 19117 Umsatzsteuer > (VAT) ID: DE 147 862 777 > CENIT AG, Industriestrasse 52-54, 70565 Stuttgart, Tel.: +49 711 7825-30, Fax: +49 711 7825-4000, Internet: www.cenit.com Geschaeftsstellen (Branch Offices): Berlin, Frankfurt, Hamburg, Hannover, Muenchen, Oelsnitz, Ratingen, Saarbruecken Vorstandsmitglieder (Members of the Board): Peter Schneck, Dr. Markus Wesel Aufsichtsratsmitglieder (Supervisory Board Members): Rainer Koppitz (Vorsitzender des Aufsichtsrats / Chairman of the Supervisory Board), Prof. Dr. Isabell Welpe, Ricardo Malta Bankverbindungen (Bank Accounts): Deutsche Bank (BLZ 600 700 70) Kto. 1661 040 IBAN : DE85 6007 0070 0166 1040 00 SWIFT-CODE : DEUTDESS, Commerzbank (BLZ 600 400 71) Kto. 532 015 500 IBAN : DE83 6004 0071 0532 0155 00 SWIFT-Code : COBADEFF600, Registergericht (Registry court): Amtsgericht Stuttgart Handelsregister (Commercial Register): HRB Nr. 19117 Umsatzsteuer (VAT) ID: DE 147 862 777