第22章 KIE Server の機能と拡張

KIE Server の機能は、ビジネスニーズに合わせて有効化、無効化、または拡張可能なプラグインにより決まります。KIE Server は以下の機能および拡張をサポートします。

表22.1 KIE Server の機能と拡張

機能名拡張名説明

KieServer

KieServer

サーバーインスタンスでの KIE コンテナーの作成や削除など、KIE Server のコア機能を提供します。

BRM

Drools

ファクトの挿入やビジネスルールの実行など、ビジネスルール管理 (BRM) 機能を提供します。

BPM

jBPM

ユーザータスクの管理や、ビジネスプロセスの実行など、Business Process Management (BPM) 機能を提供します。

BPM-UI

jBPM-UI

XML フォームや SVG イメージをプロセスダイアグラムにレンダリングするなど、ビジネスプロセスに関連するユーザーインターフェイス機能を提供します。

CaseMgmt

Case-Mgmt

ケースの定義やマイルストーン管理など、ビジネスプロセスのケース管理機能を提供します。

BRP

OptaPlanner

ソルバーの実装など、ビジネスリソースプランニング (BRP) 機能を提供します。

DMN

DMN

DMN データ型の管理や DMN モデルの実行など、Decision Model and Notation (DMN) 機能を提供します。

Swagger

Swagger

KIE Server REST API と対話するための Swagger の Web インターフェイス機能を提供します。

実行中の KIE Server インスタンスに対応する拡張を表示するには、以下の REST API エンドポイントに GET 要求を送信して、XML または JSON サーバーの要求を確認します。

KIE Server の情報に対する GET 要求のベース URL

http://SERVER:PORT/kie-server/services/rest/server

KIE Server の情報を含む JSON 応答の例

{
  "type": "SUCCESS",
  "msg": "Kie Server info",
  "result": {
    "kie-server-info": {
      "id": "test-kie-server",
      "version": "7.26.0.20190818-050814",
      "name": "test-kie-server",
      "location": "http://localhost:8080/kie-server/services/rest/server",
      "capabilities": [
        "KieServer",
        "BRM",
        "BPM",
        "CaseMgmt",
        "BPM-UI",
        "BRP",
        "DMN",
        "Swagger"
      ],
      "messages": [
        {
          "severity": "INFO",
          "timestamp": {
            "java.util.Date": 1566169865791
          },
          "content": [
            "Server KieServerInfo{serverId='test-kie-server', version='7.26.0.20190818-050814', name='test-kie-server', location='http:/localhost:8080/kie-server/services/rest/server', capabilities=[KieServer, BRM, BPM, CaseMgmt, BPM-UI, BRP, DMN, Swagger]', messages=null', mode=DEVELOPMENT}started successfully at Sun Aug 18 23:11:05 UTC 2019"
          ]
        }
      ],
      "mode": "DEVELOPMENT"
    }
  }
}

KIE Server 拡張機能を有効または無効にするには、関連する KIE Server システムプロパティー (*.server.ext.disabled) を設定します。たとえば、BRM 機能を無効にするには、org.drools.server.ext.disabled=true システムプロパティーを設定します。全 KIE Server システムプロパティーについては、21章KIE Server のシステムプロパティー を参照してください。

KIE Server 拡張機能は、デフォルトでは REST または JMS データトランスポートで公開され、事前定義済みのクライアント API を使用します。追加の REST エンドポイントで既存の KIE Server 機能を拡張するか、REST または JMS 以外の対応のトランスポートメソッドを拡張するか、KIE Server クライアントの機能を拡張できます。

KIE Server 機能は柔軟であるため、デフォルトの KIE Server 機能にビジネスニーズを合わせるのではなく、KIE Server インスタンスをビジネスニーズに合わせることができます。

重要

KIE Server 機能を拡張した場合、Red Hat では、カスタムの実装や拡張の一部として使用したカスタムコードをサポートしません。

22.1. カスタム REST API エンドポイントを使用した既存の KIE Server 機能の拡張

KIE Server REST API を使用すると、Business Central ユーザーインターフェイスを使わずに Red Hat Process Automation Manager の KIE コンテナーやビジネスアセット (ビジネスルール、プロセス、ソルバーなど) を操作することができます。利用可能な REST エンドポイントは、KIE Server システムプロパティーで有効にした機能により決まります (例: BRM 機能は org.drools.server.ext.disabled=false)。既存の KIE Server 機能は、カスタムの REST API エンドポイントで拡張し、ビジネスニーズに合わせて KIE Server REST API を適合できます。

たとえば、この手順では、以下のカスタム REST API エンドポイントで Drools KIE Server 機能 (BRM 機能向け) を拡張します。

カスタム REST API エンドポイントの例

/server/containers/instances/{containerId}/ksession/{ksessionId}

このカスタムのエンドポイントの例では、デシジョンエンジンの作業メモリーに挿入するファクト一覧を受け入れ、自動的に全ルールを実行して、指定の KIE コンテナーで KIE セッションからのオブジェクトをすべて取得します。

手順

  1. 空の Maven プロジェクトを作成して、以下のパッケージタイプと依存関係を、プロジェクトの pom.xml ファイルに定義します。

    サンプルプロジェクトの pom.xml ファイルの例

    <packaging>jar</packaging>
    
    <properties>
      <version.org.kie>7.48.0.Final-redhat-00004</version.org.kie>
    </properties>
    
    <dependencies>
      <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-api</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-internal</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.kie.server</groupId>
        <artifactId>kie-server-api</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.kie.server</groupId>
        <artifactId>kie-server-services-common</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.kie.server</groupId>
        <artifactId>kie-server-services-drools</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.kie.server</groupId>
        <artifactId>kie-server-rest-common</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>${version.org.kie}</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
      </dependency>
    </dependencies>

  2. 以下の例のように、プロジェクトの Java クラスに org.kie.server.services.api.KieServerApplicationComponentsService インターフェイスを実装します。

    KieServerApplicationComponentsService インターフェイスの実装例

    public class CusomtDroolsKieServerApplicationComponentsService implements KieServerApplicationComponentsService {  1
    
        private static final String OWNER_EXTENSION = "Drools";  2
    
        public Collection<Object> getAppComponents(String extension, SupportedTransports type, Object... services) {  3
            // Do not accept calls from extensions other than the owner extension:
            if ( !OWNER_EXTENSION.equals(extension) ) {
                return Collections.emptyList();
            }
    
            RulesExecutionService rulesExecutionService = null;  4
            KieServerRegistry context = null;
    
            for( Object object : services ) {
                if( RulesExecutionService.class.isAssignableFrom(object.getClass()) ) {
                    rulesExecutionService = (RulesExecutionService) object;
                    continue;
                } else if( KieServerRegistry.class.isAssignableFrom(object.getClass()) ) {
                    context = (KieServerRegistry) object;
                    continue;
                }
            }
    
            List<Object> components = new ArrayList<Object>(1);
            if( SupportedTransports.REST.equals(type) ) {
                components.add(new CustomResource(rulesExecutionService, context));  5
            }
    
            return components;
        }
    
    }

    1
    アプリケーションの起動時にデプロイされる KIE Server インフラストラクチャーに REST エンドポイントを提供します。
    2
    この例の Drools 拡張など、拡張する機能を指定します。
    3
    REST コンテナーがデプロイする必要のある全リソースを返します。KIE Server インスタンスで有効化した各拡張で、getAppComponents メソッドを呼び出して、指定した OWNER_EXTENSION 以外の拡張の空のコレクションを、if ( !OWNER_EXTENSION.equals(extension) ) の呼び出しで返します。
    4
    この例の Drools 拡張の RulesExecutionServiceKieServerRegistry サービスなど、指定の拡張から使用するサービスを表示します。
    5
    components リストの一部としてリソースを返す CustomResource クラスと、拡張のトランスポートタイプを REST または JMS に指定します (この例では REST)。
  3. 以下の例のように、KIE Server を使用して新規の REST リソースの機能を追加する CustomResource クラスを実装します。

    CustomResource クラスの実装例

    // Custom base endpoint:
    @Path("server/containers/instances/{containerId}/ksession")
    public class CustomResource {
    
        private static final Logger logger = LoggerFactory.getLogger(CustomResource.class);
    
        private KieCommands commandsFactory = KieServices.Factory.get().getCommands();
    
        private RulesExecutionService rulesExecutionService;
        private KieServerRegistry registry;
    
        public CustomResource() {
    
        }
    
        public CustomResource(RulesExecutionService rulesExecutionService, KieServerRegistry registry) {
            this.rulesExecutionService = rulesExecutionService;
            this.registry = registry;
        }
    
        // Supported HTTP method, path parameters, and data formats:
        @POST
        @Path("/{ksessionId}")
        @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        public Response insertFireReturn(@Context HttpHeaders headers,
                @PathParam("containerId") String id,
                @PathParam("ksessionId") String ksessionId,
                String cmdPayload) {
    
            Variant v = getVariant(headers);
            String contentType = getContentType(headers);
    
            // Marshalling behavior and supported actions:
            MarshallingFormat format = MarshallingFormat.fromType(contentType);
            if (format == null) {
                format = MarshallingFormat.valueOf(contentType);
            }
            try {
                KieContainerInstance kci = registry.getContainer(id);
    
                Marshaller marshaller = kci.getMarshaller(format);
    
                List<?> listOfFacts = marshaller.unmarshall(cmdPayload, List.class);
    
                List<Command<?>> commands = new ArrayList<Command<?>>();
                BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands, ksessionId);
    
                for (Object fact : listOfFacts) {
                    commands.add(commandsFactory.newInsert(fact, fact.toString()));
                }
                commands.add(commandsFactory.newFireAllRules());
                commands.add(commandsFactory.newGetObjects());
    
                ExecutionResults results = rulesExecutionService.call(kci, executionCommand);
    
                String result = marshaller.marshall(results);
    
    
                logger.debug("Returning OK response with content '{}'", result);
                return createResponse(result, v, Response.Status.OK);
            } catch (Exception e) {
                // If marshalling fails, return the `call-container` response to maintain backward compatibility:
                String response = "Execution failed with error : " + e.getMessage();
                logger.debug("Returning Failure response with content '{}'", response);
                return createResponse(response, v, Response.Status.INTERNAL_SERVER_ERROR);
            }
    
        }
    }

    この例では、カスタムエンドポイントの CustomResource クラスで、以下のデータと動作を指定します。

    • server/containers/instances/{containerId}/ksession のベースポイントを使用します。
    • POST HTTP メソッドを使用します。
    • REST 要求で以下のデータを指定する必要があります。

      • パスの引数として containerId
      • パスの引数として ksessionId
      • メッセージペイロードとしてファクトの一覧
    • 全 KIE Server データ形式をサポートします。

      • XML (JAXB、XStream)
      • JSON
    • List<?> コレクションにペイロードをアンマーシャリングして、リスト内のアイテムごとに、InsertCommand インスタンスを作成し、その後に FireAllRulesGetObject コマンドを追加します。
    • デシジョンエンジンを呼び出す BatchExecutionCommand インスタンスに全コマンドを追加します。
  4. 新規エンドポイントを KIE Server で検出できるようにするには、Maven プロジェクト内に META-INF/services/org.kie.server.services.api.KieServerApplicationComponentsService ファイルを作成して、このファイルに KieServerApplicationComponentsService 実装クラスの完全修飾名を追加します。たとえば、このファイルには、org.kie.server.ext.drools.rest.CusomtDroolsKieServerApplicationComponentsService の 1 行が含まれます。
  5. プロジェクトを構築して、作成された JAR ファイルをプロジェクトの ~/kie-server.war/WEB-INF/lib ディレクトリーにコピーします。たとえば、Red Hat JBoss EAP ではこのディレクトリーへのパスは EAP_HOME/standalone/deployments/kie-server.war/WEB-INF/lib です。
  6. KIE Server を起動して、実行中の KIE Server に構築したプロジェクトをデプロイします。プロジェクトは、Business Central インターフェイスまたは KIE Server REST API (http://SERVER:PORT/kie-server/services/rest/server/containers/{containerId} への PUT 要求) を使用してデプロイできます。

    実行中の KIE Server にプロジェクトを追加した後に、新しい REST エンドポイントとの対話を開始します。

    今回の例では、以下の情報を使用して新規エンドポイントを呼び出すことができます。

    • 要求 URL 例: http://localhost:8080/kie-server/services/rest/server/containers/instances/demo/ksession/defaultKieSession
    • HTTP メソッド: POST
    • HTTP ヘッダー:

      • Content-Type: application/json
      • Accept: application/json
    • メッセージペイロードの例:

      [
        {
          "org.jbpm.test.Person": {
            "name": "john",
            "age": 25
          }
        },
        {
          "org.jbpm.test.Person": {
            "name": "mary",
            "age": 22
          }
        }
      ]
    • サーバーの応答例: 200 (success)
    • サーバーのログ出力例:

      13:37:20,347 INFO  [stdout] (default task-24) Hello mary
      13:37:20,348 INFO  [stdout] (default task-24) Hello john