|
@@ -41,32 +41,34 @@ public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
|
/**
|
|
|
* 初始化消息队列
|
|
|
*/
|
|
|
- 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();
|
|
|
+ private void initializeMessageQueue() {
|
|
|
+ if (this.queue == null) {
|
|
|
+ synchronized (this) {
|
|
|
+ if (this.queue == null) {
|
|
|
+ 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.removeSessionContext();
|
|
|
+ }
|
|
|
+ ThreadUtils.sleep(5000);
|
|
|
+ return false;
|
|
|
+ });
|
|
|
}
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("RocketMQ message send failed: {}", RocketMQContextHolder.topic(message), e);
|
|
|
- } finally {
|
|
|
- SessionContextHolder.removeSessionContext();
|
|
|
}
|
|
|
- ThreadUtils.sleep(5000);
|
|
|
- return false;
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -76,12 +78,8 @@ public class RocketMQMemoryProducer extends RocketMQFallbackProducer {
|
|
|
log.debug("RocketMQ fallback message: {}, {}", topic, body);
|
|
|
}
|
|
|
|
|
|
- // 初始化消息队列
|
|
|
- if (this.queue == null) {
|
|
|
- this.initializeMessageQueue();
|
|
|
- }
|
|
|
-
|
|
|
// 将消息放入内存消息队列
|
|
|
+ this.initializeMessageQueue();
|
|
|
Session session = SessionContextHolder.getSession(false);
|
|
|
if (!this.queue.offer(Pair.of(Pair.of(message, transaction), session))) {
|
|
|
String topic = RocketMQContextHolder.topic(message), body = RocketMQContextHolder.body(message);
|