{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":822,"path":"api-reference/server-api-reference/HermesEventPublishOOBScopedAPI.md","title":"HermesEventPublisherOOB - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# HermesEventPublisherOOB- Scoped\n\nThe HermesEventPublisherOOB script include provides methods to publish messages to the Hermes Messaging Service.\n\nIf you want to change the default functionality of this script include, you need to override the methods contained in this script include in the `HermesEventPublisher` script include. For additional information on overriding the methods in this script include, see [Configure the Producer Event Notification Framework to use the Hermes Messaging Service](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/developer-guides/prod_evt_not-dev_gd-cfg_herm_bus.md).\n\nThis script include runs in the `sn_api_notif_mgmt` namespace.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## HermesEventPublisherOOB - fetchMessageHeaders\\(Object message\\)\n\nReturns the message headers associated with the passed message.\n\nYou must call this method before publishing the message through the Hermes Messaging Service.\n\nThe default implementation of this method only sets the *origin* header value to `TMT_SERVICENOW`. If any other header attributes should be passed back by this method based on your business requirements, you must override this method in the `HermesEventPublisher` script include and provide the additional header attributes.\n\n|Name|Type|Description|\n|----|----|-----------|\n|message|Object|TMF event to publish to the Hermes Kafka message broker. For details on the format of this message, see [TMF688 Event Management API User Guide v4.0.0](https://www.tmforum.org/resources/standard/tmf688-event-management-api-user-guide-v4-0-0/).|\n\n<table id=\"table_vwm_zd1_vzb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nObject\n\n</td><td>\n\nKafka headers to use for the passed message.For example:\n\n```\n{\n  origin: 'TMT_SERVICENOW'\n}\n```\n\n</td></tr></tbody>\n</table>The following code example shows how to call this method.\n\n```\npublishMessageThroughProducerV2API: function(message) {\n  if (gs.nil(message)) return;\n  var comptableTopicSysIds = this.fetchTopicsForMessage(message);\n  if (gs.nil(comptableTopicSysIds)) return;\n\n  for (var i = 0; i < comptableTopicSysIds.length; i++) {\n    var partitionKey = this.fetchPartionKey(message);\n    var headers = this.fetchMessageHeaders(message)\n    try {\n      var producer = new sn_ih_kafka.ProducerV2();\n      producer.send(comptableTopicSysIds[i], partitionKey, JSON.stringify(message), false, headers);\n    } catch (ex) {\n      this._logger.logErr(\"Publishing message to Hermes Kafka Failed, error is: \" + ex);\n    }\n  }\n},\n```\n\n## HermesEventPublisherOOB - fetchPartionKey\\(Object message\\)\n\nReturns the partition key associated with the passed message.\n\nYou must call this method before publishing the message through the Hermes Messaging Service.\n\nThe default implementation for this method returns an auto-generated unique ID as the partition key. This type of implementation ensures that messages are evenly published across all partitions of the topic. If you want to modify the handling of partitions, you must override this method in the `HermesEventPublisher` script include. Another possible implementation is to return a unique partition key based on the message event type, or other attribute, to ensure the sequencing of messages of the same event type or attribute.\n\n|Name|Type|Description|\n|----|----|-----------|\n|message|Object|Message to publish using the Hermes Kafka message broker. For details on the format of this message, see [TMF688 Event Management API User Guide v4.0.0](https://www.tmforum.org/resources/standard/tmf688-event-management-api-user-guide-v4-0-0/).|\n\n<table id=\"table_vwm_zd1_vzb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nString\n\n</td><td>\n\nKafka partition key to use for the passed message.For example: 6B29FC40-CA47-1067-B31D-00DD010662DA\n\n</td></tr></tbody>\n</table>The following code example shows how to call this method.\n\n```\npublishMessageThroughProducerV2API: function(message) {\n  if (gs.nil(message)) return;\n  var comptableTopicSysIds = this.fetchTopicsForMessage(message);\n  if (gs.nil(comptableTopicSysIds)) return;\n\n  for (var i = 0; i < comptableTopicSysIds.length; i++) {\n    var partitionKey = this.fetchPartionKey(message);\n    var headers = this.fetchMessageHeaders(message)\n    try {\n      var producer = new sn_ih_kafka.ProducerV2();\n      producer.send(comptableTopicSysIds[i], partitionKey, JSON.stringify(message), false, headers);\n    } catch (ex) {\n      this._logger.logErr(\"Publishing message to Hermes Kafka Failed, error is: \" + ex);\n    }\n  }\n},\n```\n\nThe following code example shows how to override this method to include your own logic for determining the partition key to use for the specified message.\n\n```\n// Existing implementation for fetching a partition key which is an auto-generated unique ID.\nfetchPartionKey: function(message) { \n  // Implement it for custom partition key \n  // Random message key ensures better distribution of messages across partitions. \n  // Same message key ensures that messages always goes to the same partition. This ensures a strict sequencing\n  //  of messages at the Kafka consumer group level. \n  return gs.generateGUID(); \n}, \n\n// Implementation where the partition key is determined based on the event type.\nfetchPartionKey: function(message) {\n  var eventType = message.eventType;\n  var partionKey = fetchPartionKeyForMessage(eventType);\n  return partionKey;\n},\n```\n\n## HermesEventPublisherOOB - fetchTopicsForMessage\\(Object message\\)\n\nReturns all Kafka topics that are associated with the passed message.\n\nThis method is not implemented in the base implementation, it only returns a static sys\\_id. You must override this method in the `HermesEventPublisher` script include and provide logic that determines which Kafka topic or topics to send the passed message to.\n\nFor additional information on how to implement this method, see [Configure the Producer Event Notification Framework to use the Hermes Messaging Service](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/developer-guides/prod_evt_not-dev_gd-cfg_herm_bus.md).\n\n|Name|Type|Description|\n|----|----|-----------|\n|message|Object|TMF message to publish to the Hermes Kafka message broker. For details on the format of this message, see [TMF688 Event Management API User Guide v4.0.0](https://www.tmforum.org/resources/standard/tmf688-event-management-api-user-guide-v4-0-0/).|\n\n<table id=\"table_vwm_zd1_vzb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nArray\n\n</td><td>\n\nList of sys\\_ids of the Kafka topics to which the message should be sent.For example:\n\n```\n[\n  \"9d385017c611228701d22104cc95c371\",\n  \"8438201ec613628421e13105d396d421\"\n]\n```\n\nTable: Kafka Topic \\[cmdb\\_ci\\_appl\\_kafka\\_topic\\]\n\n</td></tr></tbody>\n</table>The following code example shows how to call this method.\n\n```\npublishMessageThroughProducerV2API: function(message) {\n  if (gs.nil(message)) return;\n  var comptableTopicSysIds = this.fetchTopicsForMessage(message);\n  if (gs.nil(comptableTopicSysIds)) return;\n\n  for (var i = 0; i < comptableTopicSysIds.length; i++) {\n    var partitionKey = this.fetchPartionKey(message);\n    var headers = this.fetchMessageHeaders(message)\n    try {\n      var producer = new sn_ih_kafka.ProducerV2();\n      producer.send(comptableTopicSysIds[i], partitionKey, JSON.stringify(message), false, headers);\n    } catch (ex) {\n      this._logger.logErr(\"Publishing message to Hermes Kafka Failed, error is: \" + ex);\n    }\n  }\n},\n```\n\nThe following code example shows how to override this method to return the same topic record sys\\_id for each passed message.\n\n```\n// Return the same topic for all messages if your \n // implementation only supports one kind of topic\n\nfetchTopicsForMessage: function(message) { \n// Return the same sys_id for all passed messages which\n// implies that all of the messages are published to the same topic.\nreturn ['91564fc087f8351063151f473cbb35f8'];\n},\n```\n\nThe following code example shows how to override this method to identify topics to send a message to based on the event type in the message.\n\n```\n// Fetch the topic based on the message content. \n// This could be based on the message domain type or the message event type.\n\nfetchTopicsForMessage: function(message) { \n// Implement to return topic based on a message content\n// Implementor may decide a topic based on 'eventType' or any other attribute.\n\nvar eventType = message.eventType;\n\nvar topicSysIdsArr = this.fetchTopicBasedOnEventType(eventType);\nreturn topicSysIdsArr\n\n},\n```\n\n## HermesEventPublisherOOB - publishEventToHermesKafka\\(Object tmfEventPayload\\)\n\nPublishes the passed TMF688-compliant payload using the Hermes Messaging Service.\n\nThis is a parent method that calls other methods to publish the event. It is unlikely that you will need to override this method unless you want to change the Kafka publishing mechanism, such as using Workflow Studio instead of using the [ProducerV2](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ProducerV2ScopedAPI.md) API.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tmfEventPayload|Object|TMF688-compliant event payload. For a description of the format of this event payload, refer to the [TMF688 Event Management API User Guide](https://www.tmforum.org/resources/standard/tmf688-event-management-api-user-guide-v4-0-0/).|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following code example shows how to call this method.\n\n```\npublishMessageToHermesKafka: function(tmfEventPayload) {\n  new HermesEventPublisher().publishEventToHermesKafka(tmfEventPayload);\n},\n```\n\n"}}