Construct a generic client producer using property name-value pairs
- Last Updated: July 18, 2024
- 4 minute read
- OpenEdge
- Version 12.8
- Documentation
- Use the
OpenEdge.MessagingAPI. - Create a producer builder.
- Configure producer builder properties using the
SetProducerOption()method. - Build the producer.
- Create a record builder.
- Set the topic name and message body.
- Build the record.
- Send the record.
- Wait for the response.
- Handle any errors.
- Clean up.
These steps are similar to the steps in Construct a client producer using strongly-typed methods, except for the first three steps.
A breakdown of each step is provided next, followed by a complete code example.
Use the OpenEdge.Messaging API
|
The OpenEdge.Messaging API provides a common
set of messaging-focused interfaces. With this API, you can instantiate a message
producer, create messages, populate the message body, and send the message to a
destination.
Create a producer builder
|
To create a Kafka ProducerBuilder, call the Create() method with the "progress-kafka" argument, to get the Progress-provided implementation
of the Kafka client.
Configure producer builder properties using the SetProducerOption() method
SetProducerOption() is a method in the OpenEdge.Messaging.ProducerBuilder class which sets a
Kafka producer configuration property using a name-value pair. Here is the
syntax:
|
- prop-name
- The name of a Kafka producer property.
- value
- The value of the property.
The following example uses SetProducerOption() to set two properties:
|
The bootstrap.servers property takes a
comma-delimited list of servers in the Kafka cluster. Bootstrap.servers is required.
The value.serializer property specifies the
serializer to be used to convert the body of a message into bytes for transmission
to the destination topic. In the example above, "OpenEdge.Messaging.StringSerializer" is specified. For more
information on serialization, see Producer serialization.
Build the producer
|
Build() causes the producer builder to create
a producer. At this point, the producer is completely configured.
Create a new record builder
|
You use the producer to request a record builder. This allows a producer to initialize the record builder with defaults and allows the producer to produce a record with extra features that are not part of the default implementation.
Set the topic name and message body
Once a record builder is created, you assign a topic and a message body. Both
are required. Use SetTopicName() to set the topic
name and SetBody() to set the message body.
|
The same record builder is normally used to send many messages to the same topic.
Build the record
|
Once the record builder is set, call the Build() method to generate the record..
Records are usually specific to a particular producer, but could also be used by other producers, depending on the needs of the application.
Send the record to the producer
|
The next step is to send the record to the producer. Call Send(), with the built record as an argument, on the
producer. The producer is responsible for the message from this point on. To capture
the response, use an ISendResponse object to hold
the response.
See Behavior of send response properties, for more information on the properties and behavior of the response object.
You can call Flush() to flush the
queue to ensure the message is sent in a timely manner. The value supplied to
Flush() is the number of milliseconds to wait
for all outstanding messages to be sent. Flush() returns TRUE if
all messages in the producer queue are delivered and returns FALSE if it does not
finish due to the specified timeout.
Wait for the response
|
Use a loop to wait until the delivery response is received and the response object properties have been populated.
Handle any errors
|
Check the Success property on the response
object for any failures and handle them appropriately. See Exception handling for messaging errors for more information on handling
errors.
Clean up
|
You can delete the producer when finished with it, as shown in the example above, but this is not required. If not explicitly deleted, the object will be garbage collected.
Complete example
The following code contains the complete example:
|