Retrieve messages from Hermes with the Kafka Message trigger in Workflow Studio and the Script consumer.
Procedure
-
Create a topic to consume messages.
-
Use the following command to create a topic in Hermes.
bin/kafka-topics.sh --create --command-config config/bootcamp.properties --bootstrap-server <instance name>.service-now.com:4000,<instance name>.service-now.com:4001,<instance name>.service-now.com:4002, <instance name>.service-now.com:4003 --topic snc.<instance name>.topic1
Be
sure to replace
<instance name> with the name of your instance.
-
To view the topic, log in to the instance and navigate to .
Note: It takes approximately 10 minutes to create a topic.
-
Create a flow trigger to consume messages from the topic.
-
Navigate to .
-
Select .
-
On the Flow properties form, enter a name for your flow, then enter Submit.
This example uses the name Topic1 Consumer.
-
In the trigger section, select Add a trigger, then select Kafka Message as the trigger type.
-
Select the Topic1 topic.
You can use the magnifying glass icon to see the list of available topics.
-
In the Actions section of the flow, navigate to .
-
Drag the Messages data pill to the Items field, then select Done.
-
In the Actions section of the flow, navigate to .
-
Drag the Payload data pill to the Message field, then select Done.
-
Save the flow trigger.
-
Go to the Settings [sys_flow_execution_setting] table to enable reporting for this flow.
-
Create a new entry and enable reporting to the flow created above.
-
Navigate back to your flow and select Activate.
-
To view your topic subscription, navigate to .
-
Publish messages to the topic using the Kafka command-line tool.
-
Run the following command to publish messages to the topic.
bin/kafka-console-producer.sh --topic snc.<instance name>.topic1 --producer.config config/bootcamp.properties --bootstrap-server <instance name>.service-now.com:4000,<instance name>.service-now.com:4001,<instance name>.service-now.com:4002,<instance name>.service-now.com:4003
Be sure to replace <instance name> with the name of your instance.
This command prompts you for messages. Add a few sample messages. This example uses the following messages.
- Test message1
- Test message2
- Test message3
You should receive the messages in about a minute.
-
Navigate back to the flow and select Executions.
-
Open the flow execution.
-
To view the statistics for this subscription, navigate to , open the subscription, and go to the statistics view.
-
Create a Script consumer to consume messages from the topic.
-
Navigate to .
-
Select New.
-
In the Name field, enter a name for your script consumer.
This example uses Topic1 Consumer.
-
Set the script to log the message by adding the following logic in the script box.
for (var i = 0; i < messages.length; i++) {
gs.info('Received message ' + JSON.stringify(messages[i].message));
}
-
Save the form.
-
In the Related Links section, select New to add a Kafka stream to consume the messages.
-
On the Kafka Stream form, set the following field values.
- Name to Topic1 Stream.
- Topic to the topic created earlier, topic1.
- Max concurrency to 1.
- Start Consuming to All the messages stored in the topic. This option enables you to read the messages already published to this topic.
- Message handling to Automatically optimize.
-
Select the Activate link under Related Links.
You should be able to see the activated subscription.
-
To view the messages, navigate to and filter messages that start with Received message.
-
You should see all the messages after 1 minute.