dongyukun
9 天以前 e88fba9645a57535d858ce48da8e9d9a3dc84adc
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.tenant.core.mq.rabbitmq;
H 2
3 import com.iailab.framework.tenant.core.context.TenantContextHolder;
4 import org.apache.kafka.clients.producer.ProducerInterceptor;
5 import org.springframework.amqp.AmqpException;
6 import org.springframework.amqp.core.Message;
7 import org.springframework.amqp.core.MessagePostProcessor;
8 import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
9
10 import static com.iailab.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID;
11
12 /**
13  * RabbitMQ 消息队列的多租户 {@link ProducerInterceptor} 实现类
14  *
15  * 1. Producer 发送消息时,将 {@link TenantContextHolder} 租户编号,添加到消息的 Header 中
16  * 2. Consumer 消费消息时,将消息的 Header 的租户编号,添加到 {@link TenantContextHolder} 中,通过 {@link InvocableHandlerMethod} 实现
17  *
18  * @author iailab
19  */
20 public class TenantRabbitMQMessagePostProcessor implements MessagePostProcessor {
21
22     @Override
23     public Message postProcessMessage(Message message) throws AmqpException {
24         Long tenantId = TenantContextHolder.getTenantId();
25         if (tenantId != null) {
26             message.getMessageProperties().getHeaders().put(HEADER_TENANT_ID, tenantId);
27         }
28         return message;
29     }
30
31 }