Karaf 无法在运行时加载 Afterburner 类

Karaf not able to load Afterburner classes during runtime

我正在尝试使用 Java 1.8 在 Karaf 3.0.5(默认 felix)中使用 AfterBurner。

在同一个代码库中,它在一个地方工作得很好,在一个地方却失败了。它工作的代码如下所示(并且在流程中首先被调用)

public static WFRequest fromJSON(String json, String targetClassName)
        throws ClassNotFoundException, IOException {
    WFRequest reqParam = null;
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new AfterburnerModule());
    reqParam = (WFRequest) mapper.readValue(json, Class.forName(targetClassName));
    return reqParam;
    }

失败的代码如下所示:-

@SuppressWarnings("unchecked")
    private static <T extends BaseCamelHTTPComponentResponse> T fromJSON(final JSONObject jsonObj,
        String serviceResponseClass) throws ClassNotFoundException, IOException {
    LOG.info("Response: {}, Marshalling it to an object of {}", jsonObj.toString(), serviceResponseClass);
    ObjectMapper objectMapper = new ObjectMapper();
    AfterburnerModule afterBurner = new AfterburnerModule();
    afterBurner.setUseOptimizedBeanDeserializer(false);
    objectMapper.registerModule(afterBurner);
    return (T) objectMapper.readValue(jsonObj.toString(), Class.forName(serviceResponseClass));
    }

在第二种情况下失败并出现以下错误:-

OrderException-GetOrdersHTTPFailure
com.fasterxml.jackson.databind.JsonMappingException: Failed to load class 'org.apache.camel.Message$Access4JacksonDeserializer98898f0f': com.fasterxml.jackson.module.afterburner.deser.BeanPropertyMutator
 at [Source: {"data":{"total":2,"order":[], "status":{"success":true}},"_metaData":{"urlParams":{}},"status":{"success":true,"errors":null,"httpStatusCode":200}}; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:290)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:444)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:948)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:446)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:296)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3908)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3803)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2797)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.jabong.orchestratorservice.basecomponent.http.JSONCamelHttpResponseHandler.marshallJSON(JSONCamelHttpResponseHandler.java:73)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.http.JSONCamelHttpResponseHandler.handleResponse(JSONCamelHttpResponseHandler.java:38)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.http.BaseCamelHTTPComponent.prepareResponse(BaseCamelHTTPComponent.java:133)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.http.BaseCamelHTTPComponent.call(BaseCamelHTTPComponent.java:88)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.node.HttpWFNode.executeRequest(HttpWFNode.java:130)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.node.HttpWFNode.executeWithRetry(HttpWFNode.java:118)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.node.HttpWFNode.executeWithRetry(HttpWFNode.java:49)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.node.HttpWFNode.execute(HttpWFNode.java:43)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.adapter.order.executor.GetOrdersWorkflowExecutor.execute(GetOrdersWorkflowExecutor.java:44)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.adapter.order.OrderHelper.getMarketplaceOrders(OrderHelper.java:53)[286:basecomponent:0.0.1]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_77]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_77]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_77]
    at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_77]
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.bean.MethodInfo.doProceed(MethodInfo.java:279)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.bean.MethodInfo.proceed(MethodInfo.java:252)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:177)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:68)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:38)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.DelayProcessorSupport.process(DelayProcessorSupport.java:160)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:68)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:412)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:380)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:270)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:380)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:238)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:132)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:149)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.DefaultProducerTemplate.requestBody(DefaultProducerTemplate.java:301)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.impl.DefaultProducerTemplate.requestBody(DefaultProducerTemplate.java:331)[136:org.apache.camel.camel-core:2.16.1]
    at com.jabong.orchestratorservice.basecomponent.processor.AbstractProcessor.makeRequest(AbstractProcessor.java:47)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.processor.JSONRPCProcessor.processRequest(JSONRPCProcessor.java:167)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.processor.JSONRPCProcessor.processRequestAndSendResponse(JSONRPCProcessor.java:132)[286:basecomponent:0.0.1]
    at com.jabong.orchestratorservice.basecomponent.processor.JSONRPCProcessor.process(JSONRPCProcessor.java:72)[286:basecomponent:0.0.1]
    at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)[136:org.apache.camel.camel-core:2.16.1]
    at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:170)[250:org.apache.camel.camel-jetty-common:2.16.1]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)[45:org.apache.geronimo.specs.geronimo-servlet_3.0_spec:1.0.0]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.Server.handle(Server.java:370)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:53)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:543)[50:org.eclipse.jetty.aggregate.jetty-all-server:8.1.17.v20150415]
    at java.lang.Thread.run(Thread.java:745)[:1.8.0_77]
Caused by: java.lang.IllegalArgumentException: Failed to load class 'org.apache.camel.Message$Access4JacksonDeserializer98898f0f': com.fasterxml.jackson.module.afterburner.deser.BeanPropertyMutator
    at com.fasterxml.jackson.module.afterburner.util.MyClassLoader.loadAndResolve(MyClassLoader.java:100)
    at com.fasterxml.jackson.module.afterburner.deser.PropertyMutatorCollector.generateMutatorClass(PropertyMutatorCollector.java:204)[278:com.fasterxml.jackson.module.jackson-module-afterburner:2.7.4]
    at com.fasterxml.jackson.module.afterburner.deser.PropertyMutatorCollector.buildMutator(PropertyMutatorCollector.java:102)[278:com.fasterxml.jackson.module.jackson-module-afterburner:2.7.4]
    at com.fasterxml.jackson.module.afterburner.deser.DeserializerModifier.updateBuilder(DeserializerModifier.java:62)[278:com.fasterxml.jackson.module.jackson-module-afterburner:2.7.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:239)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:143)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:406)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)[267:com.fasterxml.jackson.core.jackson-databind:2.7.4]
    ... 86 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.module.afterburner.deser.BeanPropertyMutator
    at java.lang.ClassLoader.findClass(ClassLoader.java:530)[:1.8.0_77]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)[:1.8.0_77]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_77]
    at java.lang.ClassLoader.defineClass1(Native Method)[:1.8.0_77]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)[:1.8.0_77]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:642)[:1.8.0_77]
    at com.fasterxml.jackson.module.afterburner.util.MyClassLoader.loadAndResolve(MyClassLoader.java:94)
    ... 94 more

捆绑包中的 MANIFEST.mf 没有问题。添加所有导入:-

Import-Package: com.fasterxml.jackson.core;resolut
 ion:=optional;version="[2.7,3)",com.fasterxml.jackson.databind;resoluti
 on:=optional;version="[2.7,3)",com.fasterxml.jackson.databind.annotatio
 n;resolution:=optional;version="[2.7,3)",com.fasterxml.jackson.module.a
 fterburner;resolution:=optional;version="[2.7,3)",org.json;reso
 lution:=optional,org.osgi.service.blueprint;version="[1.0.0,2.0.0)";res
 olution:=optional,com.fasterxml.jack
 son.databind.ser;version="[2.7,3)",com.fasterxml.jackson.databind.deser
 ;version="[2.7,3)",com.fasterxml.jackson.databind.ser.std;version="[2.7
 ,3)",com.fasterxml.jackson.databind.deser.std;version="[2.7,3)",com.fas
 terxml.jackson.module.afterburner.ser;version="[2.7,3)",com.fasterxml.j
 ackson.module.afterburner.deser;version="[2.7,3)"

...

即使在 Karaf 中执行 bundle:headers,我也看到所有的包都正确导入了

karaf@root>bundle:headers 286

basecomponent (286)
-------------------
Created-By = Apache Maven Bundle Plugin
Manifest-Version = 1.0
Bnd-LastModified = 1462969467812
Build-Jdk = 1.8.0_77
Built-By = debraj
Tool = Bnd-3.0.0.201509101326

Bundle-Blueprint = OSGI-INF/blueprint/authenticationBeans.xml,OSGI-INF/blueprint/awsBeans.xml,OSGI-INF/blueprint/blueprint.xml,OSGI-INF/blueprint/camelContext.xml,OSGI-INF/blueprint/catalogBeans.xml,OSGI-INF/blueprint/erpBeans.xml,OSGI-INF/blueprint/healthCheckBeans.xml,OSGI-INF/blueprint/orderBeans.xml,OSGI-INF/blueprint/productBeans.xml,OSGI-INF/blueprint/vendorBeans.xml
Bundle-ManifestVersion = 2
Bundle-SymbolicName = basecomponent
Bundle-Version = 0.0.1
Bundle-Name = basecomponent

Require-Capability = 
    osgi.ee;filter:=(&(osgi.ee=JavaSE)(version=1.8))


Import-Package = 
    com.fasterxml.jackson.annotation;resolution:=optional;version="[2.7,3)",
    com.fasterxml.jackson.core;resolution:=optional;version="[2.7,3)",
    com.fasterxml.jackson.databind;resolution:=optional;version="[2.7,3)",
    com.fasterxml.jackson.databind.annotation;resolution:=optional;version="[2.7,3)",
    com.fasterxml.jackson.module.afterburner;resolution:=optional;version="[2.7,3)",
    com.fasterxml.jackson.databind.ser;version="[2.7,3)",
    com.fasterxml.jackson.databind.deser;version="[2.7,3)",
    com.fasterxml.jackson.databind.ser.std;version="[2.7,3)",
    com.fasterxml.jackson.databind.deser.std;version="[2.7,3)",
    com.fasterxml.jackson.module.afterburner.ser;version="[2.7,3)",
    com.fasterxml.jackson.module.afterburner.deser;version="[2.7,3)"

...

你能告诉我哪里出了问题吗?为什么 Karaf 能够在一个地方加载 class 而在另一个地方失败?如果我 运行 这是 Karaf OSGi 容器外的普通 jar,只需添加一切就可以正常工作。

正如 Karaf 邮件 list 中所讨论的,resolution:optional 正在制造问题。从 Import-Package 中删除 resolution:optional 解决了问题。

<Import-Package>com.fasterxml.jackson.databind.ser,com.fasterxml.jackson.databind.deser,com.fasterxml.jackson.databind.ser.std,com.fasterxml.jackson.databind.deser.std,com.fasterxml.jackson.module.afterburner.ser,com.fasterxml.jackson.module.afterburner.deser,*</Import-Package>