Skip to content

Commit

Permalink
feat:add polaris ThreadLocal plugin. (#1255)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman authored Apr 2, 2024
1 parent 27b96bb commit 64ac291
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
- [feat: PolarisServiceRegistry#deregister support idempotency.](https://github.com/Tencent/spring-cloud-tencent/pull/1243)
- [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: 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)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-tencent-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-tencent-coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 7 additions & 1 deletion spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions spring-cloud-tencent-plugin-starters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 64ac291

Please sign in to comment.