自定义注解+工厂管理采集器逻辑
0.自定义注解
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Downloader {
String value();
}
1.声明采集器
public interface Collector {
CollectResponse collect(CollectRequest request);
}
2.定义默认采集器,使用自定义注解修饰
@Downloader("default")
public class DefaultCollector implements Collector {
@Override
public CollectResponse collect(CollectRequest request) {
return CollectResponse.create(request);
}
}
3.定义工厂,管理Collector(核心用的扫描代码包+反射+注解值的对应关系)
public class CollectorFactory {
protected Reflections reflections;
private final Map<String, Collector> downloaders;
public CollectorFactory() {
this.reflections = new Reflections(
ConfigurationBuilder.build("delta.fetch.collector", Thread.currentThread().getContextClassLoader())
.setMetadataAdapter(new CollectorJavaReflectionAdapter())
.setExpandSuperTypes(false));
this.downloaders = new ConcurrentHashMap<>();
loadDownloaderBean(reflections);
}
private void loadDownloaderBean(Reflections reflections) {
Set<Class<?>> downloaderBeans = reflections.getTypesAnnotatedWith(Downloader.class);
for (Class<?> downloaderBean : downloaderBeans) {
addDownloaderBean(downloaderBean);
}
}
private void addDownloaderBean(Class<?> spiderBeanClass) {
Downloader downloader = spiderBeanClass.getAnnotation(Downloader.class);
String name = downloader.value();
try {
downloaders.put(name.toLowerCase(Locale.ROOT), (Collector) spiderBeanClass.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Collector matchDownloader(CollectRequest request) {
String serviceCode = request.getCollector();
return downloaders.getOrDefault(serviceCode.toLowerCase(Locale.ROOT), new DefaultCollector());
}
}
3.1 依赖
public class CollectorJavaReflectionAdapter extends JavaReflectionAdapter {
@Override
public Class<?> getOfCreateClassObject(Vfs.File file) throws Exception {
return getOfCreateClassObject(file, Thread.currentThread().getContextClassLoader());
}
}
4.基于注解+工厂的使用
private void processRequest(CollectRequest request) {
Collector collector = collectorFactory.matchDownloader(request);
// ......
}
5.工厂的初始化逻辑
public class SpiderInitialization implements InitializingBean {
private CollectorFactory collectorFactory = new CollectorFactory();
@Override
public void afterPropertiesSet() {
initCollect();
initEvent();
addShutdownHook();
}
// ......
}
原创:做时间的朋友