|
@@ -25,9 +25,7 @@ import com.chelvc.framework.common.util.ObjectUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.common.util.ThreadUtils;
|
|
import com.chelvc.framework.common.util.ThreadUtils;
|
|
import com.chelvc.framework.rocketmq.config.RocketMQProperties;
|
|
import com.chelvc.framework.rocketmq.config.RocketMQProperties;
|
|
-import com.chelvc.framework.rocketmq.producer.Demoting;
|
|
|
|
import com.chelvc.framework.rocketmq.producer.RocketMQProducerFactory;
|
|
import com.chelvc.framework.rocketmq.producer.RocketMQProducerFactory;
|
|
-import com.chelvc.framework.rocketmq.producer.TransactionDemoting;
|
|
|
|
import lombok.NonNull;
|
|
import lombok.NonNull;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
@@ -69,7 +67,7 @@ public final class RocketMQContextHolder {
|
|
/**
|
|
/**
|
|
* 消息发送降级队列
|
|
* 消息发送降级队列
|
|
*/
|
|
*/
|
|
- private static final BlockingQueue<Demoting> DEMOTING_QUEUE = new LinkedBlockingQueue<>();
|
|
|
|
|
|
+ private static final BlockingQueue<MessageSending> MESSAGE_SENDING_QUEUE = new LinkedBlockingQueue<>();
|
|
|
|
|
|
/**
|
|
/**
|
|
* 配置属性
|
|
* 配置属性
|
|
@@ -91,15 +89,15 @@ public final class RocketMQContextHolder {
|
|
ThreadUtils.run(() -> {
|
|
ThreadUtils.run(() -> {
|
|
while (!Thread.interrupted()) {
|
|
while (!Thread.interrupted()) {
|
|
// 尝试从队列中获取降级处理信息,如果为空则等待1分钟后继续
|
|
// 尝试从队列中获取降级处理信息,如果为空则等待1分钟后继续
|
|
- Demoting demoting = DEMOTING_QUEUE.peek();
|
|
|
|
- if (demoting == null) {
|
|
|
|
|
|
+ MessageSending sending = MESSAGE_SENDING_QUEUE.peek();
|
|
|
|
+ if (sending == null) {
|
|
ThreadUtils.sleep(60000);
|
|
ThreadUtils.sleep(60000);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
// 降级处理,重试消息发送
|
|
// 降级处理,重试消息发送
|
|
try {
|
|
try {
|
|
- demoting.retry();
|
|
|
|
|
|
+ sending.retry();
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
// 如果因网络超时或触发TPS限制则等待1分钟后继续
|
|
// 如果因网络超时或触发TPS限制则等待1分钟后继续
|
|
log.warn("RocketMQ message send failed: {}", e.getMessage());
|
|
log.warn("RocketMQ message send failed: {}", e.getMessage());
|
|
@@ -111,7 +109,7 @@ public final class RocketMQContextHolder {
|
|
}
|
|
}
|
|
|
|
|
|
// 降级处理完成后移除队列
|
|
// 降级处理完成后移除队列
|
|
- DEMOTING_QUEUE.remove();
|
|
|
|
|
|
+ MESSAGE_SENDING_QUEUE.remove();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -333,9 +331,11 @@ public final class RocketMQContextHolder {
|
|
try {
|
|
try {
|
|
producer.send(message);
|
|
producer.send(message);
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
|
|
+ log.warn("RocketMQ message send failed: {}", e.getMessage());
|
|
|
|
+
|
|
// 降级处理
|
|
// 降级处理
|
|
try {
|
|
try {
|
|
- DEMOTING_QUEUE.put(new Demoting(producer, message));
|
|
|
|
|
|
+ MESSAGE_SENDING_QUEUE.put(new MessageSending(producer, message));
|
|
} catch (InterruptedException ignore) {
|
|
} catch (InterruptedException ignore) {
|
|
}
|
|
}
|
|
} catch (ClientException e) {
|
|
} catch (ClientException e) {
|
|
@@ -442,9 +442,11 @@ public final class RocketMQContextHolder {
|
|
Producer producer = getProducerFactory().getProducer(message.getTopic());
|
|
Producer producer = getProducerFactory().getProducer(message.getTopic());
|
|
producer.sendAsync(message).exceptionally(e -> {
|
|
producer.sendAsync(message).exceptionally(e -> {
|
|
if (e instanceof ProxyTimeoutException || e instanceof TooManyRequestsException) {
|
|
if (e instanceof ProxyTimeoutException || e instanceof TooManyRequestsException) {
|
|
|
|
+ log.warn("RocketMQ message async send failed: {}", e.getMessage());
|
|
|
|
+
|
|
// 降级处理
|
|
// 降级处理
|
|
try {
|
|
try {
|
|
- DEMOTING_QUEUE.put(new Demoting(producer, message));
|
|
|
|
|
|
+ MESSAGE_SENDING_QUEUE.put(new MessageSending(producer, message));
|
|
} catch (InterruptedException ignore) {
|
|
} catch (InterruptedException ignore) {
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -569,9 +571,11 @@ public final class RocketMQContextHolder {
|
|
transaction.rollback();
|
|
transaction.rollback();
|
|
}
|
|
}
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
} catch (ProxyTimeoutException | TooManyRequestsException e) {
|
|
|
|
+ log.warn("RocketMQ message send failed: {}", e.getMessage());
|
|
|
|
+
|
|
// 降级处理
|
|
// 降级处理
|
|
try {
|
|
try {
|
|
- DEMOTING_QUEUE.put(new TransactionDemoting(producer, message, executor));
|
|
|
|
|
|
+ MESSAGE_SENDING_QUEUE.put(new TransactionalMessageSending(producer, message, executor));
|
|
} catch (InterruptedException ignore) {
|
|
} catch (InterruptedException ignore) {
|
|
}
|
|
}
|
|
} catch (ClientException e) {
|
|
} catch (ClientException e) {
|