-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:add polaris ThreadLocal plugin. (#1255)
- Loading branch information
1 parent
27b96bb
commit 64ac291
Showing
9 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
45 changes: 45 additions & 0 deletions
45
...dlocal-plugin/src/main/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...al-plugin/src/test/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |