Skip to content

Commit 3f7e040

Browse files
committed
refactor: 优化上下文传递
1 parent 2dbb9c7 commit 3f7e040

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/context/AuthenticationThreadLocalAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class AuthenticationThreadLocalAccessor
1111
implements ThreadLocalAccessor<Authentication> {
1212

13-
static final String KEY = "cp.hs.auth";
13+
static final Object KEY = Authentication.class;
1414

1515
static {
1616
ReactiveAuthenticationHolder.addSupplier(

hsweb-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,11 @@
161161
<artifactId>hsweb-easy-orm-core</artifactId>
162162
</dependency>
163163

164+
<dependency>
165+
<groupId>io.micrometer</groupId>
166+
<artifactId>context-propagation</artifactId>
167+
<optional>true</optional>
168+
</dependency>
169+
164170
</dependencies>
165171
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.hswebframework.web.i18n;
2+
3+
import io.micrometer.context.ThreadLocalAccessor;
4+
5+
import javax.annotation.Nonnull;
6+
import java.util.Locale;
7+
8+
public class LocaleThreadLocalAccessor implements ThreadLocalAccessor<Locale> {
9+
10+
@Override
11+
@Nonnull
12+
public Object key() {
13+
return Locale.class;
14+
}
15+
16+
@Override
17+
public Locale getValue() {
18+
return LocaleUtils.CONTEXT_THREAD_LOCAL.getIfExists();
19+
}
20+
21+
@Override
22+
public void setValue() {
23+
LocaleUtils.CONTEXT_THREAD_LOCAL.remove();
24+
}
25+
26+
@Override
27+
public void setValue(@Nonnull Locale value) {
28+
LocaleUtils.CONTEXT_THREAD_LOCAL.set(value);
29+
}
30+
}

hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class LocaleUtils {
3535

3636
public static final Locale DEFAULT_LOCALE = Locale.getDefault();
3737

38-
private static final FastThreadLocal<Locale> CONTEXT_THREAD_LOCAL = new FastThreadLocal<>();
38+
static final FastThreadLocal<Locale> CONTEXT_THREAD_LOCAL = new FastThreadLocal<>();
3939

4040
static MessageSource messageSource = UnsupportedMessageSource.instance();
4141

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.hswebframework.web.i18n.LocaleThreadLocalAccessor
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hswebframework.web.i18n;
2+
3+
import org.junit.jupiter.api.Test;
4+
import reactor.core.publisher.Hooks;
5+
import reactor.core.scheduler.Schedulers;
6+
7+
import java.util.Locale;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class LocaleThreadLocalAccessorTest {
12+
13+
static {
14+
Hooks.enableAutomaticContextPropagation();
15+
}
16+
17+
@Test
18+
void testInReactive() {
19+
20+
for (Locale availableLocale : Locale.getAvailableLocales()) {
21+
assertEquals(availableLocale,
22+
LocaleUtils.doWith(
23+
availableLocale,
24+
() -> LocaleUtils
25+
.currentReactive()
26+
.subscribeOn(Schedulers.boundedElastic())
27+
.block()));
28+
}
29+
30+
31+
}
32+
}

0 commit comments

Comments
 (0)