1.前置条件:
搭建kie-web + kie-server的drools规则引擎
2.KieServerClientTest.java
package com.caicongyang;
import java.util.LinkedList;
import java.util.List;
import org.kie.api.KieServices;
import org.kie.api.command.Command;
import org.kie.api.command.KieCommands;
import org.kie.api.runtime.ExecutionResults;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.RuleServicesClient;
import com.ccy.com.ccy.drools.Person;
/**
*
*
* @author caicongyang
* @description 本例子为kie-server java client json 调用droos规则
*/
public class KieServerClientTest {
private static String url = "http://192.168.236.101:8080/kie-server/services/rest/server";
private static String username = "kieserver";
private static String password = "kieserver";
private static String container = "drools";
private static String kieSession = "kiesession1";
public static void main(String[] args) {
//bean 路径必须与kie-web上的bean 路径包名字段完全相同
Person person = new Person();
person.setAge(11);
person.setName("ccy");
KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(url, username, password);
config.setMarshallingFormat(MarshallingFormat.JSON);
config.setTimeout(30000L);
KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
RuleServicesClient rules = client.getServicesClient(RuleServicesClient.class);
KieCommands cmdFactory = KieServices.Factory.get().getCommands();
List<Command<?>> commands = new LinkedList<Command<?>>();
commands.add(cmdFactory.newInsert(person, "person"));
commands.add(cmdFactory.newFireAllRules());
ServiceResponse<org.kie.api.runtime.ExecutionResults> response = rules.executeCommandsWithResults(container,
cmdFactory.newBatchExecution(commands, kieSession));
System.out.println(response.getMsg());
ExecutionResults result = response.getResult();
person = (Person) result.getValue("person");
System.out.println(person.getName());
}
}
更多精彩内容请继续关注我的博客:http://blog.csdn.net/caicongyang
记录与分享,你我共成长 -from
caicongyang
如果你觉得本文对你有帮助,可以扫描下面的微信二维码,请我喝杯水咯!