【问题标题】:How to integrate KIE Workbench 6.1.0.Final (Drools Guvnor) Project with Java Application如何将 KIE Workbench 6.1.0.Final (Drools Guvnor) 项目与 Java 应用程序集成
【发布时间】:2014-12-24 07:02:08
【问题描述】:

我正在使用 KIE Workbench 6.1.0.Final,我尝试执行规则的 Eclipse 项目也启用了 6.1.0 Runtime。

我想知道如何从我的 java 应用程序中执行/加载使用 Drools Guvnor 创建的规则。在发布此问题之前,我查看了以下类似问题并尝试了给定的方法,但没有一个对我有用。

Integrating Drool 6 work bench with Java Application

why does loading Drools 6 KIE JAR into code fail?

Loading Drools/KIE Workbench artifacts directly from the repository

KIE Workbench Integration Responds with 401

我已按照click this 此处提供的步骤创建项目并定义规则。我能够构建和部署规则。我已尝试使用以下代码加载项目,但都失败并出现不同的异常。请注意,我可以从浏览器访问工作台 jar。

方法一。

public void load1(){
    KieServices kieServices = KieServices.Factory.get();
    ReleaseId releaseId = kieServices.newReleaseId( "com.example.rule", "RuleProject", "1.0" );
    KieContainer kContainer = kieServices.newKieContainer( releaseId );
    KieBaseConfiguration kieBaseConf = kieServices.newKieBaseConfiguration();
    kieBaseConf.setOption( EventProcessingOption.STREAM );
    KieBase kBase = kContainer.newKieBase(kieBaseConf);
    for ( KiePackage a : kBase.getKiePackages()) {
        for (Rule r : a.getRules()) {
            System.out.println("KiePackage {} Rule {}"+ new Object[]{a.getName()+"-"+ r.getName()});
        }
    }
}

例外

java.lang.RuntimeException: 找不到 KieModule: com.example.rule:RuleProject:1.0 在 org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:99)

方法 2。

public void load2(){            
    String url = "http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar";
    ReleaseIdImpl releaseId = new ReleaseIdImpl("com.example.rule", "RuleProject", "1.0");
    KieServices kieServices = KieServices.Factory.get();
    kieServices.getResources().newUrlResource(url);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);     
    // check every 5 seconds if there is a new version at the URL
    KieScanner kieScanner = kieServices.newKieScanner(kieContainer);
    kieScanner.start(5000L);
    // alternatively:
    // kieScanner.scanNow();
    Scanner scanner = new Scanner(System.in);
    while (true) {
        //runRule(kieContainer);
        System.out.println("Press enter in order to run the test again....");
        scanner.nextLine();
    }
}

异常

java.lang.RuntimeException: 找不到 KieModule: com.example.rule:RuleProject:1.0 在 org.drools.compiler.kie.builder.impl.KieServicesImpl.newKieContainer(KieServicesImpl.java:99)

方法3。

public void load3() throws IOException{         
    String url = "http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar";
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    UrlResource urlResource = (UrlResource) ks.getResources()
            .newUrlResource(url);
    urlResource.setUsername("admin");
    urlResource.setPassword("password");
    urlResource.setBasicAuthentication("enabled");
    InputStream is = urlResource.getInputStream();
    KieModule kModule = kr.addKieModule(ks.getResources()
            .newInputStreamResource(is));
    KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());

    kContainer.newStatelessKieSession();        

}

例外

java.io.IOException: Server returned HTTP response code: 401 for URL: http://example.com/drools/maven2/com/example/rule/RuleProject/1.0/RuleProject-1.0.jar
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at org.drools.core.io.impl.UrlResource.grabStream(UrlResource.java:257)
    at org.drools.core.io.impl.UrlResource.getInputStream(UrlResource.java:174)

这是我的工作台 jar pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.rule</groupId>
  <artifactId>RuleProject</artifactId>
  <version>1.0</version>
  <packaging>kjar</packaging>
  <name>RuleProject</name>
  <repositories>
    <repository>
      <id>guvnor-m2-repo</id>
      <name>Guvnor M2 Repo</name>
      <url>http://localhost:8080/drools/maven2/</url>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.kie</groupId>
        <artifactId>kie-maven-plugin</artifactId>
        <version>6.1.0.Final</version>
        <extensions>true</extensions>
      </plugin>
    </plugins>
  </build>
</project>

这是我的 java 应用程序 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>xcp.drool</groupId>
  <artifactId>xcpdrool</artifactId>
  <version>1</version>
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>always</updatePolicy>
      </releases>
      <id>guvnor-m2-repo</id>
      <name>Guvnor M2 Repo</name>
      <url>http://localhost:8080/drools/maven2/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\src\test\java</testSourceDirectory>
    <outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\classes</outputDirectory>
    <testOutputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>C:\p4_xb\xCP-Drool\xCPDrool1\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\p4_xb\xCP-Drool\xCPDrool1\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\p4_xb\xCP-Drool\xCPDrool1\target</directory>
    <finalName>xcpdrool-1</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\p4_xb\xCP-Drool\xCPDrool1\target\site</outputDirectory>
  </reporting>
  <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <id>guvnor-m2-repo</id>
          <name>Guvnor M2 Repo</name>
          <url>http://localhost:8080/drools/maven2/</url>
        </repository>
      </repositories>
    </profile>
  </profiles>
</project>

【问题讨论】:

    标签: workbench integrate drools-guvnor kie


    【解决方案1】:

    将您的 Eclipse 运行时更改为 6.0 0 并将工作台保持为 6.1.0 final。这应该适用于您的方法 3。但是您将无法使用 KieScanner。

    【讨论】:

      【解决方案2】:

      我遇到了和你一样的问题。使用您提到的第三种方法,我终于能够让它发挥作用。

      问题在于 UrlResource 类无法正确进行身份验证,因此出现了问题。我用以下代码 sn-p 替换了 UrlResource:

      String userpassword = "username:password";
      String url = "http://localhost:8080/drools/maven2/com/org/project/1.0/project-1.0.jar";
      httpURLConnection = (HttpURLConnection)new URL(url).openConnection();
      String authEnc = new Base64Encoder().encode(userpassword.getBytes());
      httpURLConnection.setRequestProperty("Authorization", "Basic "+ authEnc);
      is = httpURLConnection.getInputStream();
      KieModule kModule = kr.addKieModule(ks.getResources().newInputStreamResource(is));
      

      并使用这个 kModule 来获取 kContainer 和 kSession。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-06
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多