dongyukun
2024-12-31 0a2b23ad3f30dfb01c5d590fb98f39e93bfe1932
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.tenant.core.mq.rocketmq;
H 2
3 import com.iailab.framework.tenant.core.context.TenantContextHolder;
4 import org.apache.rocketmq.client.hook.SendMessageContext;
5 import org.apache.rocketmq.client.hook.SendMessageHook;
6
7 import static com.iailab.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID;
8
9 /**
10  * RocketMQ 消息队列的多租户 {@link SendMessageHook} 实现类
11  *
12  * Producer 发送消息时,将 {@link TenantContextHolder} 租户编号,添加到消息的 Header 中
13  *
14  * @author iailab
15  */
16 public class TenantRocketMQSendMessageHook implements SendMessageHook {
17
18     @Override
19     public String hookName() {
20         return getClass().getSimpleName();
21     }
22
23     @Override
24     public void sendMessageBefore(SendMessageContext sendMessageContext) {
25         Long tenantId = TenantContextHolder.getTenantId();
26         if (tenantId == null) {
27             return;
28         }
29         sendMessageContext.getMessage().putUserProperty(HEADER_TENANT_ID, tenantId.toString());
30     }
31
32     @Override
33     public void sendMessageAfter(SendMessageContext sendMessageContext) {
34     }
35
36 }