0

RuleServiceClient当来自 Java 应用程序的调用使用来自 Postman 的 REST 调用返回预期结果时,我收到了来自 Drools 执行服务器的成功但 EMPTY结果。

我的问题:我的 Java 代码中有什么不正确的地方?

请在下面找到详细信息。

我创建了示例规则(如果字段Message.MyField == 1然后将此字段设置为400),我能够使用 Postmen 在 KIE 执行服务器上触发它:

POST 请求http://SERVER:8080/kie-server-webc/services/rest/server/containers/instances/kie-container

{
    "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 代码中有什么不正确的,所以结果是空的?

谢谢

4

3 回答 3

0

我建议为客户端打开调试日志以查看发送的请求并将其与您的另一个请求进行比较以找出不正确的地方。

对于您的应用程序使用的任何日志记录系统,将此记录器设置为 DEBUG(或者如果这对您更好,则将所有内容设置为 DEBUG):org.kie.server.client.impl.AbstractKieServicesClientImpl

于 2017-03-25T21:16:35.420 回答
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"
    }
于 2019-02-27T12:48:18.390 回答
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 中配置

于 2018-01-26T17:06:09.263 回答