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

Red Hat Fuse - Announcing Fuse 7 Tech preview 3 release.

Red Hat Fuse 7.0 technical preview three is out today! On the pathway to become one of the best cloud-native integration platform, Fuse gives developer freedom to choose how they want to develop the integration solution, where they want to deploy it and capabilities to address new integration personas that do not have development experience.
By supporting the three major runtime, developer is free to work on the runtime of their choice.By supporting standalone and cloud deployment, it simplifies the complexity to distinguish between these environments, allowing application to deploy freely among the environment of your choice. All levels of developers are welcome, you can either dive deep into creating customize complex integration logic, or using the new low code platform to quickly build a simple integration. In this Tech Preview release you get it all.
Fuse StandaloneSpring-boot for microserviceKaraf 4 for OSGi loverJBoss EAP for JavaEE developersFuse on OpenShiftPlugins for easy co…

JBoss EAP 6 - 效能調校 (一) DataSource 的 Connection Pool

效能沒有什麼Best Practice, 反正能調整的就那些。 通常,一個程式的效能大概有70-80% 都跟程式怎麼寫的其實比較有關係。

最近我最疼愛的小貓Puji 因為膀胱結石開刀的時候過世了,心情很差請原諒我的口氣沒有很好,也沒有心情寫部落格。

Puji R.I.P.

=======================正文=======================

這個題目很多人叫我寫,可是這題目好大,這分明就是整死我咩~
所以我會分幾段慢慢寫。

JBoss 的 SubsystemDatasource WebWeb Service EJB Hibernate JMSJCAJVM 調校OS (作業系統)

先來看一下 DataSource Subsystem, DataSource 的部分主要是針對Connection Pool 做調校。

通常,程式都會需要跟資料庫界接,電腦在本機,尤其是在記憶體的運算很快,但是一旦要外部的資源連接,就是會非常的耗資源。所以現在的應用程式伺服器都會有個Pool 放一些先連接好的 資料庫connection,當程式有需要的時候就可以馬上提供,而不用花那些多餘的資源去連接資料庫。

這就是為什麼要針對Connection Pool 去做調校。

以下會討論到的參數,都是跟效能比較有關係,Datasource 還有很多參數,像是檢核connection 是否正確的,我都不會提到。如果你追求的是非常快速的效能,那我建議你一個檢核都不要加。當然,這樣就會為伺服器上面執行的程式帶來風險。這就是你要在效能與正確,安全性上面的取捨了。 (套句我朋友說的話,不可能又要馬兒好,又要馬兒不吃草的..)

最重要的調校參數就是 Connection 的 Pool 數量。(也就是那個Pool 裡面要放幾條的connection.) 這個參數是每一個應用程式都不一樣的。

min-pool-size 

Connection Pool 最少會存留的connection 數量

max-pool-size 

Connection Pool 最多可以開啓的 connection 數量

prefill

事先將connection pool 裡面建立好min-pool-size 的connection.

我的建議是觀察一下平常程式要用到的量設定為 min-pool-size 。
加上…

JBoss Fuse - Fuse workshop 101 - Part One

On my way to Hong Kong for a day of workshop on JBoss Fuse, and as I go through my Slide deck, I cannot find any decent easy workshop for beginners. Therefore I decide make a workshop that is easy for Camel first timer to get their hands dirty.

The first of part of the workshop is an introduction to Camel, it first goes through what is exactly inside JBoss Fuse.

For part one of the workshop, it takes your through the very basic of Camel, one of the very important component inside JBoss Fuse.
Every Camel need to have a runtime container to run in, inside camel we call it a CAMEL CONTEXT.  Inside every Camel context, you can define lots of camel route and registry, don't worry about what those are, we will explain later.


So inside out blueprint xml, you will see a tag called camelContext.



Next up is camel route, they are a chain of command or process defined by you, as a developer.
Inside the camel route, there are consumer endpoints that listens to the incoming messages, producers …