---
sourceDocument: Xanadu API リファレンス
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu API リファレンス

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# を使用するようにプロデューサーイベント通知フレームワークを構成する Hermes メッセージングサービス

# を使用するようにプロデューサーイベント通知フレームワークを構成する Hermes メッセージングサービス {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：7分

インスタンスで Hermes メッセージングサービス を使用している場合は、Kafka メッセージバスのプロデューサーイベント通知フレームワークを構成する必要があります。

## 始める前に

必要なロール：admin

Hermes メッセージングサービスをアクティブ化する必要があります。詳細については、「[Hermes Messaging Service activation](https://www.servicenow.com/docs/access?context=hermes-messaging-service-activation&version=xanadu&pubname=xanadu-servicenow-platform&ft:locale=en-US)」を参照してください。  
次のコード スニペットは、Hermes メッセージングサービス を使用してトピックにメッセージを送信するために必要な要素を構成するために呼び出す必要がある HermesEventPublisher メソッドを示しています。その後、 [ProducerV2](https://servicenow-prod.fluidtopics.net/sPVWKQg9VjSeHVQiuZol0A#ProducerV2ScopedAPI "ProducerV2 API は、ServiceNowインスタンスから Kafka トピックにメッセージを公開するためのメソッドを提供します。") API が呼び出されてメッセージが送信されます。

    publishEventToHermesKafka: function(tmfEventPayload) {
      if (gs.nil(tmfEventPayload)) {
        return;
      }
      this.publishMessageThroughProducerV2API(tmfEventPayload)

    },

    publishMessageThroughProducerV2API: function(message) {
      if (gs.nil(message)) return;
      var comptableTopicSysIds = this.fetchTopicsForMessage(message);
      if (gs.nil(comptableTopicSysIds)) return;

      for (var i = 0; i < comptableTopicSysIds.length; i++) {
        var partitionKey = this.fetchPartionKey(message);
        var headers = this.fetchMessageHeaders(message)
        try {
          var producer = new sn_ih_kafka.ProducerV2();
          producer.send(comptableTopicSysIds[i], partitionKey, JSON.stringify(message), false, headers);
        } catch (ex) {
          this._logger.logErr("Publishing message to Hermes Kafka Failed, error is: " + ex);
        }
      }
    },

## 手順

1. Hermes メッセージングサービス への安全な接続をセットアップします。  
   詳細については、「[Set up a secure connection to the Hermes Messaging Service](https://www.servicenow.com/docs/access?context=set-up-secure-connection-to-hermes&version=xanadu&pubname=xanadu-servicenow-platform&ft:locale=en-US)」を参照してください。
2. Hermes メッセージングサービスにメッセージを公開するときに、[HermesEventPublisherOOB:fetchTopicsForMessage (オブジェクトメッセージ)](https://servicenow-prod.fluidtopics.net/xTDquFi7RBIPWL_WR~N1_g#HEPO-fetchTopicsForMessage_O "渡されたメッセージに関連付けられているすべての Kafka トピックを返します。") メソッドのデフォルト機能をカスタムトピックピッカーロジックで上書きします。  
   HermesEventPublisher - fetchTopicsForMessage() メソッドでは、指定されたメッセージを送信する Kafka トピックを決定するロジックを指定する必要があります。次に、それらのトピックレコードのsys_idsをアレイで返します。

   メッセージペイロードの属性に基づいてトピックを選択することも、実装で単一のトピックのみを使用する場合は、メッセージごとに同じトピックを返すこともできます。  
   次の例は、渡されたメッセージごとに同じトピックレコードsys_idを返す方法を示しています。

       // Return the same topic for all messages if your 
        // implementation only supports one kind of topic

       fetchTopicsForMessage: function(message) { 
       // Return the same sys_id for all passed messages which
       // implies that all of the messages are published to the same topic.
       return ['91564fc087f8351063151f473cbb35f8'];
       },

   次の例は、メッセージ内のイベントタイプに基づいてメッセージを送信するトピックを識別する方法を示しています。

       // Fetch the topic based on the message content. 
       // This could be based on the message domain type or the message event type.

       fetchTopicsForMessage: function(message) { 
       // Implement to return topic based on a message content
       // Implementor may decide a topic based on 'eventType' or any other attribute.

       var eventType = message.eventType;

       var topicSysIdsArr = this.fetchTopicBasedOnEventType(eventType);
       return topicSysIdsArr

       },

3. [HermesEventPublisherOOB:fetchPartionKey (オブジェクトメッセージ)](https://servicenow-prod.fluidtopics.net/xTDquFi7RBIPWL_WR~N1_g#HEPO-fetchPartionKey_O "渡されたメッセージに関連付けられたパーティションキーを返します。") メソッドのデフォルト機能をカスタムロジックでオーバーライドして、Hermes メッセージングサービスにメッセージを発行するときに使用するパーティションキーを返します。  
   次の一覧には、返される可能性のあるパーティション キーの種類と、それに関連する利点が含まれています。
   * 自動生成された一意の ID。この種類のパーティション キーにより、メッセージがトピック パーティション間で均等に発行されます。これはデフォルトの実装です。
   * メッセージ・イベント・タイプ (またはその他のメッセージ属性) に基づく一意のキー。このタイプのパーティション キーは、そのイベントの種類 (または他のメッセージ属性) のメッセージの順序付けを保証します。
   * 同じパーティション キーであるため、メッセージは同じパーティションで送信されます。このタイプのパーティションキーにより、Kafka コンシューマーグループレベルでのメッセージの厳密な順序付けが保証されます。

   次の例は、パーティションキーごとにランダムな ID を返す方法を示しています。

       fetchPartionKey: function(message) { 
       // Implement it for custom partition key 
       // Random message keys ensure better distribution of message across partitions. 
       // Same message keys ensure that message always goes to the same partition.   
       // This ensures strict sequencing of messages at the Kafka consumer group level. 
       return gs.generateGUID(); 
       },

   次の例は、メッセージ内のイベントの種類に基づいてパーティション キーを返す方法を示しています。

       // Implementation where the partition key is decided based on the event type.
       fetchPartionKey: function(message) { 

       var eventType = message.eventType;
       var partionKey = fetchPartionKeyForMessage(eventType);
       return partionKey;
       },

4. [HermesEventPublisherOOB:fetchMessageHeaders (オブジェクトメッセージ)](https://servicenow-prod.fluidtopics.net/xTDquFi7RBIPWL_WR~N1_g#HEPO-fetchMessageHeaders_O "渡されたメッセージに関連付けられたメッセージヘッダーを返します。") メソッドのデフォルト機能をカスタムロジックで上書きして、Hermes メッセージングサービスを介してメッセージを公開する前に、指定されたメッセージのヘッダーをフェッチします。  
   これらのヘッダーの詳細については、 [Apache Kafka](https://kafka.apache.org/) のドキュメントを参照してください。  
   次の例は、メッセージ内のイベントの種類に基づいてヘッダーを返す方法を示しています。

       fetchMessageHeaders: function(message) { 
       // Returned headers are based on the event type passed in the message object. 
         var eventType = message.eventType;
         var headerObject = fetchHeadersBasedOnEventType(eventType);
         return headerObject;
       },


