Manually create and edit a Kafka script

This task describes how to manually create and edit Kafka protocol scripts.

Create or edit a Kafka script

You can manually create and edit Kafka scripts using the VuGen editor.

To manually create or edit a Kafka script:

  1. Prerequisite: Before working with Kafka scripts, make sure your Java environment is set up. For details, see Set up the environment for Java protocols.

  2. In VuGen, open a Kafka script.

  3. Insert your Java code into the script in the relevant methods. When creating a script, you must initialize the producer and consumer using VuGen's Kafka APIs. You initialize the producer and consumer in the init method and close them in the end method. For details, see Script classes and methods.

    Note: Kafka scripts run as a scalable multi-threaded application. Make sure that any code you include is thread-safe. For details, see Java protocol programming tips.

  4. Enhance your script with rendezvous points, transactions, output messages, and so on. For more information, see Enhance a Java script.

Back to top

Script classes and methods

When working with a Kafka script, you place all your code in the Actions class, which is displayed when the script opens. The Actions class contains three methods: init, action, and end.

The following table shows what to include in each method and when each method is executed:

Script method
Method is executed
Details
init
When the Vuser is initialized

This method emulates a login to a server and is executed only once. Place all login procedures and one-time configuration settings here, including initializing the producer and consumer.

Example: Inserting the following lines in the init method initializes the producer and consumer using the login properties of the specified files:

LrKafkaConsumer.initConsumer(“\\any.domain.net\KafkaConfigs\consumer.properties”);

LrKafkaProducer.initProducer(“\\any.domain.net\KafkaConfigs\producer.properties”);

action
When the Vuser is in "Running" status
This method emulates client activity, and is executed according to the number of iterations you set in the runtime settings. Place all Vuser actions here.
end
When the Vuser finishes or is stopped

This method emulates a log off procedure and is executed only once. Place all procedures you want the Vuser to execute at the end of the script here, including closing the producer and consumer, and deallocating any resources used during the script.

Example: Inserting the following lines into the end method closes the producer and consumer:

LrKafkaConsumer.closeConsumer();

LrKafkaProducer.closeProducer();

Back to top

See also: