|
@@ -25,7 +25,7 @@ import org.apache.rocketmq.client.apis.producer.Transaction;
|
|
@Slf4j
|
|
@Slf4j
|
|
public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
private final int capacity;
|
|
private final int capacity;
|
|
- private volatile Thread thread;
|
|
|
|
|
|
+ private volatile Thread consumer;
|
|
private volatile BlockingQueue<Pair<Pair<Message, Transaction>, Session>> queue;
|
|
private volatile BlockingQueue<Pair<Pair<Message, Transaction>, Session>> queue;
|
|
|
|
|
|
public RocketMQMemoryProducer(@NonNull Producer delegate) {
|
|
public RocketMQMemoryProducer(@NonNull Producer delegate) {
|
|
@@ -38,6 +38,37 @@ public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
this.capacity = capacity;
|
|
this.capacity = capacity;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 初始化消息队列
|
|
|
|
+ */
|
|
|
|
+ private synchronized void initializeMessageQueue() {
|
|
|
|
+ if (this.queue != null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.queue = new ArrayBlockingQueue<>(this.capacity);
|
|
|
|
+ this.consumer = ThreadUtils.consume(this.queue, pair -> {
|
|
|
|
+ Message message = pair.getLeft().getLeft();
|
|
|
|
+ Transaction transaction = pair.getLeft().getRight();
|
|
|
|
+ SessionContextHolder.setSession(pair.getRight());
|
|
|
|
+ try {
|
|
|
|
+ if (transaction == null) {
|
|
|
|
+ this.delegate.send(message);
|
|
|
|
+ } else {
|
|
|
|
+ this.delegate.send(message, transaction);
|
|
|
|
+ transaction.commit();
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("RocketMQ message send failed: {}", RocketMQContextHolder.topic(message), e);
|
|
|
|
+ } finally {
|
|
|
|
+ SessionContextHolder.clearSessionContext();
|
|
|
|
+ }
|
|
|
|
+ ThreadUtils.sleep(5000);
|
|
|
|
+ return false;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
protected void fallback(Message message, Transaction transaction) {
|
|
protected void fallback(Message message, Transaction transaction) {
|
|
if (log.isDebugEnabled()) {
|
|
if (log.isDebugEnabled()) {
|
|
@@ -45,33 +76,9 @@ public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
log.debug("RocketMQ fallback message: {}, {}", topic, body);
|
|
log.debug("RocketMQ fallback message: {}, {}", topic, body);
|
|
}
|
|
}
|
|
|
|
|
|
- // 初始化消息队列及重试线程
|
|
|
|
|
|
+ // 初始化消息队列
|
|
if (this.queue == null) {
|
|
if (this.queue == null) {
|
|
- synchronized (this) {
|
|
|
|
- if (this.queue == null) {
|
|
|
|
- this.queue = new ArrayBlockingQueue<>(this.capacity);
|
|
|
|
- this.thread = ThreadUtils.consume(this.queue, pair -> {
|
|
|
|
- Message _message = pair.getLeft().getLeft();
|
|
|
|
- Transaction _transaction = pair.getLeft().getRight();
|
|
|
|
- SessionContextHolder.setSession(pair.getRight());
|
|
|
|
- try {
|
|
|
|
- if (_transaction == null) {
|
|
|
|
- this.delegate.send(_message);
|
|
|
|
- } else {
|
|
|
|
- this.delegate.send(_message, _transaction);
|
|
|
|
- _transaction.commit();
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("RocketMQ message send failed: {}", RocketMQContextHolder.topic(_message), e);
|
|
|
|
- } finally {
|
|
|
|
- SessionContextHolder.clearSessionContext();
|
|
|
|
- }
|
|
|
|
- ThreadUtils.sleep(5000);
|
|
|
|
- return false;
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ this.initializeMessageQueue();
|
|
}
|
|
}
|
|
|
|
|
|
// 将消息放入内存消息队列
|
|
// 将消息放入内存消息队列
|
|
@@ -85,9 +92,9 @@ public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
@Override
|
|
@Override
|
|
public void close() throws IOException {
|
|
public void close() throws IOException {
|
|
try {
|
|
try {
|
|
- if (this.thread != null) {
|
|
|
|
- this.thread.interrupt();
|
|
|
|
- ThreadUtils.join(this.thread);
|
|
|
|
|
|
+ if (this.consumer != null) {
|
|
|
|
+ this.consumer.interrupt();
|
|
|
|
+ ThreadUtils.join(this.consumer);
|
|
}
|
|
}
|
|
} finally {
|
|
} finally {
|
|
super.close();
|
|
super.close();
|