what is the rightway to use producer send async messages
- Please describe the issue you observed:
- What did you do (The steps to reproduce)? I use producer to send async messages in a loop, there is only one thread call producer async send, the code is as follows: while(true){ producer.send(msg,callback,30000L) }
for a while, the following exceptions occured:
org.apache.rocketmq.client.exception.MQClientException: executor rejected
For more information, please visit the url, http://rocketmq.apache.org/docs/faq/
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:522)
at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:382)
at org.apache.rocketmq.example.tracemessage.TraceProducer.main(TraceProducer.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@46b7c89b rejected from java.util.concurrent.ThreadPoolExecutor@57536d79[Running, pool size = 8, active threads = 8, queued tasks = 50000, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:504)
... 7 more
org.apache.rocketmq.client.exception.MQClientException: executor rejected
org.apache.rocketmq.client.exception.MQClientException: wait response timeout 0ms
For more information, please visit the url, http://rocketmq.apache.org/docs/faq/
at org.apache.rocketmq.client.impl.MQClientAPIImpl$1.operationComplete(MQClientAPIImpl.java:569)
at org.apache.rocketmq.remoting.netty.ResponseFuture.executeInvokeCallback(ResponseFuture.java:54)
at org.apache.rocketmq.remoting.netty.NettyRemotingAbstract$2.run(NettyRemotingAbstract.java:309)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
org.apache.rocketmq.client.exception.MQClientException: wait response timeout 0ms
- What did you expect to see? From the rocketmq source code, I found that the defaultAsyncSenderExecutor only support 5w requests in the queue. So if the broker can not process the msgs as quickly as the producer send, the defaultAsyncSenderExecutor will reject request when the defaultAsyncSenderExecutor full. My questions are : What is the right way to use the producer to send msg async? Should I control the frequency when send a async msg? If so, what is the recommend frequency to send async?
-
Please tell us about your environment: 4core +4G rocketmq 4.6.0
-
Other information (e.g. detailed explanation, logs, related issues, suggestions how to fix, etc):
Why do you say only support 5w requests in the queue?
From the error info of java.util.concurrent.ThreadPoolExecutor@57536d79[Running, pool size = 8, active threads = 8, queued tasks = 50000, completed tasks = 0],I doubt that not even one message was sent successfully. Because the completed task shouldn't be 0.
There must be sth. wrong. Could you please provide more infos?
Why do you say
only support 5w requests in the queue? From the error info ofjava.util.concurrent.ThreadPoolExecutor@57536d79[Running, pool size = 8, active threads = 8, queued tasks = 50000, completed tasks = 0],I doubt that not even one message was sent successfully. Because the completed task shouldn't be 0. There must be sth. wrong. Could you please provide more infos?
The ‘completed tasks’ is not always 0. This exception occured because the broker can not process message as quickly as the producer async send.
https://github.com/apache/rocketmq/blob/master/client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java#L125
Maybe the performance nest is on the client-side. In async mode, the client sends messages without waiting for responses from the broker. So, actually the broker has no business with that. Maybe it is the client-side that the defaultAsyncSenderExecutor can not follow the speed of sending messages.
Maybe the performance nest is on the client-side. In async mode, the client sends messages without waiting for responses from the broker. So, actually the broker has no business with that. Maybe it is the client-side that the defaultAsyncSenderExecutor can not follow the speed of sending messages.
Thanks. I reproduce the scene, the producer client node‘s cpu load is too high. Maybe defaultAsyncSenderExecutor exhausts cpu, so the task thread have no to time to send message to broker.
So how to solve this problem?
So how to solve this problem?
So how to solve this problem?
A custom thread pool and a good rejection strategy may be a good choice.
/**
* Sets an Executor to be used for executing asynchronous send. If the Executor is not set, {@link
* DefaultMQProducerImpl#defaultAsyncSenderExecutor} will be used.
*
* @param asyncSenderExecutor the instance of Executor
*/
public void setAsyncSenderExecutor(final ExecutorService asyncSenderExecutor) {
this.defaultMQProducerImpl.setAsyncSenderExecutor(asyncSenderExecutor);
}