Browse Source

Merge branch 'release_toaliyun_real' into aliyun_real_xhxu

release_toaliyun_real
xiaohanzi 5 years ago
parent
commit
220ef4ff96
2 changed files with 64 additions and 6 deletions
  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 View File

@@ -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 View File

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

Loading…
Cancel
Save