简体   繁体   中英

Karaf + Camel + Maven + Docker

I am new to Karaf and Camel and I'm trying to deploy custom camel routes (java) and I'm facing a lot of problems at the time of deploying the camel bundle (.jar) in the hot deploy directory.

What I got so far:

  • Apache Karaf 4.3.1 running in docker container
  • Bundle.jar with the java defined route

My idea is to have a /deploy directory mapped to the karaf container so any.jar that's added to that directory is deployed (or maybe build a new image for karaf).

When I tried to add my current bundle to the directory I got the following error message:

20:19:32.490 INFO [fileinstall-/opt/karaf/deploy] Installing bundle org.apache.karaf.examples.karaf-camel-example-java / 4.3.1
20:19:32.535 WARN [fileinstall-/opt/karaf/deploy] Error while starting bundle: file:/opt/karaf/deploy/karaf-camel-example-java-4.3.1.jar
org.osgi.framework.BundleException: Unable to resolve org.apache.karaf.examples.karaf-camel-example-java [111](R 111.0): missing requirement [org.apache.karaf.examples.karaf-camel-example-java [111](R 111.0)] osgi.wiring.package; (&(osgi.wiring.package=org.apache.camel)(version>=3.6.0)(!(version>=4.0.0))) Unresolved requirements: [[org.apache.karaf.examples.karaf-camel-example-java [111](R 111.0)] osgi.wiring.package; (&(osgi.wiring.package=org.apache.camel)(version>=3.6.0)(!(version>=4.0.0)))]
        at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4368) ~[?:?]
        at org.apache.felix.framework.Felix.startBundle(Felix.java:2281) ~[?:?]
        at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998) ~[?:?]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1260) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1233) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:520) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:365) [!/:3.6.8]
        at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:316) [!/:3.6.8]

I think this can be solve with a maven bundle "wrap" but I'm not sure if this is correct, and if so, how should I wrap the bundle?

Thank you for reading:D

A bit late but hope this helps someone as I've fiddled with this setup quite a bit for the past year while exploring OSGi, Karaf, Camel and Docker.

If you want to do local development with karaf you can actually map your maven repository to the container which can make installing bundles and features quite a bit easier.

Here's a docker-compose for karaf 4.2.11 but you can probably change it to 4.3.1 without any problems. (add:z on volumes if using SELinux)

version: "2.4"
services: 

  karaf-runtime: 
    container_name: karaf
    image: apache/karaf:4.2.11
    ports: 
      - 8101:8101
      - 8181:8181
      - 1098:1098      
    volumes: 
      - ./karaf/etc:/opt/apache-karaf/etc
      - ./karaf/deploy:/opt/apache-karaf/deploy
      - karaf-data:/opt/apache-karaf/data
      - ~/.m2:/root/.m2
      - karaf-history:/root/.karaf
    command: [ karaf, server ]

volumes: 
  karaf-data: 
  karaf-history: 

Just save it to a empty folder somewhere as docker-compose.yml and use following commands in the folder to start and stop

# Start
docker-compose up -d

# Stop
docker-compose down

Easy way to create bundles that can be installed to karaf is to use one of the official archetypes karaf-bundle-archetype or karaf-blueprint-archetype when creating the project. To use them in karaf you can just run mvn clean install on them to package them to local repository.

Then from karaf console you can use maven to install the bundle with command: bundle:install mvn:groupid/artifactId/version . When you make changes you can update the bundle from local repository with command bundle:update bundleId .

As for camel you can follow the official guide on how to add camel feature repository and the features you need.

But the steps are basically:

# Add camel feature repo
feature:repo-add camel <version>

# Install camel feature
feature:install camel

# List available camel features for install
feature:list | grep camel

# Install camel features you need
feature:install <feature-name>

For Java-DSL you can use OSGiDefaultCamelContext , DefaultCamelContext or the Camel Main class to run your routes. OSGiDefaultCamelContext is part of camel-core-osgi artifact which is included in camel-scr for camel versions 2.25.4 and older for camel 3 it's probably included in camel-blueprint feature, if not then you can just install the individual bundle.

You can start/stop your CamelContexts using OSGi blueprints, bundle activator class or declarative service annotations. There are other ways but these use the underlying OSGi framework.

When installing bundles the missing requirement usually tells you that package that the bundle depends on is missing from karaf which means you'll have to install bundle that exports the said package.

These messages are usually best read starting from the end:

(osgi.wiring.package=org.apache.camel)(version>=3.6.0)(!(version>=4.0.0)

Which means that your karaf installation doesn't have camel installed. OSGi bundles expect OSGi framework/runtime to satisfy their dependencies which is quite a bit different from say standalone SpringBoot projects.

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