diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoader.java b/hippo4j-common/src/main/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoader.java index ecfdbacd..2885259a 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoader.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoader.java @@ -26,12 +26,14 @@ import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import cn.hippo4j.common.spi.annotation.SingletonSPI; + /** * Dynamic thread-pool service loader. */ public class DynamicThreadPoolServiceLoader { - private static final Map, Collection> SERVICES = new ConcurrentHashMap(); + private static final Map, Collection> SERVICES = new ConcurrentHashMap<>(); /** * Register. @@ -59,6 +61,16 @@ public class DynamicThreadPoolServiceLoader { return result; } + /** + * getServiceInstances + * @param serviceClass serviceClass + * @param + * @return + */ + public static Collection getServiceInstances(final Class serviceClass) { + return null == serviceClass.getAnnotation(SingletonSPI.class) ? newServiceInstances(serviceClass) : getSingletonServiceInstances(serviceClass); + } + /** * Get singleton service instances. * @@ -66,6 +78,7 @@ public class DynamicThreadPoolServiceLoader { * @param * @return */ + @SuppressWarnings("unchecked") public static Collection getSingletonServiceInstances(final Class service) { return (Collection) SERVICES.getOrDefault(service, Collections.emptyList()); } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/spi/annotation/SingletonSPI.java b/hippo4j-common/src/main/java/cn/hippo4j/common/spi/annotation/SingletonSPI.java new file mode 100644 index 00000000..11388776 --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/spi/annotation/SingletonSPI.java @@ -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 { +} diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoaderTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoaderTest.java index 8387793c..6f164e6d 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoaderTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/DynamicThreadPoolServiceLoaderTest.java @@ -17,5 +17,54 @@ 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 { + + @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 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 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 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 instances = DynamicThreadPoolServiceLoader.getServiceInstances(TestInterfaceSpi.class); + assertThat(instances.iterator().next(), not(DynamicThreadPoolServiceLoader.getSingletonServiceInstances(TestInterfaceSpi.class).iterator().next())); + + } } diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpi.java b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpi.java new file mode 100644 index 00000000..1c4427ff --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpi.java @@ -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 { +} diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpiImpl.java b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpiImpl.java new file mode 100644 index 00000000..a55e16f1 --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestInterfaceSpiImpl.java @@ -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 { +} diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpi.java b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpi.java new file mode 100644 index 00000000..30d7202c --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpi.java @@ -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 { +} diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpiImpl.java b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpiImpl.java new file mode 100644 index 00000000..2acc2ef8 --- /dev/null +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/spi/TestSingletonInterfaceSpiImpl.java @@ -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 { +} diff --git a/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.TestInterfaceSpi b/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.TestInterfaceSpi new file mode 100644 index 00000000..2c941eb8 --- /dev/null +++ b/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.TestInterfaceSpi @@ -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 \ No newline at end of file diff --git a/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.TestSingletonInterfaceSpi b/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.TestSingletonInterfaceSpi new file mode 100644 index 00000000..69b55ffd --- /dev/null +++ b/hippo4j-common/src/test/resources/META-INF/services/cn.hippo4j.common.spi.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.TestSingletonInterfaceSpiImpl \ No newline at end of file diff --git a/hippo4j-monitor/hippo4j-monitor-elasticsearch/src/main/java/cn/hippo4j/monitor/elasticsearch/DynamicThreadPoolElasticSearchMonitorHandler.java b/hippo4j-monitor/hippo4j-monitor-elasticsearch/src/main/java/cn/hippo4j/monitor/elasticsearch/DynamicThreadPoolElasticSearchMonitorHandler.java index 203d2457..f476ae1c 100644 --- a/hippo4j-monitor/hippo4j-monitor-elasticsearch/src/main/java/cn/hippo4j/monitor/elasticsearch/DynamicThreadPoolElasticSearchMonitorHandler.java +++ b/hippo4j-monitor/hippo4j-monitor-elasticsearch/src/main/java/cn/hippo4j/monitor/elasticsearch/DynamicThreadPoolElasticSearchMonitorHandler.java @@ -147,6 +147,7 @@ public class DynamicThreadPoolElasticSearchMonitorHandler extends AbstractDynami @Getter @Builder private static class EsIndex { + String index; String type; String mapping;