繁体   English   中英

找不到默认的 KieSession

[英]Cannot find a default KieSession

我正在尝试使用 KieModule 创建 jar。 请看一下代码。

public static ReleaseId createKJarWithMultipleResources(String id, String[] resourceFiles) throws IOException {
        KieServices ks = KieServices.Factory.get();
        KieModuleModel kproj = ks.newKieModuleModel();
        KieFileSystem kfs = ks.newKieFileSystem();

        for (int i = 0; i < resourceFiles.length; i++) {

            kfs.write("src/main/resources/" + id.replaceAll("\\.", "/")
                    + "/" + i + ".drl", resourceFiles[i]);
        }

        KieBaseModel kBase1 = kproj.newKieBaseModel(id)
                .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
                .setEventProcessingMode(EventProcessingOption.STREAM);

        KieSessionModel ksession1 = kBase1
                .newKieSessionModel(id + ".KSession1")
                .setType(KieSessionModel.KieSessionType.STATEFUL)
                .setClockType(ClockTypeOption.get("pseudo"));
        kfs.writeKModuleXML(kproj.toXML());

        KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
        Results results = kieBuilder.getResults();
            if( results.hasMessages( org.kie.api.builder.Message.Level.ERROR ) ){
                System.out.println( results.getMessages() );
                throw new IllegalStateException( "### errors ###" );
            }
        KieModule kieModule = kieBuilder.getKieModule();
        return kieModule.getReleaseId();
    }

但是当我尝试使用以下代码使用 jar 时:

KieContainer kieContainer =
kieServices.newKieContainer(createKJarWithMultipleResources("1", 
new String[] 
{new String(Files.readAllBytes(Paths.get("path to drl file")))}
));
KieSession kSession = kieContainer.newKieSession();

我收到以下错误:

java.lang.RuntimeException: Cannot find a default KieSession
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:628)
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:621)
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:604)
    at com.sample.DroolsTest.test2(DroolsTest.java:87)

我错过了什么吗? 任何帮助将不胜感激。

问题解决了。 我只需要更改代码中的以下几行:

KieSessionModel ksession1 = kBase1
                .newKieSessionModel(id + ".KSession1")
                .setType(KieSessionModel.KieSessionType.STATEFUL)
                .setClockType(ClockTypeOption.get("pseudo"))
                .setDefault(true);

注意setDefault(true) ,这在原始代码中不存在。

我在 src/main/resources/META-INF/ 文件夹中创建了 kmodule.xml,使用这样的 conetnet

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule"/>

它对我有用

没有这个文件,它失败了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM