optimize code

pull/422/head
wulingxiao 3 years ago
parent efc572dfee
commit 8254fe98cd

@ -61,8 +61,8 @@
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId> <artifactId>transmittable-thread-local</artifactId>
<optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

@ -1,37 +0,0 @@
/*
* 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.metadata.concurrent;
/**
* Metadata Wrapper interface.
* <p>
* Used to mark wrapper types, for example:
*
* @author wlx
*/
public interface MetadataWrap<T> {
/**
* unwrap to the original/underneath one.
*
* @return a unWrap instance
*/
T unWrap();
}

@ -21,10 +21,10 @@ package com.tencent.cloud.metadata.concurrent.executor;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import com.alibaba.ttl.spi.TtlWrapper;
import com.alibaba.ttl.threadpool.TtlExecutors; import com.alibaba.ttl.threadpool.TtlExecutors;
import com.alibaba.ttl.threadpool.agent.TtlAgent; import com.alibaba.ttl.threadpool.agent.TtlAgent;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.metadata.concurrent.MetadataWrap;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -35,7 +35,7 @@ import org.springframework.lang.NonNull;
* *
* @author wlx * @author wlx
*/ */
class MetadataExecutor implements Executor, MetadataWrap<Executor> { class MetadataExecutor implements Executor, TtlWrapper<Executor> {
private final Executor delegate; private final Executor delegate;
@ -71,8 +71,7 @@ class MetadataExecutor implements Executor, MetadataWrap<Executor> {
} }
@Override @Override
public Executor unWrap() { public Executor unwrap() {
return TtlExecutors.unwrap(delegate); return TtlExecutors.unwrap(delegate);
} }
} }

@ -123,7 +123,7 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic
} }
@Override @Override
public ExecutorService unWrap() { public ExecutorService unwrap() {
return TtlExecutors.unwrap(delegate); return TtlExecutors.unwrap(delegate);
} }
@ -143,5 +143,4 @@ class MetadataExecutorService extends MetadataExecutor implements ExecutorServic
public int hashCode() { public int hashCode() {
return Objects.hash(delegate); return Objects.hash(delegate);
} }
} }

@ -22,7 +22,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import com.tencent.cloud.metadata.concurrent.MetadataWrap; import com.alibaba.ttl.spi.TtlWrapper;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -85,7 +85,7 @@ public final class MetadataExecutors {
if (!isMetadataWrap(executor)) { if (!isMetadataWrap(executor)) {
return executor; return executor;
} }
return (T) ((MetadataExecutor) executor).unWrap(); return (T) ((MetadataExecutor) executor).unwrap();
} }
/** /**
@ -100,10 +100,9 @@ public final class MetadataExecutors {
* @return if the parameter executor is MetadataExecutor wrapper * @return if the parameter executor is MetadataExecutor wrapper
*/ */
public static <T extends Executor> boolean isMetadataWrap(@Nullable T executor) { public static <T extends Executor> boolean isMetadataWrap(@Nullable T executor) {
return executor instanceof MetadataWrap; return executor instanceof TtlWrapper;
} }
private MetadataExecutors() { private MetadataExecutors() {
} }
} }

@ -77,7 +77,7 @@ class MetadataScheduledExecutorService extends MetadataExecutorService
} }
@Override @Override
public ScheduledExecutorService unWrap() { public ScheduledExecutorService unwrap() {
return TtlExecutors.unwrap(delegate); return TtlExecutors.unwrap(delegate);
} }
@ -97,5 +97,4 @@ class MetadataScheduledExecutorService extends MetadataExecutorService
public int hashCode() { public int hashCode() {
return Objects.hash(delegate); return Objects.hash(delegate);
} }
} }

@ -46,6 +46,5 @@ public final class MetadataTestUtil {
customMetadata = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); customMetadata = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("2"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("2");
} }
} }

@ -124,5 +124,4 @@ public class MetadataExecutorServiceTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -74,7 +74,7 @@ public class MetadataExecutorTest {
metadataExecutor.execute(() -> { metadataExecutor.execute(() -> {
Map<String, String> fragmentContext = Map<String, String> fragmentContext =
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
Assertions.assertThat(fragmentContextAfterInit.equals(fragmentContext)); Assertions.assertThat(fragmentContextAfterInit.equals(fragmentContext)).isTrue();
}); });
// wait 200ms for metadataExecutor execute task // wait 200ms for metadataExecutor execute task
@ -84,7 +84,7 @@ public class MetadataExecutorTest {
@Test @Test
public void metadataExecutorUnWrap() { public void metadataExecutorUnWrap() {
MetadataExecutor metadataExecutor = new MetadataExecutor(executorService); MetadataExecutor metadataExecutor = new MetadataExecutor(executorService);
Executor executor = metadataExecutor.unWrap(); Executor executor = metadataExecutor.unwrap();
Assertions.assertThat(executor).isEqualTo(executorService); Assertions.assertThat(executor).isEqualTo(executorService);
} }
@ -129,5 +129,4 @@ public class MetadataExecutorTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -60,5 +60,4 @@ public class MetadataExecutorsTest {
MetadataExecutors.getMetadataScheduledExecutorService(scheduledExecutorService)); MetadataExecutors.getMetadataScheduledExecutorService(scheduledExecutorService));
Assertions.assertThat(unwrap).isEqualTo(scheduledExecutorService); Assertions.assertThat(unwrap).isEqualTo(scheduledExecutorService);
} }
} }

@ -85,5 +85,4 @@ public class MetadataScheduledExecutorServiceTest {
@SpringBootApplication @SpringBootApplication
protected static class TestApplication { protected static class TestApplication {
} }
} }

@ -105,6 +105,12 @@
<artifactId>spring-boot-actuator-autoconfigure</artifactId> <artifactId>spring-boot-actuator-autoconfigure</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
@ -34,7 +35,7 @@ import org.springframework.util.CollectionUtils;
*/ */
public final class MetadataContextHolder { public final class MetadataContextHolder {
private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new InheritableThreadLocal<>(); private static final ThreadLocal<MetadataContext> METADATA_CONTEXT = new TransmittableThreadLocal<>();
private static MetadataLocalProperties metadataLocalProperties; private static MetadataLocalProperties metadataLocalProperties;
private static StaticMetadataManager staticMetadataManager; private static StaticMetadataManager staticMetadataManager;

@ -241,7 +241,6 @@
<artifactId>transmittable-thread-local</artifactId> <artifactId>transmittable-thread-local</artifactId>
<version>${transmittable-thread-local.version}</version> <version>${transmittable-thread-local.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save