diff --git a/CHANGELOG.md b/CHANGELOG.md index 0526fb27..c1070584 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) \ No newline at end of file +- [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) \ No newline at end of file diff --git a/pom.xml b/pom.xml index 49e6e8c3..110bbbf4 100644 --- a/pom.xml +++ b/pom.xml @@ -90,7 +90,7 @@ - 1.14.0-Hoxton.SR12-RC1 + 1.14.0-Hoxton.SR12-SNAPSHOT 5.2.25.RELEASE diff --git a/spring-cloud-tencent-commons/pom.xml b/spring-cloud-tencent-commons/pom.xml index 970a5bf9..d2c5477a 100644 --- a/spring-cloud-tencent-commons/pom.xml +++ b/spring-cloud-tencent-commons/pom.xml @@ -30,6 +30,11 @@ + + com.tencent.cloud + spring-cloud-starter-tencent-threadlocal-plugin + + org.springframework.boot spring-boot-autoconfigure diff --git a/spring-cloud-tencent-coverage/pom.xml b/spring-cloud-tencent-coverage/pom.xml index 0cb2681a..716f69b7 100644 --- a/spring-cloud-tencent-coverage/pom.xml +++ b/spring-cloud-tencent-coverage/pom.xml @@ -93,6 +93,11 @@ com.tencent.cloud spring-cloud-tencent-lossless-plugin + + + com.tencent.cloud + spring-cloud-starter-tencent-threadlocal-plugin + diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index eb47d201..0a0926af 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.14.0-Hoxton.SR12-RC1 + 1.14.0-Hoxton.SR12-SNAPSHOT 1.15.3-SNAPSHOT @@ -199,6 +199,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-starter-tencent-threadlocal-plugin + ${revision} + + com.google.guava diff --git a/spring-cloud-tencent-plugin-starters/pom.xml b/spring-cloud-tencent-plugin-starters/pom.xml index 0258ceee..5debf636 100644 --- a/spring-cloud-tencent-plugin-starters/pom.xml +++ b/spring-cloud-tencent-plugin-starters/pom.xml @@ -19,6 +19,7 @@ spring-cloud-tencent-gateway-plugin spring-cloud-starter-tencent-discovery-adapter-plugin spring-cloud-tencent-lossless-plugin + spring-cloud-starter-tencent-threadlocal-plugin diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/pom.xml b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/pom.xml new file mode 100644 index 00000000..91975c14 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/pom.xml @@ -0,0 +1,35 @@ + + + + spring-cloud-tencent-plugin-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-starter-tencent-threadlocal-plugin + Spring Cloud Starter Tencent ThreadLocal plugin + + + + + com.tencent.polaris + polaris-threadlocal + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/main/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapper.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/main/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapper.java new file mode 100644 index 00000000..f2f9dc1c --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/main/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapper.java @@ -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 implements TaskExecutor { + + private final TaskExecutor taskExecutor; + + private final Supplier contextGetter; + + private final Consumer contextSetter; + + public TaskExecutorWrapper(TaskExecutor taskExecutor, Supplier contextGetter, Consumer contextSetter) { + this.taskExecutor = taskExecutor; + this.contextGetter = contextGetter; + this.contextSetter = contextSetter; + } + + @Override + public void execute(Runnable command) { + taskExecutor.execute(new RunnableWrapper<>(command, contextGetter, contextSetter)); + } +} diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/test/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapperTest.java b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/test/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapperTest.java new file mode 100644 index 00000000..b56215f9 --- /dev/null +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-starter-tencent-threadlocal-plugin/src/test/java/com/tencent/cloud/plugin/threadlocal/TaskExecutorWrapperTest.java @@ -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 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 result = new AtomicReference<>(false); + CountDownLatch latch = new CountDownLatch(1); + TaskExecutorWrapper 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()); + } + } +}