JBoss 7.3部署Camel服务遇SpringCamelContext类型转换异常求助
解决JBoss 7.3上Camel类转换异常问题
问题场景
在JBoss 7.3上基于Wildfly Camel实现Web服务,依赖配置如下:
<dependency> <groupId>org.wildfly.camel</groupId> <artifactId>wildfly-camel-subsystem-core</artifactId> <version>12.0.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>3.18.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>3.18.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>3.18.0</version> <scope>provided</scope> </dependency>
CamelContext配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <bean class="MyBean" id="helloBean"/> <route id="_route1"> <from id="_from1" uri="direct:start"/> <convertBodyTo type="java.lang.String"/> <bean method="sayHi(*)" ref="helloBean"/> <log message="After Bean: ${body}" loggingLevel="INFO"/> </route> </beans>
调用服务时抛出异常:
class org.apache.camel.spring.SpringCamelContext cannot be cast to class org.apache.camel.ExtendedCamelContext (org.apache.camel.spring.SpringCamelContext is in unnamed module of loader 'org.apache.camel.spring@2.23.2.fuse-780036-redhat-00001')
原因分析
异常信息里的org.apache.camel.spring@2.23.2.fuse-780036-redhat-00001表明:JBoss 7.3集成的Wildfly Camel子系统自带了旧版本(2.23.2)的Camel Spring模块,而项目中手动引入了3.18.0版本的Camel Core。两个版本的Camel类被不同类加载器加载,导致类型转换失败——同一个接口/类在不同类加载器下属于不同类型,无法互相转换。
解决方案
1. 对齐Wildfly Camel与Camel核心版本
Wildfly Camel子系统对Camel版本有严格的兼容性要求,不同版本的Wildfly Camel对应固定的Camel核心版本:
- Wildfly Camel 12.0.0 对应 Camel 3.14.0(适配JBoss 7.3/Wildfly 23)
修改camel-core的版本为与Wildfly Camel匹配的3.14.0,同时保持camel-spring、camel-cxf的scope=provided,让服务器提供这些依赖:
<dependency> <groupId>org.wildfly.camel</groupId> <artifactId>wildfly-camel-subsystem-core</artifactId> <version>12.0.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>3.14.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>3.14.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>3.14.0</version> <scope>provided</scope> </dependency>
2. 通过JBoss部署结构文件排除冲突模块
如果必须使用特定版本的Camel,可在项目的WEB-INF目录下创建jboss-deployment-structure.xml,排除服务器自带的Camel模块,强制使用项目打包的版本:
<jboss-deployment-structure> <deployment> <!-- 排除服务器自带的旧版本Camel模块 --> <exclusions> <module name="org.apache.camel.spring"/> <module name="org.apache.camel.core"/> <module name="org.apache.camel.cxf"/> </exclusions> <!-- 依赖Wildfly Camel子系统 --> <dependencies> <module name="org.wildfly.camel.subsystem" export="true"/> </dependencies> </deployment> </jboss-deployment-structure>
3. 检查Camel Spring Schema版本
确保CamelContext配置文件中的Spring Schema版本与使用的Camel版本匹配,将xsi:schemaLocation中的Camel Spring Schema地址更新为对应版本:
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-3.14.0.xsd"
内容的提问来源于stack exchange,提问作者TSSK
相关产品推荐
相关产品推荐

