提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.tenant.core.mq.redis; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.mq.redis.core.interceptor.RedisMessageInterceptor; |
|
5 |
import com.iailab.framework.mq.redis.core.message.AbstractRedisMessage; |
|
6 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
|
7 |
|
|
8 |
import static com.iailab.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID; |
|
9 |
|
|
10 |
/** |
|
11 |
* 多租户 {@link AbstractRedisMessage} 拦截器 |
|
12 |
* |
|
13 |
* 1. Producer 发送消息时,将 {@link TenantContextHolder} 租户编号,添加到消息的 Header 中 |
|
14 |
* 2. Consumer 消费消息时,将消息的 Header 的租户编号,添加到 {@link TenantContextHolder} 中 |
|
15 |
* |
|
16 |
* @author iailab |
|
17 |
*/ |
|
18 |
public class TenantRedisMessageInterceptor implements RedisMessageInterceptor { |
|
19 |
|
|
20 |
@Override |
|
21 |
public void sendMessageBefore(AbstractRedisMessage message) { |
|
22 |
Long tenantId = TenantContextHolder.getTenantId(); |
|
23 |
if (tenantId != null) { |
|
24 |
message.addHeader(HEADER_TENANT_ID, tenantId.toString()); |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public void consumeMessageBefore(AbstractRedisMessage message) { |
|
30 |
String tenantIdStr = message.getHeader(HEADER_TENANT_ID); |
|
31 |
if (StrUtil.isNotEmpty(tenantIdStr)) { |
|
32 |
TenantContextHolder.setTenantId(Long.valueOf(tenantIdStr)); |
|
33 |
} |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public void consumeMessageAfter(AbstractRedisMessage message) { |
|
38 |
// 注意,Consumer 是一个逻辑的入口,所以不考虑原本上下文就存在租户编号的情况 |
|
39 |
TenantContextHolder.clear(); |
|
40 |
} |
|
41 |
|
|
42 |
} |