Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - Loading Drools/KIE Workbench artifacts directly from the repository

We try to switch to Drools 6 with the all new KIE workbench (formerly known as Guvnor) and the new maven-based artifacts.

Now I'd like to use the the system described in this blog post in the second image ("Deployment"): Loading the rules via HTTP from the workbench repository (the dotted arrow, going from HTTP on the left directly into the application).

The problem is, that I have no idea how to load the artifact into my KieServices/KieModule object. I basically do not want to use maven, I also cannot provide the path to maven's settings.xml globally as a Java parameter, so this option is out.

I think that a similar issue is this one. As mentioned there, I also tried to load an URL resource but the problem seems to be that the system cannot determine, what kind of ResourceType the given URL (http://localhost:8080/kie-drools/maven2/.../-1.0.0.jar) is. And yes, I can access the .jar from the repository directly from the browser, without authentication.

Any ideas or tutorials how to do this?

My testing code:

public static void main(String[] args) {
    KieServices ks = KieServices.Factory.get();
    KieRepository repo = ks.getRepository();

    String url = "http://localhost:8080/kie-drools/maven2/de/test/test/1.0.0/test-1.0.0.jar";

    Resource urlResource = ks.getResources().newUrlResource(url);
    KieModule kModule = repo.addKieModule(urlResource); // this already fails
}

The error:

Exception in thread "main" java.lang.RuntimeException: Unable to fetch module from resource :[UrlResource path='http://localhost:8080/kie-drools/maven2/de/itm/Herma400/1.0.1/Herma400-1.0.1.jar']
    at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.getKieModule(KieRepositoryImpl.java:205)
    at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.addKieModule(KieRepositoryImpl.java:161)
    at kieTest.MainKieTest.main(MainKieTest.java:24)
Caused by: java.lang.NullPointerException
    at org.drools.compiler.kie.builder.impl.ClasspathKieProject.getPomProperties(ClasspathKieProject.java:197)
    at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:148)
    at org.drools.compiler.kie.builder.impl.ClasspathKieProject.fetchKModule(ClasspathKieProject.java:109)
    at org.drools.compiler.kie.builder.impl.KieRepositoryImpl.getKieModule(KieRepositoryImpl.java:190)
    ... 2 more

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 link helpful:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...