parent
01f81b98cf
commit
0fc47467e8
@ -1,101 +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;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link MetadataCallable} decorate {@link Callable} to get {@link MetadataContext} value
|
|
||||||
* and transfer it to the time of {@link Callable} execution, needed when use {@link Callable} to thread pool.
|
|
||||||
* <p>
|
|
||||||
* Use factory methods {@link #get} / {@link #gets} to create instance.
|
|
||||||
*
|
|
||||||
* @author wlx
|
|
||||||
*/
|
|
||||||
public final class MetadataCallable<V> implements Callable<V>,
|
|
||||||
MetadataWrap<Callable<V>> {
|
|
||||||
|
|
||||||
private final Callable<V> delegate;
|
|
||||||
|
|
||||||
private final AtomicReference<MetadataContext> metadataContextReference;
|
|
||||||
|
|
||||||
private MetadataCallable(Callable<V> delegate) {
|
|
||||||
this.delegate = delegate;
|
|
||||||
this.metadataContextReference = new AtomicReference<>(MetadataContextHolder.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public V call() throws Exception {
|
|
||||||
MetadataContext metadataContext = metadataContextReference.get();
|
|
||||||
MetadataContext metadataContextBackup = MetadataContextHolder.get();
|
|
||||||
MetadataContextHolder.set(metadataContext);
|
|
||||||
try {
|
|
||||||
return delegate.call();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
MetadataContextHolder.set(metadataContextBackup);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create {@link MetadataCallable} instance.
|
|
||||||
*
|
|
||||||
* @param delegate delegate
|
|
||||||
* @param <V> MetadataCallable return type
|
|
||||||
* @return {@link MetadataCallable} instance
|
|
||||||
*/
|
|
||||||
public static <V> Callable<V> get(Callable<V> delegate) {
|
|
||||||
if (null == delegate || delegate instanceof MetadataCallable) {
|
|
||||||
return delegate;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return new MetadataCallable<>(delegate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create some {@link MetadataCallable} instance.
|
|
||||||
*
|
|
||||||
* @param delegates delegates
|
|
||||||
* @param <V> MetadataCallable return type
|
|
||||||
* @return some {@link MetadataCallable} instance
|
|
||||||
*/
|
|
||||||
public static <V> List<Callable<V>> gets(Collection<? extends Callable<V>> delegates) {
|
|
||||||
if (delegates == null) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
return delegates.stream().map(
|
|
||||||
MetadataCallable::get
|
|
||||||
).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Callable<V> unWrap() {
|
|
||||||
return this.delegate;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +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;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link MetadataRunnable} decorate {@link Runnable} to get {@link MetadataContext} value
|
|
||||||
* and transfer it to the time of {@link Runnable} execution, needed when use {@link Runnable} to thread pool.
|
|
||||||
* <p>
|
|
||||||
* Use factory methods {@link #get} / {@link #gets} to create instance.
|
|
||||||
* <p>
|
|
||||||
*
|
|
||||||
* @author wlx
|
|
||||||
*/
|
|
||||||
public final class MetadataRunnable implements Runnable,
|
|
||||||
MetadataWrap<Runnable> {
|
|
||||||
|
|
||||||
private final Runnable delegate;
|
|
||||||
|
|
||||||
private final AtomicReference<MetadataContext> metadataContextReference;
|
|
||||||
|
|
||||||
private MetadataRunnable(Runnable delegate) {
|
|
||||||
this.delegate = delegate;
|
|
||||||
this.metadataContextReference = new AtomicReference<>(MetadataContextHolder.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
MetadataContext metadataContext = metadataContextReference.get();
|
|
||||||
MetadataContext metadataContextBackup = MetadataContextHolder.get();
|
|
||||||
MetadataContextHolder.set(metadataContext);
|
|
||||||
try {
|
|
||||||
delegate.run();
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
MetadataContextHolder.set(metadataContextBackup);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create {@link MetadataRunnable} instance.
|
|
||||||
*
|
|
||||||
* @param delegate delegate
|
|
||||||
* @return MetadataRunnable instance
|
|
||||||
*/
|
|
||||||
public static Runnable get(Runnable delegate) {
|
|
||||||
if (null == delegate || delegate instanceof MetadataRunnable) {
|
|
||||||
return delegate;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return new MetadataRunnable(delegate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method to create {@link MetadataRunnable} instance.
|
|
||||||
*
|
|
||||||
* @param delegates delegates
|
|
||||||
* @return MetadataRunnable instances
|
|
||||||
*/
|
|
||||||
public static List<Runnable> gets(Collection<Runnable> delegates) {
|
|
||||||
if (delegates == null) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
return delegates.stream().map(
|
|
||||||
MetadataRunnable::get
|
|
||||||
).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Runnable unWrap() {
|
|
||||||
return this.delegate;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,131 +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;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
import org.assertj.core.api.Assertions;
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for {@link MetadataCallable}.
|
|
||||||
*
|
|
||||||
* @author wlx
|
|
||||||
*/
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
|
||||||
classes = MetadataCallableTest.TestApplication.class,
|
|
||||||
properties = {"spring.config.location = classpath:application-test.yml",
|
|
||||||
"spring.main.web-application-type = servlet",
|
|
||||||
"spring.cloud.gateway.enabled = false"})
|
|
||||||
public class MetadataCallableTest {
|
|
||||||
|
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(1);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void threadMultiplexingTest() throws InterruptedException, ExecutionException, TimeoutException {
|
|
||||||
|
|
||||||
Future<Map<String, String>> future = executor.submit(() -> {
|
|
||||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
|
|
||||||
});
|
|
||||||
Map<String, String> map = future.get(200, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
// init after new Task, won't see parent value in in task!
|
|
||||||
MetadataTestUtil.initMetadataContext();
|
|
||||||
|
|
||||||
Future<Map<String, String>> future1 = executor.submit(() -> {
|
|
||||||
return MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
});
|
|
||||||
|
|
||||||
Map<String, String> map1 = future1.get(200, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
Assertions.assertThat(fragmentContext.get("a")).isEqualTo("1");
|
|
||||||
Assertions.assertThat(fragmentContext.get("b")).isEqualTo("2");
|
|
||||||
|
|
||||||
// init after new Task, won't see parent value in in task!,so before init and after init task res will be same!
|
|
||||||
Assertions.assertThat(map.equals(map1)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void metadataCallableTest() throws InterruptedException, ExecutionException, TimeoutException {
|
|
||||||
Future<Map<String, String>> future = executor.submit(
|
|
||||||
MetadataCallable.get(
|
|
||||||
() -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
Map<String, String> map = future.get(200, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
Assertions.assertThat(map.equals(MetadataContextHolder.get()
|
|
||||||
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)));
|
|
||||||
|
|
||||||
MetadataTestUtil.initMetadataContext();
|
|
||||||
|
|
||||||
Future<Map<String, String>> future1 = executor.submit(
|
|
||||||
MetadataCallable.get(
|
|
||||||
() -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
Map<String, String> map1 = future1.get(200, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
Assertions.assertThat(fragmentContext.get("a")).isEqualTo("1");
|
|
||||||
Assertions.assertThat(fragmentContext.get("b")).isEqualTo("2");
|
|
||||||
|
|
||||||
Assertions.assertThat(fragmentContext.equals(map1)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void metadataCallableWrap() {
|
|
||||||
Callable<Map<String, String>> callable = MetadataCallable.get(
|
|
||||||
() -> MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)
|
|
||||||
);
|
|
||||||
Assertions.assertThat(callable instanceof MetadataCallable).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void cleanUp() {
|
|
||||||
executor.shutdownNow();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
protected static class TestApplication {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,133 +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;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
|
||||||
import org.assertj.core.api.Assertions;
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for {@link MetadataRunnable}.
|
|
||||||
*
|
|
||||||
* @author wlx
|
|
||||||
*/
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT,
|
|
||||||
classes = MetadataRunnableTest.TestApplication.class,
|
|
||||||
properties = {"spring.config.location = classpath:application-test.yml",
|
|
||||||
"spring.main.web-application-type = servlet",
|
|
||||||
"spring.cloud.gateway.enabled = false"})
|
|
||||||
public class MetadataRunnableTest {
|
|
||||||
|
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(1);
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void threadMultiplexingTest() {
|
|
||||||
Map<String, String> fragmentContextBeforeInit =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
|
|
||||||
executor.submit(() -> {
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
// init after new Task, won't see parent value in in task!
|
|
||||||
MetadataTestUtil.initMetadataContext();
|
|
||||||
|
|
||||||
Map<String, String> fragmentContextAfterInit =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
|
|
||||||
|
|
||||||
executor.submit(() -> {
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
// init after new Task, won't see parent value in in task!
|
|
||||||
// so before init and after init task res will be same!
|
|
||||||
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
Assertions.assertThat(fragmentContextAfterInit.get("a")).isEqualTo("1");
|
|
||||||
Assertions.assertThat(fragmentContextAfterInit.get("b")).isEqualTo("2");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void metadataRunnableTest() {
|
|
||||||
Map<String, String> fragmentContextBeforeInit =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
|
|
||||||
executor.submit(MetadataRunnable.get(
|
|
||||||
() -> {
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext));
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
MetadataTestUtil.initMetadataContext();
|
|
||||||
|
|
||||||
Map<String, String> fragmentContextAfterInit =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
|
|
||||||
executor.submit(MetadataRunnable.get(
|
|
||||||
() -> {
|
|
||||||
Map<String, String> fragmentContext =
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
Assertions.assertThat(fragmentContextBeforeInit.equals(fragmentContext)).isFalse();
|
|
||||||
Assertions.assertThat(fragmentContextAfterInit.equals(fragmentContext)).isTrue();
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
Assertions.assertThat(fragmentContextAfterInit.get("a")).isEqualTo("1");
|
|
||||||
Assertions.assertThat(fragmentContextAfterInit.get("b")).isEqualTo("2");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void metadataRunnableWrapTest() {
|
|
||||||
Runnable runnable = MetadataRunnable.get(
|
|
||||||
() -> {
|
|
||||||
MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
Assertions.assertThat(runnable instanceof MetadataRunnable).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void cleanUp() {
|
|
||||||
executor.shutdownNow();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
protected static class TestApplication {
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue