Technical work, should be praised and then see, form a habitCopy the code

RocketMQ use tutorial related series of directories

DefaultMQProducer


Class introduction

public class DefaultMQProducer extends ClientConfig implements MQProducer

The DefaultMQProducer class is an entry point that applications use to deliver messages. Out of the box, it is possible to quickly create a producer using a no-argument constructor. It is mainly responsible for sending messages and supports synchronous, asynchronous, and Oneway sending modes, all of which support batch sending. The class provides getter/setter methods to adjust the parameters of the sender. DefaultMQProducer provides multiple send methods. Each send method is slightly different. Before using DefaultMQProducer, understand its intention in detail. Chapter 3: Teaching the wife how to be a producer and consumer of ordinary messages (synchronous, asynchronous and unidirectional)

public class Producer {
    public static void main(String[] args) throws MQClientException {
        // Create a producer with the specified group name
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");

        // Start the producer
        producer.start();

        for (int i = 0; i < 128; i++)
            try {
            	// Build the message
                Message msg = new Message("TopicTest"."TagA"."OrderID188"."Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));

                // Synchronize sending
                SendResult sendResult = producer.send(msg);

                // Prints the sending result
                System.out.printf("%s%n", sendResult);
            } catch(Exception e) { e.printStackTrace(); } producer.shutdown(); }}Copy the code

Note: This class is thread-safe. After configuration and startup, it can be shared safely between multiple threads.

Field in this paper,

type The field names describe
DefaultMQProducerImpl defaultMQProducerImpl An internal default implementation of the producer
String producerGroup Producer grouping
String createTopicKey Topics that do not exist on the server are automatically created when messages are sent
int defaultTopicQueueNums The default number of queues when creating a topic
int sendMsgTimeout Timeout period for sending messages
int compressMsgBodyOverHowmuch Threshold for compressing the message body
int retryTimesWhenSendFailed Maximum number of internal attempts to send messages in synchronous mode
int retryTimesWhenSendAsyncFailed Maximum number of internal attempts to send messages in asynchronous mode
boolean retryAnotherBrokerWhenNotStoreOK Whether to retry another broker if internal sending fails
int maxMessageSize The maximum length of a message
TraceDispatcher traceDispatcher Message trackers. Use rcpHook to trace messages

Summary of construction methods

Method names Methods described
DefaultMQProducer() Creates a producer from the default parameter values
DefaultMQProducer(final String producerGroup) Creates a producer with the specified group name
DefaultMQProducer(final String producerGroup, boolean enableMsgTrace) Creates a producer with the specified group name and sets whether message tracing is turned on
DefaultMQProducer(final String producerGroup, boolean enableMsgTrace, final String customizedTraceTopic) Create a producer with the specified group name and set whether message tracing is enabled and the name of the tracing topic
DefaultMQProducer(RPCHook rpcHook) Creates a producer using the specified hook
DefaultMQProducer(final String producerGroup, RPCHook rpcHook) Creates a producer with the specified group name and custom hook
DefaultMQProducer(final String producerGroup, RPCHook rpcHook, boolean enableMsgTrace,final String customizedTraceTopic) Create a producer with the specified group name and custom hook, and set whether message tracing is enabled and the name of the tracing topic

Summary of usage

The return value Method names Methods described
void createTopic(String key, String newTopic, int queueNum) Create the specified topic on the broker
void createTopic(String key, String newTopic, int queueNum, int topicSysFlag) Create the specified topic on the broker
long earliestMsgStoreTime(MessageQueue mq) Example Query the earliest message storage time
List fetchPublishMessageQueues(String topic) Gets the message queue for the topic
long maxOffset(MessageQueue mq) Query the maximum offset of a given message queue
long minOffset(MessageQueue mq) Query the minimum offset of a given message queue
QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end) Query messages by keyword
long searchOffset(MessageQueue mq, long timestamp) Finds the physical offset of the message queue at the specified time
SendResult send(Collection msgs) Synchronously send messages in batches
SendResult send(Collection msgs, long timeout) Synchronously send messages in batches
SendResult send(Collection msgs, MessageQueue messageQueue) Sends messages in batches synchronously to the specified message queue
SendResult send(Collection msgs, MessageQueue messageQueue, long timeout) Sends messages in batches synchronously to the specified message queue and specifies a timeout
SendResult send(Message msg) Send a single message synchronously
SendResult send(Message msg, long timeout) Sends a single message synchronously and specifies the timeout period
SendResult send(Message msg, MessageQueue mq) Sends a single message synchronously to the specified message queue
SendResult send(Message msg, MessageQueue mq, long timeout) Synchronizes a single send message to the specified message queue and specifies a timeout
void send(Message msg, MessageQueue mq, SendCallback sendCallback) Sends a single message asynchronously to the specified message queue and specifies the callback method
void send(Message msg, MessageQueue mq, SendCallback sendCallback, long timeout) Sends a single message asynchronously to the specified message queue, specifying the callback method and timeout
SendResult send(Message msg, MessageQueueSelector selector, Object arg) Synchronizes a single send message to a message queue and specifies a send queue selector
SendResult send(Message msg, MessageQueueSelector selector, Object arg, long timeout) Synchronizes a single send message to a message queue and specifies a send queue selector and a timeout
void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback) Sends a single message asynchronously to the specified message queue
void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback, long timeout) Sends a single message asynchronously to the specified message queue and specifies a timeout period
void send(Message msg, SendCallback sendCallback) Sending messages asynchronously
void send(Message msg, SendCallback sendCallback, long timeout) Send the message asynchronously and specify the callback method and timeout
TransactionSendResult sendMessageInTransaction(Message msg, LocalTransactionExecuter tranExecuter, final Object arg) Sends a transaction message and specifies a locally executed transaction instance
TransactionSendResult sendMessageInTransaction(Message msg, Object arg) Sending transaction messages
void sendOneway(Message msg) Sends messages one way without waiting for the broker to respond
void sendOneway(Message msg, MessageQueue mq) Sends messages unidirectionally to a specified queue without waiting for a broker response
void sendOneway(Message msg, MessageQueueSelector selector, Object arg) Sends messages unidirectionally to the selected queue of the queue selector without waiting for a broker response
void shutdown() Close the current producer instance and release the associated resources
void start() Start producer
MessageExt viewMessage(String offsetMsgId) Query messages based on the given msgId
MessageExt public MessageExt viewMessage(String topic, String msgId) Query messages based on the given msgId and specify a topic

Field details

  • producerGroup

    private String producerGroup

    The group name of the producer. The same group name indicates that the producer instances belong conceptually to the same group. This is important for transaction messages. If the original producer crashes after the transaction, the broker can contact different instances of producers in the same group to commit or roll back the transaction.

    Default value: DEFAULT_PRODUCER

    Note: by the Numbers, letters, underscores, shaft (-), a vertical bar (|) or percent composition; Cannot be empty; The length cannot exceed 255.

  • defaultMQProducerImpl

    protected final transient DefaultMQProducerImpl defaultMQProducerImpl

    The internal default implementation of the producer, which is automatically initialized internally when the producer is constructed, provides the internal implementation of most methods.

  • createTopicKey

    private String createTopicKey = MixAll.AUTO_CREATE_TOPIC_KEY_TOPIC

    When a message is sent, a topic that does not exist on the server is automatically created. You need to specify a Key that can be used to configure the default route for the topic from which the message is sent.

    Default value: TBW102

    Suggestion: Use this function for test or demo. Do not enable automatic creation and configuration in the production environment.

  • defaultTopicQueueNums

    private volatile int defaultTopicQueueNums = 4

    The default number of queues when creating a topic.

    Default value: 4

  • sendMsgTimeout

    private int sendMsgTimeout = 3000

    The timeout period for sending messages.

    Default value: 3000 milliseconds

    Suggestion: It is not recommended to change this value. The value should be consistent with sendTimeout in the Broker configuration. Sending timeout can be changed temporarily.

  • compressMsgBodyOverHowmuch

    private int compressMsgBodyOverHowmuch = 1024 * 4

    Compressed message body threshold. Message bodies larger than 4K are compressed by default.

    Default value: 1024 x 4 (unit: byte

    Suggestion: through DefaultMQProducerImpl setZipCompressLevel method set compression rate (defaults to 5, optional range [0, 9]); Through DefaultMQProducerImpl. TryToCompressMessage method to test the compressLevel and compressMsgBodyOverHowmuch optimal value.

  • retryTimesWhenSendFailed

    private int retryTimesWhenSendFailed = 2

    In synchronous mode, the maximum number of internal attempts to resend a message before sending back failed.

    Default value: 2. By default, a message can be delivered for a maximum of three times.

    Note: In extreme cases, this can lead to duplicate messages.

  • retryTimesWhenSendAsyncFailed

    private int retryTimesWhenSendAsyncFailed = 2

    In asynchronous mode, the maximum number of internal attempts to resend a message before sending a message fails.

    Default value: 2. By default, a message can be delivered for a maximum of three times.

    Note: In extreme cases, this can lead to duplicate messages.

  • retryAnotherBrokerWhenNotStoreOK

    private boolean retryAnotherBrokerWhenNotStoreOK = false

    Whether to retry other brokers if message saving fails in synchronous mode.

    Default value: false

    Note: When this configuration is turned off, the retryTimesWhenSendFailed configuration will be ignored in the case of non-post exceptions.

  • maxMessageSize

    private int maxMessageSize = 1024 * 1024 * 4

    The maximum size of a message. The message fails to be sent when the number of bytes exceeds maxMessageSize.

    Default value: 1024 x 1024 x 4 (unit: byte

  • traceDispatcher

    private TraceDispatcher traceDispatcher = null

    After message tracing is enabled, this class records messages consumed by message producers, message storing brokers, and consumers as links through hooks. When constructing a producer, the constructor parameter enableMsgTrace determines whether to create the object.

Constructor details

  1. DefaultMQProducer

    public DefaultMQProducer()

    Create a new producer.

  2. DefaultMQProducer

    DefaultMQProducer(final String producerGroup)

    Creates a producer with the specified group name.

    • Input parameter description:

      type Whether must The default value describe
      ucerGroup String is DEFAULT_PRODUCER
  3. DefaultMQProducer

    DefaultMQProducer(final String producerGroup, boolean enableMsgTrace)

    Creates a producer with the specified group name and sets whether message tracing is turned on.

    • Input parameter description:

      type Whether must The default value describe
      ucerGroup String is DEFAULT_PRODUCER
      leMsgTrace boolean is false
  4. DefaultMQProducer

    DefaultMQProducer(final String producerGroup, boolean enableMsgTrace, final String customizedTraceTopic)

    Create a producer with the specified group name and set whether message tracing is enabled and the name of the tracing topic.

    • Input parameter description:

      type Whether must The default value describe
      ucerGroup String is DEFAULT_PRODUCER
      ook RPCHook no null
      leMsgTrace boolean is false
      omizedTraceTopic String no RMQ_SYS_TRACE_TOPIC
  5. DefaultMQProducer

    DefaultMQProducer(RPCHook rpcHook)

    Creates a producer using the specified hook.

    • Input parameter description:

      type Whether must The default value describe
      ook RPCHook no null
  6. DefaultMQProducer

    DefaultMQProducer(final String producerGroup, RPCHook rpcHook)

    Creates a producer with the specified group name and custom hook.

    • Input parameter description:

      type Whether must The default value describe
      ucerGroup String is DEFAULT_PRODUCER
      ook RPCHook no null
  7. DefaultMQProducer

    DefaultMQProducer(final String producerGroup, RPCHook rpcHook, boolean enableMsgTrace,final String customizedTraceTopic)

    Create a producer with the specified group name and custom hook, and set whether message tracing is enabled and the name of the tracing topic.

    • Input parameter description:
    The name type Whether must The default value describe
    oducerGroup String is DEFAULT_PRODUCER The group name of the producer
    cHook RPCHook no null RpcHook is called back after each remote command is executed
    ableMsgTrace boolean is false Whether to enable message tracing
    stomizedTraceTopic String no RMQ_SYS_TRACE_TOPIC The name of the message tracing topic

Usage details

  1. createTopic

    public void createTopic(String key, String newTopic, int queueNum)

    Create a topic on the broker.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      String is Access key.
      opic String is
      eNum int is 0 (0, maxIntValue]
    • Return value description:

      void

    • Exception description:

      MQClientException – The producer state is not Running; No client exception such as broker was found.

  2. createTopic

    public void createTopic(String key, String newTopic, int queueNum, int topicSysFlag)

    Create a topic on the broker.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      String is Access key.
      opic String is
      eNum int is 0 (0, maxIntValue]
      cSysFlag int is 0
    • Return value description:

      void

    • Exception description:

      MQClientException – The producer state is not Running; No client exception such as broker was found.

  3. earliestMsgStoreTime

    public long earliestMsgStoreTime(MessageQueue mq)

    Example Query the earliest message storage time.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      MessageQueue is The message queue to query
    • Return value description:

      Specifies the earliest time a message is stored in the queue. Unit: millisecond.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; The network is abnormal. Client exceptions such as thread interruption.

  4. fetchPublishMessageQueues

    public List<MessageQueue> fetchPublishMessageQueues(String topic)

    Gets the message queue for the topic.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      c String is
    • Return value description:

      Incoming message queue under topic.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; The network is abnormal. Client exceptions such as thread interruption.

  5. maxOffset

    public long maxOffset(MessageQueue mq)

    Query the maximum physical offset of a message queue.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      MessageQueue is The message queue to query
    • Return value description:

      The maximum physical offset of a given message queue.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; The network is abnormal. Client exceptions such as thread interruption.

  6. minOffset

    public long minOffset(MessageQueue mq)

    Query the minimum physical offset for a given message queue.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      MessageQueue is The message queue to query
    • Return value description:

      The minimum physical offset for a given message queue.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; The network is abnormal. Client exceptions such as thread interruption.

  7. queryMessage

    public QueryResult queryMessage(String topic, String key, int maxNum, long begin, long end)

    Query messages by keyword.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      c String is
      String no null Find the keyword
      um int is
      n long is
      long is End time stamp, in milliseconds
    • Return value description:

      The set of messages queried.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; Network exceptions Client exceptions Client exceptions. InterruptedException – Thread interruption.

  8. searchOffset

    public long searchOffset(MessageQueue mq, long timestamp)

    Finds the physical offset of the message queue at the specified time.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      MessageQueue is The message queue to query.
      stamp long is
    • Return value description:

      The physical offset of the message queue at the specified time.

    • Exception description:

      MQClientException – The producer state is not Running; Broker not found; Broker returns failed; The network is abnormal. Client exceptions such as thread interruption.

  9. send

    public SendResult send(Collection<Message> msgs)

    Synchronously send messages in batches. The maximum number of internal attempts to resend a message before returning a send failure (see the retryTimesWhenSendFailed property). If the sending queue is not specified, the polling policy is adopted by default.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Collection is A collection of messages to be sent. The messages within the collection must belong to the same topic.
    • Return value description:

      Result of sending batch messages, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  10. send

    public SendResult send(Collection<Message> msgs, long timeout)

    Synchronous batch sends the message, if not completed within a specified timeout message delivery, throws RemotingTooMuchRequestException. The maximum number of internal attempts to resend a message before returning a send failure (see the retryTimesWhenSendFailed property). If the sending queue is not specified, the polling policy is adopted by default.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Collection is A collection of messages to be sent. The messages within the collection must belong to the same topic.
      out long is seesendMsgTimeoutattribute
    • Return value description:

      Result of sending batch messages, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  11. send

    public SendResult send(Collection<Message> msgs, MessageQueue messageQueue)

    Sends messages synchronously to a given queue in batches.

    Note: Specifying queues means that all messages are the same topic.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Collection is A collection of messages to be sent. The messages within the collection must belong to the same topic.
      ageQueue MessageQueue is
    • Return value description:

      Result of sending batch messages, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  12. send

    public SendResult send(Collection<Message> msgs, MessageQueue messageQueue, long timeout)

    Send message to a given queue synchronization in bulk, if not completed within a specified timeout message delivery, throws RemotingTooMuchRequestException.

    Note: Specifying queues means that all messages are the same topic.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Collection is A collection of messages to be sent. The messages within the collection must belong to the same topic.
      out long is seesendMsgTimeoutattribute
      ageQueue MessageQueue is
    • Return value description:

      Result of sending batch messages, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  13. send

    public SendResult send(Message msg)

    The message is sent in synchronous mode, and this method returns only when the sending process is complete. The maximum number of internal attempts to resend a message before returning a send failure (see the retryTimesWhenSendFailed property). If the sending queue is not specified, the polling policy is adopted by default.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  14. send

    public SendResult send(Message msg, long timeout)

    Send message to synchronous mode, if not completed within a specified timeout message delivery, throws RemotingTooMuchRequestException. This method returns only when the send process is complete. The maximum number of internal attempts to resend a message before returning a send failure (see the retryTimesWhenSendFailed property). If the sending queue is not specified, the polling policy is adopted by default.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      out long is seesendMsgTimeoutattribute
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  15. send

    public SendResult send(Message msg, MessageQueue mq)

    Sends a single message synchronously to the specified message queue. This method returns only when the send process is complete.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      MessageQueue is Queue of messages to be delivered.
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  16. send

    public SendResult send(Message msg, MessageQueue mq, long timeout)

    Send a single message to a specified message queue synchronization, if not completed within a specified timeout message delivery, throws RemotingTooMuchRequestException. This method returns only when the send process is complete.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      out long is seesendMsgTimeoutattribute
      MessageQueue is Queue of messages to be delivered. Specifying a queue means that the messages to be delivered are all the same topic.
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  17. send

    public void send(Message msg, MessageQueue mq, SendCallback sendCallback)

    Send a single message asynchronously to the specified message queue. Send a single message asynchronously and return sendCallback in case of success or exception. Therefore, sendCallback cannot be null when sending an asynchronous message; otherwise, NullPointerException will be thrown during the callback. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      MessageQueue is Queue of messages to be delivered. Specifying a queue means that the messages to be delivered are all the same topic.
      Callback SendCallback is
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  18. send

    public void send(Message msg, MessageQueue mq, SendCallback sendCallback, long timeout)

    Send a single message asynchronously to the specified message queue. Send a single message asynchronously and return sendCallback in case of success or exception. Therefore, sendCallback cannot be null when sending an asynchronous message; otherwise, NullPointerException will be thrown during the callback. If the message not sent successfully within a specified time, the callback method will receive RemotingTooMuchRequestException anomalies. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      MessageQueue is Queue of messages to be delivered.
      Callback SendCallback is
      out long is seesendMsgTimeoutattribute
    • The returned value is void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  19. send

    public SendResult send(Message msg, MessageQueueSelector selector, Object arg)

    Send messages synchronously to queues calculated by MessageQueueSelector.

    You can send a certain kind of message to a fixed queue through the self-implementing MessageQueueSelector interface. For example, post a status change message for the same order to a fixed queue.

    Note: This message failed to be sent internally and will not be retried.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      ctor MessageQueueSelector is
      Object no Parameter object to be used by the queue selector.
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  20. send

    public SendResult send(Message msg, MessageQueueSelector selector, Object arg, long timeout)

    Send messages synchronously to the queue calculated by MessageQueueSelector and specify a send timeout.

    You can send a certain kind of message to a fixed queue through the self-implementing MessageQueueSelector interface. For example, post a status change message for the same order to a fixed queue.

    Note: This message failed to be sent internally and will not be retried.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      ctor MessageQueueSelector is
      Object no Parameter object to be used by the queue selector.
      out long is seesendMsgTimeoutattribute
    • Return value description:

      Result of sending a message, including msgId, sending status, etc.

    • Exception description: MQclientexception-broker does not exist or not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. An error has occurred with mqBrokerException-broker. InterruptedException – The sending thread is interrupted. RemotingTooMuchRequestException – send timeout.

  21. send

    public void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback)

    Send a single message asynchronously to the queue calculated by MessageQueueSelector, send it asynchronously and return it directly, and call sendCallback in case of success or exception, so the sendCallback parameter cannot be null when sending it asynchronously. Otherwise a NullPointerException will be thrown during the callback. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    You can send a certain kind of message to a fixed queue through the self-implementing MessageQueueSelector interface. For example, post a status change message for the same order to a fixed queue.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      ctor MessageQueueSelector is
      Object no Parameter object to be used by the queue selector.
      Callback SendCallback is
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  22. send

    public void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback, long timeout)

    Send a single message asynchronously to the queue calculated by MessageQueueSelector, send it asynchronously and return it directly, and call sendCallback in case of success or exception, so the sendCallback parameter cannot be null when sending it asynchronously. Otherwise a NullPointerException will be thrown during the callback. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    You can send a certain kind of message to a fixed queue through the self-implementing MessageQueueSelector interface. For example, post a status change message for the same order to a fixed queue.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      ctor MessageQueueSelector is
      Object no Parameter object to be used by the queue selector.
      Callback SendCallback is
      out long is seesendMsgTimeoutattribute
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  23. send

    public void send(Message msg, SendCallback sendCallback)

    When sending a single message asynchronously, sendCallback will be returned directly and will be called back if the message is successfully sent or if an exception occurs. Therefore, the sendCallback parameter cannot be null in asynchronous sending; otherwise, NullPointerException will be thrown during the callback. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      Callback SendCallback is
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  24. send

    public void send(Message msg, SendCallback sendCallback, long timeout)

    When sending a single message asynchronously, sendCallback will be returned directly and will be called back if the message is successfully sent or if an exception occurs. Therefore, the sendCallback parameter cannot be null in asynchronous sending; otherwise, NullPointerException will be thrown during the callback. Asynchronous transmission, before send success, its internal will try to send a message the maximum times (see retryTimesWhenSendAsyncFailed properties).

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      Callback SendCallback is
      out long is seesendMsgTimeoutattribute
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  25. sendMessageInTransaction

    public TransactionSendResult sendMessageInTransaction(Message msg, LocalTransactionExecuter tranExecuter, final Object arg)

    Send a transaction message. This class is not implemented by default and throws a RuntimeException. See also: TransactionMQProducer class.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Transaction message to be delivered
      Executer LocalTransactionExecuter is
      Object is Parameter object used by the local transaction executor
    • Return value description:

      For transaction results, see the LocalTransactionState class.

    • Exception description:

      RuntimeException – Always throws this exception.

  26. sendMessageInTransaction

    public TransactionSendResult sendMessageInTransaction(Message msg, final Object arg)

    Send a transaction message. This class is not implemented by default and throws a RuntimeException. See also: TransactionMQProducer class.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Transaction message to be delivered
      Object is Parameter object used by the local transaction executor
    • Return value description:

      For transaction results, see the LocalTransactionState class.

    • Exception description:

      RuntimeException – Always throws this exception.

  27. sendOneway

    public void sendOneway(Message msg)

    As oneway messages are sent, the broker does not respond to any execution results, similar to UDP. It has maximum throughput but messages can be lost.

    This method can be used when the volume of messages is large, high throughput is sought, and message loss is allowed.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Message to be delivered
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  28. sendOneway

    public void sendOneway(Message msg, MessageQueue mq)

    Messages are sent oneway to a specified queue, and the broker does not respond to any execution results, similar to UDP. It has maximum throughput but messages can be lost.

    This method can be used when the volume of messages is large, high throughput is sought, and message loss is allowed.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Message to be delivered
      MessageQueue is Queue of messages to be delivered
    • The returned value is void

    • Exception description: MQclientexception-broker does not exist or not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  29. sendOneway

    public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)

    Messages are sent oneway to queues calculated via MessageQueueSelector. The broker does not respond to any execution results, similar to UDP. It has maximum throughput but messages can be lost.

    This method can be used when the volume of messages is large, high throughput is sought, and message loss is allowed.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      Message is Messages to be sent.
      ctor MessageQueueSelector is
      Object no Parameter object to be used by the queue selector.
    • Return value description:

      void

    • Exception description:

      Mqclientexception-broker does not exist or was not found; Namesrv address is empty; Client exceptions such as the routing information of the topic are not found. RemotingException – Network exception. InterruptedException – The sending thread is interrupted.

  30. shutdown

    public void shutdown()

    Close the current producer instance and release the associated resources.

    • Input parameter description:

      No.

    • Return value description:

      void

    • Exception description:

  31. start

    public void start()

    Start the producer instance. This method must be called before a message can be sent or queried. It performs a number of internal initializations, such as checking configuration, establishing a connection with NamesRV, starting a series of scheduled tasks such as heartbeats, and so on.

    • Input parameter description:

      No.

    • Return value description:

      void

    • Exception description:

      MQClientException – Failed during initialization.

  32. viewMessage

    public MessageExt viewMessage(String offsetMsgId)

    Query messages based on the given msgId.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      etMsgId String is
    • Return value description:

      Return MessageExt with topic name, message title, message ID, consumption count, producer host, etc.

    • Exception description:

      RemotingException – An error occurred at the network layer. An error has occurred with mqBrokerException-broker. InterruptedException – The thread is interrupted. MQClientException – The producer state is not Running; MsgId illegal etc.

  33. viewMessage

    public MessageExt viewMessage(String topic, String msgId)

    Query messages based on the given msgId and specify a topic.

    • Input parameter description:

      type Whether must The default value Range of values instructions
      d String is
      c String is
    • Return value description:

      Return MessageExt with topic name, message title, message ID, consumption count, producer host, etc.

    • Exception description:

      RemotingException – An error occurred at the network layer. An error has occurred with mqBrokerException-broker. InterruptedException – The thread is interrupted. MQClientException – The producer state is not Running; MsgId illegal etc.

Reference: github.com/apache/rock…