feat:add polaris ThreadLocal plugin. (#1255)

pull/1257/head
Haotian Zhang 6 months ago committed by GitHub
parent 27b96bb7bb
commit 64ac291f4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -6,3 +6,4 @@
- [feat: SCT元数据管理能力与polaris-java元数据管理能力进行下沉及整合](https://github.com/Tencent/spring-cloud-tencent/pull/1249)
- [feat: support lane router](https://github.com/Tencent/spring-cloud-tencent/pull/1250)
- [feat: lane router rule support caller ip](https://github.com/Tencent/spring-cloud-tencent/pull/1253)
- [feat:add polaris ThreadLocal plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1255)

@ -90,7 +90,7 @@
<properties>
<!-- Project revision -->
<revision>1.14.0-Hoxton.SR12-RC1</revision>
<revision>1.14.0-Hoxton.SR12-SNAPSHOT</revision>
<!-- Spring Framework -->
<spring.framework.version>5.2.25.RELEASE</spring.framework.version>

@ -30,6 +30,11 @@
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-threadlocal-plugin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>

@ -93,6 +93,11 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-lossless-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-threadlocal-plugin</artifactId>
</dependency>
</dependencies>
<build>

@ -70,7 +70,7 @@
</developers>
<properties>
<revision>1.14.0-Hoxton.SR12-RC1</revision>
<revision>1.14.0-Hoxton.SR12-SNAPSHOT</revision>
<!-- Dependencies -->
<polaris.version>1.15.3-SNAPSHOT</polaris.version>
@ -199,6 +199,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-threadlocal-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies -->
<dependency>
<groupId>com.google.guava</groupId>

@ -19,6 +19,7 @@
<module>spring-cloud-tencent-gateway-plugin</module>
<module>spring-cloud-starter-tencent-discovery-adapter-plugin</module>
<module>spring-cloud-tencent-lossless-plugin</module>
<module>spring-cloud-starter-tencent-threadlocal-plugin</module>
</modules>
</project>

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-starter-tencent-threadlocal-plugin</artifactId>
<name>Spring Cloud Starter Tencent ThreadLocal plugin</name>
<dependencies>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-threadlocal</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,45 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.plugin.threadlocal;
import java.util.function.Consumer;
import java.util.function.Supplier;
import com.tencent.polaris.threadlocal.cross.RunnableWrapper;
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorWrapper<T> implements TaskExecutor {
private final TaskExecutor taskExecutor;
private final Supplier<T> contextGetter;
private final Consumer<T> contextSetter;
public TaskExecutorWrapper(TaskExecutor taskExecutor, Supplier<T> contextGetter, Consumer<T> contextSetter) {
this.taskExecutor = taskExecutor;
this.contextGetter = contextGetter;
this.contextSetter = contextSetter;
}
@Override
public void execute(Runnable command) {
taskExecutor.execute(new RunnableWrapper<>(command, contextGetter, contextSetter));
}
}

@ -0,0 +1,62 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.plugin.threadlocal;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
/**
* Test for {@link TaskExecutorWrapper}.
*
* @author Haotian Zhang
*/
public class TaskExecutorWrapperTest {
private static final ThreadLocal<String> TEST_THREAD_LOCAL = new ThreadLocal<>();
private static final String TEST = "TEST";
@Test
public void testExecute() {
TEST_THREAD_LOCAL.set(TEST);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.initialize();
AtomicReference<Boolean> result = new AtomicReference<>(false);
CountDownLatch latch = new CountDownLatch(1);
TaskExecutorWrapper<String> taskExecutorWrapper = new TaskExecutorWrapper<>(
executor, TEST_THREAD_LOCAL::get, TEST_THREAD_LOCAL::set);
taskExecutorWrapper.execute(() -> {
result.set(TEST.equals(TEST_THREAD_LOCAL.get()));
latch.countDown();
});
try {
latch.await();
assertThat(result.get()).isTrue();
}
catch (InterruptedException e) {
fail(e.getMessage());
}
}
}
Loading…
Cancel
Save