This topic describes the concept of producers in ApsaraMQ for RocketMQ. It also describes the role of producers in the messaging model, producer attributes and compatibility, and some usage notes of working with producers.
Definition
A producer in ApsaraMQ for RocketMQ is a functional messaging entity that creates messages and sends them to the server.
A producer is typically integrated on the business system and serves to encapsulate data as messages in ApsaraMQ for RocketMQ and send the messages to the server. For more information about messages, see Messages.
The following message delivery elements are defined on the producer side:
Transmission mode: A producer can specify the message transmission mode in an API operation. ApsaraMQ for RocketMQ supports synchronous transmission and asynchronous transmission. For more information, see Models.
Transactional behavior: ApsaraMQ for RocketMQ supports transactional messages. Producers are involved in transactional checks to ensure eventual consistency of transactions. For more information, see Transactional messages.
Producers and topics have a many-to-many relationship. A producer can send messages to multiple topics, and a topic can receive messages from multiple producers. This many-to-many relationship facilitates performance scaling and disaster recovery.
Model relationship
The following figure shows the role of producers in the messaging model of ApsaraMQ for RocketMQ.
- Producers produce and send messages to the ApsaraMQ for RocketMQ broker.
- The ApsaraMQ for RocketMQ broker stores the messages in the queue that is specified by the topic in the order in which the messages are received.
- Consumers obtain and consume messages from the ApsaraMQ for RocketMQ broker based on the specified subscriptions.
Internal attributes
- Endpoint (required): the address that is used by a client to access the ApsaraMQ forRocketMQ broker. The endpoint is used to identify the cluster on the broker.
You must configure an endpoint in the specified format. We recommend that you use a domain name instead of an IP address to prevent hotspots from failing to be migrated during node changes.
- Identity authentication (optional): the credentials that are used to verify the identity of the client.
This parameter is required only when the identity authentication feature is enabled on the broker.
- Request timeout period (optional): the timeout period for client requests. For information about the valid values of the request timeout period parameter, see Limits on parameters.
A delivery retry policy specifies how a producer retries the delivery of messages upon a failed message delivery attempt. For more information, see Message sending retry.
Compatibility
Starting from ApsaraMQ for RocketMQ version 5.x, producers are anonymous, and producer groups are discontinued. For Message Queue for Apache RocketMQ version 3.x and version 4.x, existing producer groups can be discontinued, without affecting your business.
Usage notes
We recommend that you limit the number of producers on individual processes.
In ApsaraMQ for RocketMQ, producers and topics provide a many-to-many form of communication. A single producer can send messages to multiple topics. We recommend that you create and initialize the minimum number of producers that your business scenarios require, and reuse as many producers as you can. For example, in a scenario that requires message delivery to multiple topics, you do not need to create a producer for each topic.
We recommend that you do not create and destroy producers on a regular basis.
The producers of ApsaraMQ for RocketMQ are underlying resources that can be reused, like the connection pool of a database. You do not need to create producers each time you send messages or destroy the producers after you send messages. If you regularly create and destroy producers, a large number of short connection requests are generated on the broker. This imposes a high level of load on your system.
Example of correct usage
Producer p = ProducerBuilder.build(); for (int i =0;i<n;i++) { Message m= MessageBuilder.build(); p.send(m); } p.shutdown();
Example of incorrect usage
for (int i =0;i<n;i++) { Producer p = ProducerBuilder.build(); Message m= MessageBuilder.build(); p.send(m); p.shutdown(); }