Просмотр исходного кода

处理非web请求的tenantId处理

release_toaliyun_real
xiaohanzi 5 лет назад
Родитель
Сommit
0a1fe0936d
2 измененных файлов: 64 добавлений и 6 удалений
  1. +47
    -0
      mallinkService/src/main/java/com/iformall/common/NonWebRequestAttributes.java
  2. +17
    -6
      mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java

+ 47
- 0
mallinkService/src/main/java/com/iformall/common/NonWebRequestAttributes.java Просмотреть файл

@@ -0,0 +1,47 @@
package com.iformall.common;

import org.springframework.web.context.request.RequestAttributes;

public class NonWebRequestAttributes implements RequestAttributes{

@Override
public Object getAttribute(String arg0, int arg1) {
return null;
}

@Override
public String[] getAttributeNames(int arg0) {
return null;
}

@Override
public String getSessionId() {
return null;
}

@Override
public Object getSessionMutex() {
return null;
}

@Override
public void registerDestructionCallback(String arg0, Runnable arg1, int arg2) {
}

@Override
public void removeAttribute(String arg0, int arg1) {
}

@Override
public Object resolveReference(String arg0) {
return null;
}

@Override
public void setAttribute(String arg0, Object arg1, int arg2) {
}

}

+ 17
- 6
mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java Просмотреть файл

@@ -9,27 +9,38 @@ public class TenantThreadLocal {
//private static ThreadLocal<String> parentLocal = new ThreadLocal<String>(); //private static ThreadLocal<String> parentLocal = new ThreadLocal<String>();
public static void setCurrentThreadTenant(String tenantId,String parentId) { public static void setCurrentThreadTenant(String tenantId,String parentId) {
RequestContextHolder.currentRequestAttributes().setAttribute("tenaneId", tenantId, RequestAttributes.SCOPE_REQUEST);
RequestContextHolder.currentRequestAttributes().setAttribute("parentTenaneId", parentId, RequestAttributes.SCOPE_REQUEST);
getRequestAttributesSafely().setAttribute("tenaneId", tenantId, RequestAttributes.SCOPE_REQUEST);
getRequestAttributesSafely().setAttribute("parentTenaneId", parentId, RequestAttributes.SCOPE_REQUEST);
//local.set(tenantId); //local.set(tenantId);
//parentLocal.set(parentId); //parentLocal.set(parentId);
} }
public static String getTenantId() { public static String getTenantId() {
return (String)RequestContextHolder.currentRequestAttributes().getAttribute("tenaneId", RequestAttributes.SCOPE_REQUEST);
return (String)getRequestAttributesSafely().getAttribute("tenaneId", RequestAttributes.SCOPE_REQUEST);
//return local.get(); //return local.get();
} }
public static String getParentTenantId() { public static String getParentTenantId() {
return (String)RequestContextHolder.currentRequestAttributes().getAttribute("parentTenaneId", RequestAttributes.SCOPE_REQUEST);
return (String)getRequestAttributesSafely().getAttribute("parentTenaneId", RequestAttributes.SCOPE_REQUEST);
//return parentLocal.get(); //return parentLocal.get();
} }
public static void remove() { public static void remove() {
RequestContextHolder.currentRequestAttributes().removeAttribute("tenaneId", RequestAttributes.SCOPE_REQUEST);
RequestContextHolder.currentRequestAttributes().removeAttribute("parentTenaneId", RequestAttributes.SCOPE_REQUEST);
getRequestAttributesSafely().removeAttribute("tenaneId", RequestAttributes.SCOPE_REQUEST);
getRequestAttributesSafely().removeAttribute("parentTenaneId", RequestAttributes.SCOPE_REQUEST);
//local.remove(); //local.remove();
//parentLocal.remove(); //parentLocal.remove();
} }
//如果是非web的线程,则都为空
public static RequestAttributes getRequestAttributesSafely(){
RequestAttributes requestAttributes = null;
try{
requestAttributes = RequestContextHolder.currentRequestAttributes();
}catch (IllegalStateException e){
requestAttributes = new NonWebRequestAttributes();
}
return requestAttributes;
}
} }

Загрузка…
Отмена
Сохранить