Loading Drools/KIE Workbench artifacts directly from the repository

寵の児 提交于 2019-11-27 18:54:09

I finally managed to get this solved. Below is a working example which loads the Drools artifact from the KIE-repository via HTTP and executes the rules:

package kieTest;

import java.util.Scanner;

import org.drools.compiler.kproject.ReleaseIdImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.KieScanner;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.StatelessKieSession;

public class MainKieTest {

    public static void main(String[] args) {

        // works even without -SNAPSHOT versions
        String url = "http://localhost:8080/kie-drools/maven2/de/test/Test/1.2.3/Test-1.2.3.jar";

        // make sure you use "LATEST" here!
        ReleaseIdImpl releaseId = new ReleaseIdImpl("de.test", "Test", "LATEST");

        KieServices ks = KieServices.Factory.get();

        ks.getResources().newUrlResource(url);

        KieContainer kieContainer = ks.newKieContainer(releaseId);

        // check every 5 seconds if there is a new version at the URL
        KieScanner kieScanner = ks.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();
        }
    }

    private static void runRule(KieContainer kieKontainer) {
        StatelessKieSession kSession = kieKontainer.newStatelessKieSession("testSession");
        kSession.setGlobal("out", System.out);
        kSession.execute("testRuleAgain");
    }
}

When searching for the solution, I found the following links helpful:

I hope someone finds this useful when getting SO as first search result ;-)

The code above uses maven and kie-ci. The URLResource you create is not used.

Here's a working sample :

    String url = "http://localhost:8080/kie-drools-wb/maven2/groupId/artifactId/1.0/artifactId-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();

Note that you still need to tweak a bit to allow this to work with the KieScanner.

craftsmannadeem

Here are the steps, All the steps are mandatory

  • Add kie-clie dependency in your pom

    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-ci</artifactId>
        <version>6.2.0.Final</version>
    </dependency>
    
  • Add your KIE workbench maven repo to your pom.xml

    <repository>
        <id>guvnor-m2-repo</id>
        <name>Guvnor M2 Repo</name>
        <url>http://localhost:8080/drools/maven2wb/</url>
    </repository>
    
  • Add the dependency to your pom.xml

    <dependency>
        <groupId>groupId</groupId>
        <artifactId>artifactId</artifactId>
        <version>LATEST</version>
    </dependency>
    
  • Provide your repo credentials into settings.xml

    <server>
        <id>guvnor-m2-repo</id>
        <username>admin</username>
        <password>@dmin</password>
    </server>
    
  • Java code

    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("groupID", "artifactID", "LATEST");
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieSession kieSession = kieContainer.newKieSession();
    kieSession.insert(object);
    kieSession.fireAllRules();
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!