当使用RuleServiceClient
从Java应用程序调用时,我从Drools Execution Server收到成功但EMPTY结果,尽管来自Postman的REST调用返回预期结果。
我的问题:我的Java代码中有什么不正确?
请在下面找到详细信息。
我创建了示例规则(如果是字段Message.MyField == 1
,然后将此字段设置为400
),我就可以使用Postmen在KIE Execution Server上触发它:
对http://SERVER:8080/kie-server-webc/services/rest/server/containers/instances/kie-container
的POST请求:
{
"lookup": "defaultStatelessKieSession",
"commands": [{
"insert": {
"object": {
"Message": {
"myField": 1
}
},
"disconnected": false,
"out-identifier": "Message",
"return-object": true,
"entry-point": "DEFAULT"
}
}, {
"fire-all-rules": {
"max": -1,
"out-identifier": null
}
}]
}
回复(请注意"myField": 500
):
{
"type": "SUCCESS",
"msg": "Container kie-container successfully called.",
"result": {
"execution-results": {
"results": [
{
"key": "Message",
"value": {
"bnym.test1.Message": {
"myField": 500
}
}
}
],
"facts": [
{
"key": "Message",
"value": {
"org.drools.core.common.DefaultFactHandle": {
"external-form": "0:1:1208207159:1208207159:2:DEFAULT:NON_TRAIT:myProj.test1.Message"
}
}
}
]
}
}
}
借用教程的我的Java客户端代码是:
public class Message{
public Integer myField;
}
。 。
private static String URL = "http://SERVER:8080/kie-server-webc/services/rest/server";
private static final String USER = "user";
private static final String PASSWORD = "pwd";
。 。
public void transform() throws Exception {
Message m = new Message();
m.myField = 1;
KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(URL, USER, PASSWORD);
config.setMarshallingFormat(MarshallingFormat.JSON);
kieServicesClient = KieServicesFactory.newKieServicesClient(config);
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
KieCommands commandsFactory = KieServices.Factory.get().getCommands();
Command<?> insert = commandsFactory.newInsert(m);
Command<?> fireAllRules = commandsFactory.newFireAllRules();
Command<?> batchCommand = commandsFactory.newBatchExecution(Arrays.asList(insert, fireAllRules)); //0
ServiceResponse<String> executeResponse = rulesClient.executeCommands("kie-container", batchCommand);
if(executeResponse.getType() == ResponseType.SUCCESS) {
System.out.println("Commands executed with success! Response: ");
System.out.println(executeResponse.getResult());
}
}
结果:
Commands executed with success! Response:
{
"results" : [ ],
"facts" : [ ]
}
我的问题:我的Java代码中有什么不正确所以结果是空的?
谢谢
答案 0 :(得分:0)
我建议打开客户端的调试日志记录,查看已发送的请求,并将其与其他请求进行比较,以找出与其不符的内容。
对于您的应用正在使用的任何日志记录系统,请将此记录器设置为DEBUG(或者只是将所有内容设置为DEBUG,如果这对您更好): org.kie.server.client.impl.AbstractKieServicesClientImpl
答案 1 :(得分:0)
如果你想继续使用无状态会话,那么你应该创建一个无状态会话,按照示例代码:
public static StatelessKieSession getStatelessKieSession() {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
StatelessKieSession kSession = kContainer.newStatelessKieSession("defaultStatelessKieSession");
return kSession;
}
public void runRulesStateless(List<Object> objects) {
getStatelessKieSession().execute(objects);
}
public static void main(String[] args) {
runRulesStateless(Arrays.asList(new Object[] { new Message() }));
}
如果您想要一个不同的无状态会话,您可以在kmodule.xml中配置
答案 2 :(得分:0)
尝试使用以下命令:
Message m = new Message();
m.myField = 1;
Command<?> insert = commandsFactory.newInsert(m, "Message",true, "DEFAULT");
这等效于您使用的json请求:
"insert": {
"object": {
"Message": {
"myField": 1
}
},
"out-identifier": "Message",
"return-object": true,
"entry-point": "DEFAULT"
}