mq-将送礼和幸运礼物的同步批量发送改成异步发送
This commit is contained in:
@@ -6,8 +6,11 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.rocketmq.client.producer.SendCallback;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
@@ -40,6 +43,39 @@ public class MQMessageProducer {
|
||||
send(queueName, object, success, error, null, delayLevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param queueName
|
||||
* @param success
|
||||
* @param error
|
||||
* @param <T>
|
||||
*/
|
||||
public <T extends Message> void send(String queueName, Collection<T> objectList, Consumer<SendResult> success, Consumer<Throwable> error) {
|
||||
if (CollectionUtils.isEmpty(objectList)) {
|
||||
return;
|
||||
}
|
||||
long timeout = rocketMQTemplate.getProducer().getSendMsgTimeout();
|
||||
try {
|
||||
rocketMQTemplate.asyncSend(queueName, objectList, new SendCallback() {
|
||||
@Override
|
||||
public void onSuccess(SendResult sendResult) {
|
||||
if (success != null) {
|
||||
success.accept(sendResult);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable throwable) {
|
||||
if (error != null) {
|
||||
error.accept(throwable);
|
||||
}
|
||||
}
|
||||
}, timeout);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param queueName
|
||||
* @param object
|
||||
|
Reference in New Issue
Block a user