Refactor : generic type custom BlockingQueue support (#1036)

* generic refactor.

* add customeBlockingQueue generic type support.

---------

Co-authored-by: hongdan.qin <hongdan.qin@dmall.com>
pull/1073/head
lianyiwuming 3 years ago committed by GitHub
parent ed875cdf8e
commit 4210e65295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,7 @@ import java.util.concurrent.BlockingQueue;
/** /**
* Custom blocking-queue. * Custom blocking-queue.
*/ */
public interface CustomBlockingQueue { public interface CustomBlockingQueue<T> {
/** /**
* Gets the custom blocking queue type. * Gets the custom blocking queue type.
@ -45,5 +45,5 @@ public interface CustomBlockingQueue {
* *
* @return * @return
*/ */
BlockingQueue generateBlockingQueue(); BlockingQueue<T> generateBlockingQueue();
} }

@ -17,6 +17,8 @@
package cn.hippo4j.common.spi; package cn.hippo4j.common.spi;
import cn.hippo4j.common.spi.annotation.SingletonSPI;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -26,14 +28,20 @@ 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<>(); /**
* safe container
* key : SPI interface class type.
* value : collection whose elements are object of key's class type.
*/
private static final Map<Class<?>, Collection<?>> SERVICES = new ConcurrentHashMap<>();
private DynamicThreadPoolServiceLoader() {
}
/** /**
* Register. * Register.
@ -53,8 +61,8 @@ public class DynamicThreadPoolServiceLoader {
* @param <T> * @param <T>
* @return * @return
*/ */
private static <T> Collection<Object> load(final Class<T> serviceInterface) { private static <T> Collection<T> load(final Class<T> serviceInterface) {
Collection<Object> result = new LinkedList<>(); Collection<T> result = new LinkedList<>();
for (T each : ServiceLoader.load(serviceInterface)) { for (T each : ServiceLoader.load(serviceInterface)) {
result.add(each); result.add(each);
} }
@ -63,7 +71,7 @@ public class DynamicThreadPoolServiceLoader {
/** /**
* Get Service instances * Get Service instances
* *
* @param serviceClass serviceClass * @param serviceClass serviceClass
* @param <T> * @param <T>
* @return * @return

@ -0,0 +1,27 @@
package cn.hippo4j.common.spi;
import cn.hippo4j.common.executor.support.CustomBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* SPI whit generic type test.
*/
public class MyArrayBlockingQueue implements CustomBlockingQueue<Runnable> {
@Override
public Integer getType() {
return null;
}
@Override
public String getName() {
return this.getClass().getSimpleName();
}
@Override
public BlockingQueue<Runnable> generateBlockingQueue() {
return new LinkedBlockingQueue<>(20);
}
}
Loading…
Cancel
Save