Browse Source

线程优化

xiaochan 4 months ago
parent
commit
42bffc7dce
1 changed files with 4 additions and 9 deletions
  1. 4 9
      thyy-utils/src/main/java/org/thyy/utils/WebThreadLocal.java

+ 4 - 9
thyy-utils/src/main/java/org/thyy/utils/WebThreadLocal.java

@@ -5,9 +5,9 @@ import org.springframework.web.context.request.RequestContextHolder;
 
 public class WebThreadLocal {
 
-    private static void checkRequestContext() {
+    private static RequestAttributes checkRequestContext() {
         try {
-            RequestContextHolder.currentRequestAttributes();
+            return RequestContextHolder.currentRequestAttributes();
         } catch (IllegalStateException e) {
             throw new IllegalStateException("必须在Web请求线程中调用!");
         }
@@ -18,17 +18,12 @@ public class WebThreadLocal {
     }
 
     public static <T> void create(String name, T value) {
-        checkRequestContext();
-        RequestContextHolder
-                .currentRequestAttributes()
-                .setAttribute(getKeyName(name), value, RequestAttributes.SCOPE_REQUEST);
+        checkRequestContext().setAttribute(getKeyName(name), value, RequestAttributes.SCOPE_REQUEST);
     }
 
     @SuppressWarnings("unchecked")
     public static <T> T get(String name) {
-        checkRequestContext();
-        return (T) RequestContextHolder
-                .currentRequestAttributes()
+        return (T) checkRequestContext()
                 .getAttribute(getKeyName(name), RequestAttributes.SCOPE_REQUEST);
     }
 }