Java 直接从存储库加载 Drools/KIE Workbench 工件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21186570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading Drools/KIE Workbench artifacts directly from the repository
提问by Dominik Sandjaja
We try to switch to Drools 6 with the all new KIE workbench (formerly known as Guvnor) and the new maven-based artifacts.
我们尝试使用全新的 KIE 工作台(以前称为 Guvnor)和新的基于 maven 的工件切换到 Drools 6。
Now I'd like to use the the system described in this blog postin 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).
现在我想使用第二张图片(“部署”)中这篇博客文章中描述的系统:通过 HTTP 从工作台存储库加载规则(虚线箭头,从左侧的 HTTP 直接进入应用程序) .
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.
问题是,我不知道如何将工件加载到我的 KieServices/KieModule 对象中。我基本上不想使用maven,我也无法将maven的settings.xml
全局路径作为Java参数提供,所以这个选项被淘汰了。
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.
我认为类似的问题是这个。正如那里提到的,我也尝试加载一个 URL 资源,但问题似乎是系统无法确定ResourceType
给定的 URL ( http://localhost:8080/kie-drools/maven2/.../-1.0.0.jar
) 是哪种类型。是的,我可以直接从浏览器访问存储库中的 .jar,无需身份验证。
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!
提前致谢!
采纳答案by Dominik Sandjaja
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:
我终于设法解决了这个问题。下面是一个工作示例,它通过 HTTP 从 KIE 存储库加载 Drools 工件并执行规则:
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 ;-)
我希望有人在将 SO 作为第一个搜索结果时会发现这很有用 ;-)
回答by jps
The code above uses maven and kie-ci. The URLResource you create is not used.
上面的代码使用了 maven 和 kie-ci。不使用您创建的 URLResource。
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.
请注意,您仍然需要稍微调整以使其与 KieScanner 一起使用。
回答by 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();
在你的 pom 中添加 kie-clie 依赖项
<dependency> <groupId>org.kie</groupId> <artifactId>kie-ci</artifactId> <version>6.2.0.Final</version> </dependency>
将您的 KIE 工作台 maven 存储库添加到您的
pom.xml
<repository> <id>guvnor-m2-repo</id> <name>Guvnor M2 Repo</name> <url>http://localhost:8080/drools/maven2wb/</url> </repository>
将依赖添加到您的
pom.xml
<dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <version>LATEST</version> </dependency>
将您的回购凭据提供到 settings.xml 中
<server> <id>guvnor-m2-repo</id> <username>admin</username> <password>@dmin</password> </server>
Java代码
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();