mirror of https://github.com/longtai-cn/hippo4j
commit
472effafb8
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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 cn.hippo4j.threadpool.dynamic.mode.config.parser;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.toolkit.CollectionUtil;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.commons.collections.MapUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Json config parser.
|
||||||
|
*/
|
||||||
|
public class JsonConfigParser extends AbstractConfigParser {
|
||||||
|
private static final ObjectMapper MAPPER;
|
||||||
|
private static final String DOT = ".";
|
||||||
|
private static final String LEFT_BRACE = "{";
|
||||||
|
private static final String RIGHT_BRACE = "{";
|
||||||
|
private static final String LEFT_BRACKET = "[";
|
||||||
|
private static final String RIGHT_BRACKET = "]";
|
||||||
|
|
||||||
|
static {
|
||||||
|
MAPPER = new ObjectMapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Object, Object> doParse(String content, String prefix) throws IOException {
|
||||||
|
|
||||||
|
Map<String, Object> originMap = MAPPER.readValue(content, LinkedHashMap.class);
|
||||||
|
Map<Object, Object> result = new HashMap<>();
|
||||||
|
|
||||||
|
flatMap(result, originMap, prefix);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void flatMap(Map<Object, Object> result, Map<String, Object> dataMap, String prefix) {
|
||||||
|
|
||||||
|
if (MapUtils.isEmpty(dataMap)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dataMap.forEach((k, v) -> {
|
||||||
|
String fullKey = genFullKey(prefix, k);
|
||||||
|
if (v instanceof Map) {
|
||||||
|
flatMap(result, (Map<String, Object>) v, fullKey);
|
||||||
|
return;
|
||||||
|
} else if (v instanceof Collection) {
|
||||||
|
int count = 0;
|
||||||
|
for (Object obj : (Collection<Object>) v) {
|
||||||
|
String kk = LEFT_BRACKET + (count++) + RIGHT_BRACKET;
|
||||||
|
flatMap(result, Collections.singletonMap(kk, obj), fullKey);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.put(fullKey, v);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private String genFullKey(String prefix, String key) {
|
||||||
|
if (StringUtils.isEmpty(prefix)) {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return key.startsWith(LEFT_BRACE) ? prefix.concat(key) : prefix.concat(DOT).concat(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Object, Object> doParse(String content) throws IOException {
|
||||||
|
if (StringUtils.isEmpty(content)) {
|
||||||
|
return new HashMap<>(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return doParse(content,"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||||
|
return CollectionUtil.newArrayList(ConfigFileTypeEnum.JSON, ConfigFileTypeEnum.JSON);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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 brave.internal;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public abstract class WrappingExecutorService implements ExecutorService {
|
||||||
|
|
||||||
|
protected WrappingExecutorService() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract ExecutorService delegate();
|
||||||
|
|
||||||
|
protected abstract <R> Callable<R> wrap(Callable<R> var1);
|
||||||
|
|
||||||
|
protected abstract Runnable wrap(Runnable var1);
|
||||||
|
|
||||||
|
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
|
||||||
|
return this.delegate().awaitTermination(timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
|
||||||
|
return this.delegate().invokeAll(this.wrap(tasks));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
|
||||||
|
return this.delegate().invokeAll(this.wrap(tasks), timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
|
||||||
|
return this.delegate().invokeAny(this.wrap(tasks));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
return this.delegate().invokeAny(this.wrap(tasks), timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isShutdown() {
|
||||||
|
return this.delegate().isShutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTerminated() {
|
||||||
|
return this.delegate().isTerminated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
this.delegate().shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Runnable> shutdownNow() {
|
||||||
|
return this.delegate().shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Runnable task) {
|
||||||
|
this.delegate().execute(this.wrap(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Future<T> submit(Callable<T> task) {
|
||||||
|
return this.delegate().submit(this.wrap(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Future<?> submit(Runnable task) {
|
||||||
|
return this.delegate().submit(this.wrap(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Future<T> submit(Runnable task, T result) {
|
||||||
|
return this.delegate().submit(this.wrap(task), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
<T> Collection<? extends Callable<T>> wrap(Collection<? extends Callable<T>> tasks) {
|
||||||
|
ArrayList<Callable<T>> result = new ArrayList(tasks.size());
|
||||||
|
Iterator var3 = tasks.iterator();
|
||||||
|
|
||||||
|
while (var3.hasNext()) {
|
||||||
|
Callable<T> task = (Callable) var3.next();
|
||||||
|
result.add(this.wrap(task));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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 cn.hippo4j.core;
|
||||||
|
|
||||||
|
import brave.internal.WrappingExecutorService;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
|
public class CustomWrappingExecutorService extends WrappingExecutorService {
|
||||||
|
|
||||||
|
ExecutorService delegate;
|
||||||
|
|
||||||
|
public CustomWrappingExecutorService() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomWrappingExecutorService(ExecutorService delegate) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExecutorService delegate() {
|
||||||
|
return delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected <R> Callable<R> wrap(Callable<R> callable) {
|
||||||
|
return callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Runnable wrap(Runnable runnable) {
|
||||||
|
return runnable;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* 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 cn.hippo4j.core.adapter;
|
||||||
|
|
||||||
|
import cn.hippo4j.common.executor.support.RunsOldestTaskPolicy;
|
||||||
|
import cn.hippo4j.common.handler.ZipkinExecutorAdapter;
|
||||||
|
import cn.hippo4j.core.CustomWrappingExecutorService;
|
||||||
|
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.scheduling.concurrent.DefaultManagedAwareThreadFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.SynchronousQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test for ${@link ZipkinExecutorAdapter}
|
||||||
|
* */
|
||||||
|
@Slf4j
|
||||||
|
public class ZipkinExecutorAdapterTest {
|
||||||
|
|
||||||
|
ZipkinExecutorAdapter zipkinExecutorAdapter = new ZipkinExecutorAdapter();
|
||||||
|
Executor dynamicThreadPool = ThreadPoolBuilder.builder()
|
||||||
|
.threadPoolId("test")
|
||||||
|
.dynamicPool()
|
||||||
|
.corePoolSize(1)
|
||||||
|
.maximumPoolSize(2)
|
||||||
|
.keepAliveTime(1000)
|
||||||
|
.timeUnit(TimeUnit.MICROSECONDS)
|
||||||
|
.threadFactory(new DefaultManagedAwareThreadFactory())
|
||||||
|
.workQueue(new SynchronousQueue<>())
|
||||||
|
.rejected(new RunsOldestTaskPolicy())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMatch() {
|
||||||
|
Object executor = new CustomWrappingExecutorService(Executors.newCachedThreadPool());
|
||||||
|
Assert.assertTrue(zipkinExecutorAdapter.match(executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnwrap() {
|
||||||
|
Object executor = new CustomWrappingExecutorService(Executors.newCachedThreadPool());
|
||||||
|
ThreadPoolExecutor unwrap = zipkinExecutorAdapter.unwrap(executor);
|
||||||
|
Assert.assertNull(unwrap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReplace() {
|
||||||
|
Object executor = new CustomWrappingExecutorService(Executors.newCachedThreadPool());
|
||||||
|
CustomWrappingExecutorService executorChange = (CustomWrappingExecutorService)executor;
|
||||||
|
ExecutorService beforeReplace = executorChange.delegate();
|
||||||
|
zipkinExecutorAdapter.replace(executor, dynamicThreadPool);
|
||||||
|
ExecutorService afterReplace = executorChange.delegate();
|
||||||
|
|
||||||
|
Assert.assertNotSame(beforeReplace, afterReplace);
|
||||||
|
Assert.assertSame(afterReplace, dynamicThreadPool);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue