pull/957/head
weihu 3 years ago
parent e670ba28ed
commit 8ef953c6f4

@ -26,12 +26,14 @@ import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import cn.hippo4j.common.spi.annotation.SingletonSPI;
/** /**
* Dynamic thread-pool service loader. * Dynamic thread-pool service loader.
*/ */
public class DynamicThreadPoolServiceLoader { public class DynamicThreadPoolServiceLoader {
private static final Map<Class<?>, Collection<Object>> SERVICES = new ConcurrentHashMap(); private static final Map<Class<?>, Collection<Object>> SERVICES = new ConcurrentHashMap<>();
/** /**
* Register. * Register.
@ -59,6 +61,16 @@ public class DynamicThreadPoolServiceLoader {
return result; return result;
} }
/**
* getServiceInstances
* @param serviceClass serviceClass
* @param <T>
* @return
*/
public static <T> Collection<T> getServiceInstances(final Class<T> serviceClass) {
return null == serviceClass.getAnnotation(SingletonSPI.class) ? newServiceInstances(serviceClass) : getSingletonServiceInstances(serviceClass);
}
/** /**
* Get singleton service instances. * Get singleton service instances.
* *
@ -66,6 +78,7 @@ public class DynamicThreadPoolServiceLoader {
* @param <T> * @param <T>
* @return * @return
*/ */
@SuppressWarnings("unchecked")
public static <T> Collection<T> getSingletonServiceInstances(final Class<T> service) { public static <T> Collection<T> getSingletonServiceInstances(final Class<T> service) {
return (Collection<T>) SERVICES.getOrDefault(service, Collections.emptyList()); return (Collection<T>) SERVICES.getOrDefault(service, Collections.emptyList());
} }

@ -0,0 +1,33 @@
/*
* 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.common.spi.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*@author : wh
*@date : 2022/11/8 21:54
*@description:
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SingletonSPI {
}

@ -17,5 +17,54 @@
package cn.hippo4j.common.spi; package cn.hippo4j.common.spi;
import java.util.Collection;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertTrue;
public final class DynamicThreadPoolServiceLoaderTest { public final class DynamicThreadPoolServiceLoaderTest {
@Test
public void assertRegister() {
DynamicThreadPoolServiceLoader.register(Collection.class);
Collection<?> collections = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(Collection.class);
assertTrue(collections.isEmpty());
}
@Test
public void assertGetSingletonServiceInstances() {
DynamicThreadPoolServiceLoader.register(TestSingletonInterfaceSpi.class);
Collection<TestSingletonInterfaceSpi> instances = DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestSingletonInterfaceSpi.class);
assertThat(instances.size(), equalTo(1));
assertThat(instances.iterator().next(), is(DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestSingletonInterfaceSpi.class).iterator().next()));
}
@Test
public void assertNewServiceInstances() {
DynamicThreadPoolServiceLoader.register(TestSingletonInterfaceSpi.class);
Collection<TestSingletonInterfaceSpi> instances = DynamicThreadPoolServiceLoader.newServiceInstances(TestSingletonInterfaceSpi.class);
assertThat(instances.size(), equalTo(1));
assertThat(instances.iterator().next(), not(DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestSingletonInterfaceSpi.class).iterator().next()));
}
@Test
public void assertGetServiceInstancesWhenIsSingleton() {
DynamicThreadPoolServiceLoader.register(TestSingletonInterfaceSpi.class);
Collection<TestSingletonInterfaceSpi> instances = DynamicThreadPoolServiceLoader.getServiceInstances(TestSingletonInterfaceSpi.class);
assertThat(instances.iterator().next(), is(DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestSingletonInterfaceSpi.class).iterator().next()));
}
@Test
public void assertGetServiceInstancesWhenNotSingleton() {
DynamicThreadPoolServiceLoader.register(TestInterfaceSpi.class);
Collection<TestInterfaceSpi> instances = DynamicThreadPoolServiceLoader.getServiceInstances(TestInterfaceSpi.class);
assertThat(instances.iterator().next(), not(DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestInterfaceSpi.class).iterator().next()));
}
} }

@ -0,0 +1,26 @@
/*
* 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.common.spi;
/**
*@author : wh
*@date : 2022/11/8 22:01
*@description:
*/
public interface TestInterfaceSpi {
}

@ -0,0 +1,26 @@
/*
* 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.common.spi;
/**
*@author : wh
*@date : 2022/11/8 22:01
*@description:
*/
public class TestInterfaceSpiImpl implements TestInterfaceSpi {
}

@ -0,0 +1,29 @@
/*
* 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.common.spi;
import cn.hippo4j.common.spi.annotation.SingletonSPI;
/**
*@author : wh
*@date : 2022/11/8 21:02
*@description:
*/
@SingletonSPI
public interface TestSingletonInterfaceSpi {
}

@ -0,0 +1,26 @@
/*
* 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.common.spi;
/**
*@author : wh
*@date : 2022/11/8 21:03
*@description:
*/
public class TestSingletonInterfaceSpiImpl implements TestSingletonInterfaceSpi {
}

@ -0,0 +1,18 @@
#
# 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.
#
cn.hippo4j.common.spi.TestInterfaceSpiImpl

@ -0,0 +1,18 @@
#
# 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.
#
cn.hippo4j.common.spi.TestSingletonInterfaceSpiImpl

@ -147,6 +147,7 @@ public class DynamicThreadPoolElasticSearchMonitorHandler extends AbstractDynami
@Getter @Getter
@Builder @Builder
private static class EsIndex { private static class EsIndex {
String index; String index;
String type; String type;
String mapping; String mapping;

Loading…
Cancel
Save