Use the Kafka Producer step and the ProducerV2 API to publish messages

  • Release version: Yokohama
  • Updated January 30, 2025
  • 2 minutes to read
  • Push data from ServiceNow to Hermes so that the Kafka consumers can read them.

    Before you begin

    Procedure

    1. Start two consumers to receive messages.
      1. Run the following commands from the command line to receive the messages.
        bin/kafka-console-consumer.sh --consumer.config config/bootcamp.properties --topic snc.<instance name>.topic1 --group snc.<instance name>.consumer_group1 --from-beginning --bootstrap-server <instance name>.service-now.com:4100,<instance name>.service-now.com:4101,<instance name>.service-now.com:4102,<instance name>.service-now.com:4103
        bin/kafka-console-consumer.sh --consumer.config config/bootcamp.properties --topic snc.<instance name>.topic1 --group snc.<instance name>.consumer_group1 --from-beginning --bootstrap-server <instance name>.service-now.com:4200, <instance name>.service-now.com:4201, <instance name>.service-now.com:4202, <instance name>.service-now.com:4203
        Be sure to replace <instance name> with the name of your instance.
    2. Publish messages from the instance using a flow step.
      1. Navigate to All > Process Automation > Flow Designer.
      2. Create a new action by selecting Create new > Action.
      3. In the Action properties form, give your action a name, then select Submit.
        This example uses the name Message Publisher.
      4. Add a Kafka Producer step to the action.

        To add an action step, select the plus sign between the Inputs and Error Evaluation in the Action Outline.

        Add the Kafka Producer step to the action.
      5. In the step, set the Topic field to topic1.
      6. In the Message field, enter a message.

        You don't need to change any other fields.

        Fill in the fields for the Kafka Producer step.
      7. Select Save to save the action.
      8. Select the Test button to test the action.
        Testing the action publishes the message to the topic.
      9. To view the message, go to the command-line consoles created in step 1.
        The message should be present in one of the terminals.
    3. Publish messages from the instance using a script.
      1. Run the following script using the scripts background to publish messages to this topic from the instance.
        var producer = new sn_ih_kafka.ProducerV2();
        producer.send('<sys id of the topic record>', gs.generateGUID(), 'Test message from ServiceNow', false, null);
        
        Be sure to replace <sys id of the topic record> with the sys_id of the topic record.
      2. To view the message, go to the command-line consoles created in step 1.
        The message should be present in one of the terminals.