我正在做一個專案,我使用駱駝路線(Camel-sql)從 MariaDB 資料庫中獲取資料。我在我的藍圖檔案中使用 apache commons dbcp2 作為資料源。當我使用 mvn:camel-run 在開發中運行專案時,它作業得非常好。
當我嘗試在 Karaf 上部署這個專案時,問題就開始了。mvn:install 使用正確形成的藍圖檔案和清單檔案生成所需的 jar。我將它復制到 Karaf 的部署檔案夾中。
捆綁包安裝成功。但它沒有啟動。我收到一個例外,說捆綁包無法檢測到 MariaDB java 客戶端。這對我來說沒有意義,因為我在 Karaf 上安裝了它(使用 bundle:install -s mvn:...)。我為所有其他依賴項(如 fastxml、dbcp2 等)執行了此操作。
我的要求很嚴格:我需要能夠在我的藍圖檔案中定義資料源。我找到了一些關于使用 pax-jdbc 的解決方案,但到目前為止還沒有弄清楚。在下面附上所有相關提示。
例外跟蹤
17:39:01.404 WARN [Camel (dbOps-context-g3) thread #10 - timer://foo] Error processing exchange. Exchange[AA8CB8C488B457B-0000000000000005]. Caused by: [org.springframework.jdbc.CannotGetJdbcConnectionException - Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'org.mariadb.jdbc.Driver']
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'org.mariadb.jdbc.Driver'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:83) ~[!/:?]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:646) ~[!/:?]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:685) ~[!/:?]
at org.apache.camel.component.sql.SqlProducer.processInternal(SqlProducer.java:145) ~[!/:3.19.0]
at org.apache.camel.component.sql.SqlProducer.process(SqlProducer.java:132) ~[!/:3.19.0]
at org.apache.camel.support.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:66) ~[?:?]
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:172) ~[?:?]
at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:477) ~[?:?]
at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:181) ~[?:?]
at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:59) ~[?:?]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:175) ~[?:?]
at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:392) ~[?:?]
at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:210) ~[?:?]
at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76) ~[?:?]
at java.util.TimerThread.mainLoop(Timer.java:556) ~[?:?]
at java.util.TimerThread.run(Timer.java:506) ~[?:?]
Caused by: java.sql.SQLException: Cannot load JDBC driver class 'org.mariadb.jdbc.Driver'
at org.apache.commons.dbcp2.DriverFactory.createDriver(DriverFactory.java:54) ~[!/:2.9.0]
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:459) ~[!/:2.9.0]
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:525) ~[!/:2.9.0]
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:731) ~[!/:2.9.0]
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:159) ~[!/:?]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:117) ~[!/:?]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[!/:?]
... 15 more
Karaf 捆綁包串列
karaf@root()> bundle:list
START LEVEL 100 , List Threshold: 50
ID │ State │ Lvl │ Version │ Name
────┼──────────┼─────┼────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
32 │ Active │ 80 │ 4.4.1 │ Apache Karaf :: OSGi Services :: Event
59 │ Active │ 50 │ 3.19.0 │ camel-api
60 │ Active │ 50 │ 3.19.0 │ camel-base
61 │ Active │ 50 │ 3.19.0 │ camel-base-engine
62 │ Active │ 50 │ 3.19.0 │ camel-bean
63 │ Active │ 50 │ 3.19.0 │ camel-browse
64 │ Active │ 50 │ 3.19.0 │ camel-cloud
65 │ Active │ 50 │ 3.19.0 │ camel-cluster
66 │ Active │ 50 │ 3.19.0 │ camel-console
67 │ Active │ 50 │ 3.19.0 │ camel-controlbus
68 │ Active │ 50 │ 3.19.0 │ camel-core-catalog
69 │ Active │ 50 │ 3.19.0 │ camel-core-engine
70 │ Active │ 50 │ 3.19.0 │ camel-core-languages
71 │ Active │ 50 │ 3.19.0 │ camel-core-model
72 │ Active │ 50 │ 3.19.0 │ camel-core-processor
73 │ Active │ 50 │ 3.19.0 │ camel-core-reifier
74 │ Active │ 50 │ 3.19.0 │ camel-core-xml
75 │ Active │ 50 │ 3.19.0 │ camel-dataformat
76 │ Active │ 50 │ 3.19.0 │ camel-dataset
77 │ Active │ 50 │ 3.19.0 │ camel-direct
78 │ Active │ 50 │ 3.19.0 │ camel-directvm
79 │ Active │ 50 │ 3.19.0 │ camel-file
80 │ Active │ 50 │ 3.19.0 │ camel-health
81 │ Active │ 50 │ 3.19.0 │ camel-language
82 │ Active │ 50 │ 3.19.0 │ camel-log
83 │ Active │ 50 │ 3.19.0 │ camel-main
84 │ Active │ 50 │ 3.19.0 │ camel-management
85 │ Active │ 50 │ 3.19.0 │ camel-management-api
86 │ Active │ 50 │ 3.19.0 │ camel-mock
87 │ Active │ 50 │ 3.19.0 │ camel-ref
88 │ Active │ 50 │ 3.19.0 │ camel-rest
89 │ Active │ 50 │ 3.19.0 │ camel-saga
90 │ Active │ 50 │ 3.19.0 │ camel-scheduler
91 │ Active │ 50 │ 3.19.0 │ camel-seda
92 │ Active │ 50 │ 3.19.0 │ camel-stub
93 │ Active │ 50 │ 3.19.0 │ camel-support
94 │ Active │ 50 │ 3.19.0 │ camel-timer
95 │ Active │ 50 │ 3.19.0 │ camel-tooling-model
96 │ Active │ 50 │ 3.19.0 │ camel-util
97 │ Active │ 50 │ 3.19.0 │ camel-util-json
98 │ Active │ 50 │ 3.19.0 │ camel-validator
99 │ Active │ 50 │ 3.19.0 │ camel-vm
100 │ Active │ 50 │ 3.19.0 │ camel-xml-io-util
101 │ Active │ 50 │ 3.19.0 │ camel-xml-jaxb
102 │ Active │ 50 │ 3.19.0 │ camel-xml-jaxp
103 │ Active │ 50 │ 3.19.0 │ camel-xpath
104 │ Active │ 50 │ 3.19.0 │ camel-xslt
105 │ Active │ 50 │ 3.19.0 │ camel-blueprint
106 │ Active │ 80 │ 3.19.0 │ camel-commands-core
107 │ Active │ 50 │ 3.19.0 │ camel-core-osgi
108 │ Active │ 80 │ 3.19.0 │ camel-karaf-commands
120 │ Active │ 50 │ 3.1.1.SNAPSHOT │ com.github.ben-manes.caffeine
121 │ Active │ 50 │ 3.19.0 │ camel-sql
146 │ Active │ 80 │ 2.13.0 │ Jackson-annotations
147 │ Active │ 80 │ 2.13.0 │ Jackson-core
148 │ Active │ 80 │ 2.13.0 │ jackson-databind
155 │ Active │ 80 │ 2.9.0 │ Apache Commons DBCP
156 │ Active │ 80 │ 2.9.0 │ Apache Commons Pool
159 │ Active │ 50 │ 3.19.0 │ camel-paho
160 │ Active │ 50 │ 1.2.5 │ org.eclipse.paho.client.mqttv3
177 │ Resolved │ 80 │ 1.0.0.SNAPSHOT │ camel-jdbc-to-mqtt
blueprint.xml 中的資料源部分
<bean id="dataSourceBean" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
<property name="url" value="jdbc:mariadb://localhost:3306/test-db"/>
<property name="username" value="demouser"/>
<property name="password" value="1demo1"/>
</bean>
來自 POM 的依賴項和插件
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.19.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.camel.karaf</groupId>
<artifactId>camel-karaf-bom</artifactId>
<version>3.19.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- DB connectivity -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-sql</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.karaf</groupId>
<artifactId>camel-blueprint</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.karaf</groupId>
<artifactId>camel-blueprint-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-paho</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.apache.camel.karaf</groupId>
<artifactId>camel-test-blueprint</artifactId>
<scope>test</scope>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
</dependency>
<!-- JSON processing -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- to generate the MANIFEST.MF of the bundle -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bundle-plugin</artifactId>
<version>3.19.0</version>
<extensions>false</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>prepare-package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- to include MANIFEST.MF in the bundle -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<!-- to run the example using mvn camel-karaf:run -->
<plugin>
<groupId>org.apache.camel.karaf</groupId>
<artifactId>camel-karaf-maven-plugin</artifactId>
<version>3.19.0</version>
</plugin>
</plugins>
</build>
uj5u.com熱心網友回復:
您需要在 karaf 中安裝驅動程式,您的路由無法啟動的原因是因為沒有找到您的資料源的驅動程式。
像這樣安裝驅動
bundle:install -s mvn:org.mariadb.jdbc/mariadb-java-client:3.0.8
然后將生成的捆綁包啟用為動態匯入,例如
bundle:dynamic-import ${bundleId}
可能有必要為您的路線啟用動態匯入。
然后重新啟動您的路線,它應該可以作業!
希望這可以幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/523068.html
