OSGi:java.lang.IllegalStateException:卸载了Bundle(org.apache.servicemix.bundles.drools)

时间:2014-01-31 02:03:22

标签: osgi drools illegalstateexception osgi-bundle apache-servicemix

在卸载依赖于“org.apache.servicemix.bundles.drools / 5.5.0.Final_1”的代码然后再次安装我的软件包之后,drools以前工作的相关代码现在抛出了IllegalStateException“Bundle is uninstalled”消息,但仅限于特定的drools代码,例如当我尝试创建新会话时:

StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

如果我卸载org.apache.servicemix.bundles.drools然后重新安装它,问题就解决了,但我不认为这是正确的解决方案。还有其他捆绑依赖于drools,这可能会影响其功能。

这是完整的堆栈跟踪:

 java.lang.IllegalStateException: Bundle is uninstalled
    at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1832)
    at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:937)
    at org.springframework.osgi.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:99)
        at org.springframework.osgi.util.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.java:156)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.drools.util.CompositeClassLoader$DefaultLoader.load(CompositeClassLoader.java:204)
        at org.drools.util.CompositeClassLoader$DefaultLoader.load(CompositeClassLoader.java:187)
        at org.drools.util.CompositeClassLoader.loadClass(CompositeClassLoader.java:88)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.drools.SessionConfiguration.newTimerService(SessionConfiguration.java:441)
        at org.drools.time.TimerServiceFactory.getTimerService(TimerServiceFactory.java:27)
        at org.drools.common.AbstractWorkingMemory.<init>(AbstractWorkingMemory.java:315)
        at org.drools.common.AbstractWorkingMemory.<init>(AbstractWorkingMemory.java:246)
        at org.drools.common.AbstractWorkingMemory.<init>(AbstractWorkingMemory.java:212)
        at org.drools.reteoo.ReteooWorkingMemory.<init>(ReteooWorkingMemory.java:103)
        at org.drools.reteoo.ReteooStatefulSession.<init>(ReteooStatefulSession.java:81)
        at org.drools.reteoo.ReteooRuleBase.newStatefulSession(ReteooRuleBase.java:402)
        at org.drools.reteoo.ReteooRuleBase.newStatefulSession(ReteooRuleBase.java:387)
        at org.drools.impl.KnowledgeBaseImpl.newStatefulKnowledgeSession(KnowledgeBaseImpl.java:175)
        at org.drools.impl.KnowledgeBaseImpl.newStatefulKnowledgeSession(KnowledgeBaseImpl.java:162)
        at com.example.drools.ReportingRules.run(ReportingRules.java:78)
        at com.example.uimetadata.viewknowledge.builder.ReportViewKnowledgeBuilder.build(ReportViewKnowledgeBuilder.java:38)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:390)
        at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:277)
        at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:250)
        at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:161)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:73)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:91)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.fabric.FabricTraceProcessor.process(FabricTraceProcessor.java:81)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.RedeliveryErrorHandler.processErrorHandler(RedeliveryErrorHandler.java:334)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:220)
        at org.apache.camel.processor.interceptor.StreamCachingInterceptor.process(StreamCachingInterceptor.java:52)
        at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.processor.interceptor.DefaultChannel.process(DefaultChannel.java:308)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:117)
        at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
        at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:46)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150)
        at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117)
        at org.apache.camel.processor.RouteInflightRepositoryProcessor.processNext(RouteInflightRepositoryProcessor.java:48)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
        at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:73)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
        at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:275)
        at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:183)
        at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:139)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

这是我的流氓代码:

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.*;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.FactHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ReportingRules {

    private static Logger logger = LoggerFactory.getLogger(ReportingRules.class);

    private KnowledgeBase kbase;

    private ReportingRules() {

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

        kbuilder.add(ResourceFactory.newClassPathResource("com/example/drools/reports/reportRender.drl"), ResourceType.DRL);

        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                logger.error(error.getMessage());
            }
            throw new ReportRulesException("ReportRules: " + error.getMessage());
        }

        kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    }

public void run(ReportViewKnowledge reportViewKnowledge) {

        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        FactHandle factHandle = ksession.insert(reportViewKnowledge);

        ksession.fireAllRules();
    }

2 个答案:

答案 0 :(得分:0)

我从代码here看到的是,drools中的会话配置将类加载器存储在静态成员中。该错误表示此会话配置中的复合类加载器包含第一个(现在已卸载)包中的类加载器。删除此引用的唯一方法是删除包含引用的类(通过卸载drools)。

从代码中,似乎解决这个问题的方法是创建自己的会话配置对象并在该对象上设置类加载器。我不熟悉drools,但我想你需要首先创建一个KnowledgeBuilderConfiguration传递你的类的类加载器,然后从中创建一个KnowledgeBase实例,如:

KnowledgeBuilderConfiguration c = 
    KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null,
        ReportingRules.class.getClassLoader();
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(c);

希望这有助于获得最终解决方案。

答案 1 :(得分:0)

在我调查期间,我确定了异常的原因是在卸载了我们的核心软件包之后,我们需要在“Apache ServiceMix :: Bundles :: drools”软件包中使用drools库,我们需要在“Apache ServiceMix上调用osgi:refresh: :Bundles :: drools“因为bundle是”有线“的,引用了它们的依赖项,并且必须在卸载依赖项时刷新。

如果我在首先进入开发之前阅读本手册会有所帮助:)为了避免我的错误,这里是OSGi Alliance web site的链接,并尽可能多地了解OSGi。这个特殊信息可以在osgi.core-4.3.0.pdf,第147页,第7.3节中找到,这在我的案例中是正确的,因为我们正在运行基于JBoss Fuse 6.0Karaf 2.3.0 container