Skip to main content

Red Hat JBoss Fuse - Tips, Driver not found in JBoss Fuse


I have bumped into ClassNotFound Exception problem several times when I need to add Database driver or migrate non-OSGi libraries into my JBoss Fuse applications.


Could not load driverClass org.postgresql.Driver


java.lang.ClassNotFoundException: org.postgresql.Driver not found by org.apache.servicemix.bundles.c3p0 [135]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)[:1.7.0_60-ea]
at java.lang.Class.forName0(Native Method)[:1.7.0_60-ea]
at java.lang.Class.forName(Class.java:190)[:1.7.0_60-ea]

The problem is mostly due to the class loading algorithm in OSGi. As each module need to specified what package it needs setting these packages in "Import-Package" inside META-INF/MANIFEST.MF of the bundle. But sometime your application load the driver dynamically in runtime, or the 3rd party library did not specified the import in their settings.

What I normally do when I am in the middle of development is to temporary resolve this problem in the JBoss Fuse Karaf container, for instance this example, I am using the c3p0 pool in servicemix, but it needs to dynamically lookup the postgresql database. What I will do is to install both servicemix c3p0 bundle and postgresql bundle

osgi:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.c3p0/0.9.1.2_1

Bundle ID: 148

osgi:install wrap:mvn:postgresql/postgresql/9.0-801.jdbc4

Bundle ID: 148

then do a osgi:list to list all available bundles, make sure they are all active

osgi:list
[ 100] [Active     ] [            ] [   80] Fabric8 :: Web (1.2.0.redhat-133)
[ 102] [Active     ] [            ] [   80] Bean Validation API (1.1.0.Final)
[ 123] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: jackson-module-scala_2.10 (2.1.5.2)
[ 124] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: javassist (3.12.1.GA_3)
[ 127] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: json4s (3.2.4.1)
[ 128] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: reflections (0.9.8.1)
[ 129] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-annotations (1.3.2.1)
[ 130] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-core_2.10 (1.3.2.1)
[ 131] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-jaxrs_2.10 (1.3.2.3)
[ 145] [Resolved   ] [            ] [   80] Scala Standard Library (2.10.4.v20140209-180020-VFINAL-b66a39653b)
[ 148] [Installed  ] [            ] [   80] Apache ServiceMix :: Bundles :: c3p0 (0.9.1.2_1)
[ 149] [Installed  ] [            ] [   80] wrap_mvn_postgresql_postgresql_9.0-801.jdbc4 (0)

and then use dynamical loading for servicemix c3p0 to look up the postgresql

dev:dynamic-import 148
Enabling dynamic imports on bundle org.apache.servicemix.bundles.c3p0 [148]

But this is not a good idea when you want to package the application, I used the dynamical import when developing because I am the only person running the container, and the dependency may change rapidly when I am developing, but this is not the best way to do in OSGi, instead of a strict modularized system, it will search the public exported packages to find a match instead of the careful normal calculation. Not a good practice. That's when I want to deploy my application into production, is I will create a fragment bundle. There is no big secret about this  "fragment bundle", all you have to do is to specify a Fragment Host. And all the content in your fragment bundle will be made available to that host. From my example, all I have to do it to create an empty bundle, and have my servicemix c3p0 bundle as the fragment host and import postgresql host.

Fragment-Host: org.apache.servicemix.bundles.c3p0
Import-Package: org.postgresql 

To be more specific, create a JBoss Fuse blueprint project, remove all the unnecessary blueprint.xml or java classes (Basically an empty project), Then in your pom.xml, find the maven plugin, add your fragmented Host and package you need in Import-package tag.

      <!-- to generate the MANIFEST-FILE of the bundle -->
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.7</version>
        <extensions>true</extensions>
        <executions>
          <execution>
           <id>bundle-manifest</id>
           <phase>process-classes</phase>
           <goals>
               <goal>manifest</goal>
           </goals>
          </execution>
        </executions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>demojobfrag</Bundle-SymbolicName>
            <Private-Package>org.blogdemo.demojobfrag.*</Private-Package>
            <Fragment-Host>org.apache.servicemix.bundles.c3p0</Fragment-Host>
            <Import-Package>org.postgresql,*</Import-Package>
          </instructions>
        </configuration>

      </plugin>

And then deploy this bundle first along with your application. This is my MANIFEST.MF look like after packaging it.                

Manifest-Version: 1.0
Bnd-LastModified: 1441500854071
Build-Jdk: 1.7.0_60-ea
Built-By: christina
Bundle-ManifestVersion: 2
Bundle-Name: Camel Blueprint Route
Bundle-SymbolicName: demojobfrag
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: org.blogdemo.demojobfrag;version="1.0.0.SNAPSHOT"
Fragment-Host: org.apache.servicemix.bundles.c3p0
Import-Package: org.osgi.service.blueprint;version="[1.0.0,2.0.0)",org.p
 ostgresql
Tool: Bnd-1.50.0

The problem should go away if you done it correctly.

Comments

Jader said…
This comment has been removed by the author.
Jader said…
Thank you!!! After a long time trying to use c3p0 on Jboss fuse, finally I found your post. It's very useful! Thank you again!
Danny Suarez said…
Hi cristina thanks for the help in jboss fuss!

i have a issue trying to deploy the home loan demo with postgresql in fuse 6.3.0 it throws me this error ClassNotFound org.osgi.service.jdbc.datasourcefactory
Lea said…
Hi cristina thanks for the help in jboss fuse!
Namphibian said…
This is really nice to read as we just added a PgSQL server to our integration space and deploying the driver is turning out a lot more tricky than even SQL server jTDS. I used DBCP though so will have to apply it to my case however the analysis is clear concise and very informative.

I love Karaf, Fuse, Camel but this class loading hell you can experience needs to be addressed on a fundamental level.

Popular posts from this blog

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

RHTE - Supercharge your integration services

Red Hat Tech Exchange has taken place in Vietnam, Ho Chi Minh city two weeks ago, it is a great event held by Red Hat in Asia Pacific Region. It is open to all Red Hat partners who are interested in learning what Red Hat is doing recently, see what the trend of the open source world, basically it is a great event to share your knowledge and experience, to meet other enthusiastic people.

I am very fortunate to talk in this great event, to talk about the things I have been working on and even discuss it with many. Also got lots of great ideas too. So here are the slide.

My first talk was with Thomas Qvarnström about how to handle large size data in JBoss Fuse and how JBoss Data Grid can help in the situation.

Here is the agenda of the talk, we will be talk about this in the up coming webinar on 24th Sept.

Integration often involves storing, retrieving, and transforming data. Using a traditional database in your integration is likely to becomes a bottleneck that is expensive and hard to …

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …