diff --git a/mallinkService/src/main/java/com/iformall/common/NonWebRequestAttributes.java b/mallinkService/src/main/java/com/iformall/common/NonWebRequestAttributes.java new file mode 100644 index 000000000..e9fc1d913 --- /dev/null +++ b/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) { + + } + +} diff --git a/mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java b/mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java index 0e7c3e39b..8e1785ce9 100644 --- a/mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java +++ b/mallinkService/src/main/java/com/iformall/common/TenantThreadLocal.java @@ -9,27 +9,38 @@ public class TenantThreadLocal { //private static ThreadLocal parentLocal = new ThreadLocal(); 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; + } + }