|
|
@@ -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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |