public class DefaultMQProducer extends ClientConfig implements MQProducer
send
methods to deliver messages to brokers. Each of them has pros and
cons; you'd better understand strengths and weakness of them before actually coding.
Thread Safety: After configuring and starting process, this class can be regarded as thread-safe and used among multiple threads context.
Modifier and Type | Field and Description |
---|---|
protected DefaultMQProducerImpl |
defaultMQProducerImpl
Wrapping internal implementations for virtually all methods presented in this class.
|
SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY
Constructor and Description |
---|
DefaultMQProducer()
Default constructor.
|
DefaultMQProducer(RPCHook rpcHook)
Constructor specifying the RPC hook.
|
DefaultMQProducer(java.lang.String producerGroup)
Constructor specifying producer group.
|
DefaultMQProducer(java.lang.String producerGroup,
RPCHook rpcHook)
Constructor specifying both producer group and RPC hook.
|
Modifier and Type | Method and Description |
---|---|
void |
createTopic(java.lang.String key,
java.lang.String newTopic,
int queueNum)
Create a topic on broker.
|
void |
createTopic(java.lang.String key,
java.lang.String newTopic,
int queueNum,
int topicSysFlag)
Create a topic on broker.
|
long |
earliestMsgStoreTime(MessageQueue mq)
Query earliest message store time.
|
java.util.List<MessageQueue> |
fetchPublishMessageQueues(java.lang.String topic)
Fetch message queues of topic
topic , to which we may send/publish messages. |
int |
getCompressMsgBodyOverHowmuch() |
java.lang.String |
getCreateTopicKey() |
DefaultMQProducerImpl |
getDefaultMQProducerImpl() |
int |
getDefaultTopicQueueNums() |
long[] |
getLatencyMax() |
int |
getMaxMessageSize() |
long[] |
getNotAvailableDuration() |
java.lang.String |
getProducerGroup() |
int |
getRetryTimesWhenSendAsyncFailed() |
int |
getRetryTimesWhenSendFailed() |
int |
getSendMsgTimeout() |
boolean |
isRetryAnotherBrokerWhenNotStoreOK() |
boolean |
isSendLatencyFaultEnable() |
boolean |
isSendMessageWithVIPChannel() |
long |
maxOffset(MessageQueue mq)
Query maximum offset of the given message queue.
|
long |
minOffset(MessageQueue mq)
Query minimum offset of the given message queue.
|
QueryResult |
queryMessage(java.lang.String topic,
java.lang.String key,
int maxNum,
long begin,
long end)
Query message by key.
|
long |
searchOffset(MessageQueue mq,
long timestamp)
Search consume queue offset of the given time stamp.
|
SendResult |
send(java.util.Collection<Message> msgs) |
SendResult |
send(java.util.Collection<Message> msgs,
long timeout) |
SendResult |
send(java.util.Collection<Message> msgs,
MessageQueue messageQueue) |
SendResult |
send(java.util.Collection<Message> msgs,
MessageQueue messageQueue,
long timeout) |
SendResult |
send(Message msg)
Send message in synchronous mode.
|
SendResult |
send(Message msg,
long timeout)
Same to
send(Message) with send timeout specified in addition. |
SendResult |
send(Message msg,
MessageQueue mq)
Same to
send(Message) with target message queue specified in addition. |
SendResult |
send(Message msg,
MessageQueue mq,
long timeout)
Same to
send(Message) with target message queue and send timeout specified. |
SendResult |
send(Message msg,
MessageQueueSelector selector,
java.lang.Object arg)
Same to
send(Message) with message queue selector specified. |
SendResult |
send(Message msg,
MessageQueueSelector selector,
java.lang.Object arg,
long timeout)
Same to
send(Message, MessageQueueSelector, Object) with send timeout specified. |
void |
send(Message msg,
MessageQueueSelector selector,
java.lang.Object arg,
SendCallback sendCallback)
Same to
send(Message, SendCallback) with message queue selector specified. |
void |
send(Message msg,
MessageQueueSelector selector,
java.lang.Object arg,
SendCallback sendCallback,
long timeout)
Same to
send(Message, MessageQueueSelector, Object, SendCallback) with timeout specified. |
void |
send(Message msg,
MessageQueue mq,
SendCallback sendCallback)
Same to
send(Message, SendCallback) with target message queue specified. |
void |
send(Message msg,
MessageQueue mq,
SendCallback sendCallback,
long timeout)
Same to
send(Message, SendCallback) with target message queue and send timeout specified. |
void |
send(Message msg,
SendCallback sendCallback)
Send message to broker asynchronously.
|
void |
send(Message msg,
SendCallback sendCallback,
long timeout)
Same to
send(Message, SendCallback) with send timeout specified in addition. |
TransactionSendResult |
sendMessageInTransaction(Message msg,
LocalTransactionExecuter tranExecuter,
java.lang.Object arg)
This method is to send transactional messages.
|
void |
sendOneway(Message msg)
Similar to UDP, this method won't wait for
acknowledgement from broker before return.
|
void |
sendOneway(Message msg,
MessageQueue mq)
Same to
sendOneway(Message) with target message queue specified. |
void |
sendOneway(Message msg,
MessageQueueSelector selector,
java.lang.Object arg)
Same to
sendOneway(Message) with message queue selector specified. |
void |
setCompressMsgBodyOverHowmuch(int compressMsgBodyOverHowmuch) |
void |
setCreateTopicKey(java.lang.String createTopicKey) |
void |
setDefaultTopicQueueNums(int defaultTopicQueueNums) |
void |
setLatencyMax(long[] latencyMax) |
void |
setMaxMessageSize(int maxMessageSize) |
void |
setNotAvailableDuration(long[] notAvailableDuration) |
void |
setProducerGroup(java.lang.String producerGroup) |
void |
setRetryAnotherBrokerWhenNotStoreOK(boolean retryAnotherBrokerWhenNotStoreOK) |
void |
setRetryTimesWhenSendAsyncFailed(int retryTimesWhenSendAsyncFailed) |
void |
setRetryTimesWhenSendFailed(int retryTimesWhenSendFailed) |
void |
setSendLatencyFaultEnable(boolean sendLatencyFaultEnable) |
void |
setSendMessageWithVIPChannel(boolean sendMessageWithVIPChannel) |
void |
setSendMsgTimeout(int sendMsgTimeout) |
void |
shutdown()
This method shuts down this producer instance and releases related resources.
|
void |
start()
Start this producer instance.
|
MessageExt |
viewMessage(java.lang.String offsetMsgId)
Query message of the given offset message ID.
|
MessageExt |
viewMessage(java.lang.String topic,
java.lang.String msgId)
Query message of the given message ID.
|
buildMQClientId, changeInstanceNameToPID, cloneClientConfig, getClientCallbackExecutorThreads, getClientIP, getHeartbeatBrokerInterval, getInstanceName, getNamesrvAddr, getPersistConsumerOffsetInterval, getPollNameServerInterval, getUnitName, isUnitMode, isVipChannelEnabled, resetClientConfig, setClientCallbackExecutorThreads, setClientIP, setHeartbeatBrokerInterval, setInstanceName, setNamesrvAddr, setPersistConsumerOffsetInterval, setPollNameServerInterval, setUnitMode, setUnitName, setVipChannelEnabled, toString
protected final transient DefaultMQProducerImpl defaultMQProducerImpl
public DefaultMQProducer()
public DefaultMQProducer(java.lang.String producerGroup, RPCHook rpcHook)
producerGroup
- Producer group, see the name-sake field.rpcHook
- RPC hook to execute per each remoting command execution.public DefaultMQProducer(java.lang.String producerGroup)
producerGroup
- Producer group, see the name-sake field.public DefaultMQProducer(RPCHook rpcHook)
rpcHook
- RPC hook to execute per each remoting command execution.public void start() throws MQClientException
start
in interface MQProducer
MQClientException
- if there is any unexpected error.public void shutdown()
shutdown
in interface MQProducer
public java.util.List<MessageQueue> fetchPublishMessageQueues(java.lang.String topic) throws MQClientException
topic
, to which we may send/publish messages.fetchPublishMessageQueues
in interface MQProducer
topic
- Topic to fetch.MQClientException
- if there is any client error.public SendResult send(Message msg) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
retryTimesWhenSendFailed
times before claiming failure. As a result, multiple messages may potentially
delivered to broker(s). It's up to the application developers to resolve potential duplication issue.send
in interface MQProducer
msg
- Message to send.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(Message msg, long timeout) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send(Message)
with send timeout specified in addition.send
in interface MQProducer
msg
- Message to send.timeout
- send timeout.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, SendCallback sendCallback) throws MQClientException, RemotingException, java.lang.InterruptedException
sendCallback
will be executed.
Similar to send(Message)
, internal implementation would potentially retry up to
retryTimesWhenSendAsyncFailed
times before claiming sending failure, which may yield message duplication
and application developers are the one to resolve this potential issue.send
in interface MQProducer
msg
- Message to send.sendCallback
- Callback to execute on sending completed, either successful or unsuccessful.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, SendCallback sendCallback, long timeout) throws MQClientException, RemotingException, java.lang.InterruptedException
send(Message, SendCallback)
with send timeout specified in addition.send
in interface MQProducer
msg
- message to send.sendCallback
- Callback to execute.timeout
- send timeout.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void sendOneway(Message msg) throws MQClientException, RemotingException, java.lang.InterruptedException
sendOneway
in interface MQProducer
msg
- Message to send.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(Message msg, MessageQueue mq) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send(Message)
with target message queue specified in addition.send
in interface MQProducer
msg
- Message to send.mq
- Target message queue.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(Message msg, MessageQueue mq, long timeout) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send(Message)
with target message queue and send timeout specified.send
in interface MQProducer
msg
- Message to send.mq
- Target message queue.timeout
- send timeout.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, MessageQueue mq, SendCallback sendCallback) throws MQClientException, RemotingException, java.lang.InterruptedException
send(Message, SendCallback)
with target message queue specified.send
in interface MQProducer
msg
- Message to send.mq
- Target message queue.sendCallback
- Callback to execute on sending completed, either successful or unsuccessful.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, MessageQueue mq, SendCallback sendCallback, long timeout) throws MQClientException, RemotingException, java.lang.InterruptedException
send(Message, SendCallback)
with target message queue and send timeout specified.send
in interface MQProducer
msg
- Message to send.mq
- Target message queue.sendCallback
- Callback to execute on sending completed, either successful or unsuccessful.timeout
- Send timeout.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void sendOneway(Message msg, MessageQueue mq) throws MQClientException, RemotingException, java.lang.InterruptedException
sendOneway(Message)
with target message queue specified.sendOneway
in interface MQProducer
msg
- Message to send.mq
- Target message queue.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(Message msg, MessageQueueSelector selector, java.lang.Object arg) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send(Message)
with message queue selector specified.send
in interface MQProducer
msg
- Message to send.selector
- Message queue selector, through which we get target message queue to deliver message to.arg
- Argument to work along with message queue selector.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(Message msg, MessageQueueSelector selector, java.lang.Object arg, long timeout) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send(Message, MessageQueueSelector, Object)
with send timeout specified.send
in interface MQProducer
msg
- Message to send.selector
- Message queue selector, through which we get target message queue to deliver message to.arg
- Argument to work along with message queue selector.timeout
- Send timeout.SendResult
instance to inform senders details of the deliverable, say Message ID of the message,
SendStatus
indicating broker storage/replication status, message queue sent to, etc.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.MQBrokerException
- if there is any error with broker.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, MessageQueueSelector selector, java.lang.Object arg, SendCallback sendCallback) throws MQClientException, RemotingException, java.lang.InterruptedException
send(Message, SendCallback)
with message queue selector specified.send
in interface MQProducer
msg
- Message to send.selector
- Message selector through which to get target message queue.arg
- Argument used along with message queue selector.sendCallback
- callback to execute on sending completion.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void send(Message msg, MessageQueueSelector selector, java.lang.Object arg, SendCallback sendCallback, long timeout) throws MQClientException, RemotingException, java.lang.InterruptedException
send(Message, MessageQueueSelector, Object, SendCallback)
with timeout specified.send
in interface MQProducer
msg
- Message to send.selector
- Message selector through which to get target message queue.arg
- Argument used along with message queue selector.sendCallback
- callback to execute on sending completion.timeout
- Send timeout.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public void sendOneway(Message msg, MessageQueueSelector selector, java.lang.Object arg) throws MQClientException, RemotingException, java.lang.InterruptedException
sendOneway(Message)
with message queue selector specified.sendOneway
in interface MQProducer
msg
- Message to send.selector
- Message queue selector, through which to determine target message queue to deliver messagearg
- Argument used along with message queue selector.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public TransactionSendResult sendMessageInTransaction(Message msg, LocalTransactionExecuter tranExecuter, java.lang.Object arg) throws MQClientException
sendMessageInTransaction
in interface MQProducer
msg
- Transactional message to send.tranExecuter
- local transaction executor.arg
- Argument used along with local transaction executor.MQClientException
- if there is any client error.public void createTopic(java.lang.String key, java.lang.String newTopic, int queueNum) throws MQClientException
createTopic
in interface MQAdmin
key
- accesskeynewTopic
- topic namequeueNum
- topic's queue numberMQClientException
- if there is any client error.public void createTopic(java.lang.String key, java.lang.String newTopic, int queueNum, int topicSysFlag) throws MQClientException
createTopic
in interface MQAdmin
key
- accesskeynewTopic
- topic namequeueNum
- topic's queue numbertopicSysFlag
- topic system flagMQClientException
- if there is any client error.public long searchOffset(MessageQueue mq, long timestamp) throws MQClientException
searchOffset
in interface MQAdmin
mq
- Instance of MessageQueuetimestamp
- from when in milliseconds.MQClientException
- if there is any client error.public long maxOffset(MessageQueue mq) throws MQClientException
maxOffset
in interface MQAdmin
mq
- Instance of MessageQueueMQClientException
- if there is any client error.public long minOffset(MessageQueue mq) throws MQClientException
minOffset
in interface MQAdmin
mq
- Instance of MessageQueueMQClientException
- if there is any client error.public long earliestMsgStoreTime(MessageQueue mq) throws MQClientException
earliestMsgStoreTime
in interface MQAdmin
mq
- Instance of MessageQueueMQClientException
- if there is any client error.public MessageExt viewMessage(java.lang.String offsetMsgId) throws RemotingException, MQBrokerException, java.lang.InterruptedException, MQClientException
viewMessage
in interface MQAdmin
offsetMsgId
- message idMQBrokerException
- if there is any broker error.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public QueryResult queryMessage(java.lang.String topic, java.lang.String key, int maxNum, long begin, long end) throws MQClientException, java.lang.InterruptedException
queryMessage
in interface MQAdmin
topic
- message topickey
- message key index wordmaxNum
- max message numberbegin
- from whenend
- to whenMQClientException
- if there is any client error.java.lang.InterruptedException
- if the thread is interrupted.public MessageExt viewMessage(java.lang.String topic, java.lang.String msgId) throws RemotingException, MQBrokerException, java.lang.InterruptedException, MQClientException
viewMessage
in interface MQAdmin
topic
- TopicmsgId
- Message IDMQBrokerException
- if there is any broker error.MQClientException
- if there is any client error.RemotingException
- if there is any network-tier error.java.lang.InterruptedException
- if the sending thread is interrupted.public SendResult send(java.util.Collection<Message> msgs) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send
in interface MQProducer
MQClientException
RemotingException
MQBrokerException
java.lang.InterruptedException
public SendResult send(java.util.Collection<Message> msgs, long timeout) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send
in interface MQProducer
MQClientException
RemotingException
MQBrokerException
java.lang.InterruptedException
public SendResult send(java.util.Collection<Message> msgs, MessageQueue messageQueue) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send
in interface MQProducer
MQClientException
RemotingException
MQBrokerException
java.lang.InterruptedException
public SendResult send(java.util.Collection<Message> msgs, MessageQueue messageQueue, long timeout) throws MQClientException, RemotingException, MQBrokerException, java.lang.InterruptedException
send
in interface MQProducer
MQClientException
RemotingException
MQBrokerException
java.lang.InterruptedException
public java.lang.String getProducerGroup()
public void setProducerGroup(java.lang.String producerGroup)
public java.lang.String getCreateTopicKey()
public void setCreateTopicKey(java.lang.String createTopicKey)
public int getSendMsgTimeout()
public void setSendMsgTimeout(int sendMsgTimeout)
public int getCompressMsgBodyOverHowmuch()
public void setCompressMsgBodyOverHowmuch(int compressMsgBodyOverHowmuch)
public DefaultMQProducerImpl getDefaultMQProducerImpl()
public boolean isRetryAnotherBrokerWhenNotStoreOK()
public void setRetryAnotherBrokerWhenNotStoreOK(boolean retryAnotherBrokerWhenNotStoreOK)
public int getMaxMessageSize()
public void setMaxMessageSize(int maxMessageSize)
public int getDefaultTopicQueueNums()
public void setDefaultTopicQueueNums(int defaultTopicQueueNums)
public int getRetryTimesWhenSendFailed()
public void setRetryTimesWhenSendFailed(int retryTimesWhenSendFailed)
public boolean isSendMessageWithVIPChannel()
public void setSendMessageWithVIPChannel(boolean sendMessageWithVIPChannel)
public long[] getNotAvailableDuration()
public void setNotAvailableDuration(long[] notAvailableDuration)
public long[] getLatencyMax()
public void setLatencyMax(long[] latencyMax)
public boolean isSendLatencyFaultEnable()
public void setSendLatencyFaultEnable(boolean sendLatencyFaultEnable)
public int getRetryTimesWhenSendAsyncFailed()
public void setRetryTimesWhenSendAsyncFailed(int retryTimesWhenSendAsyncFailed)