refactor: 修改查询接口子类的方式

This commit is contained in:
CaptainB 2022-04-21 12:08:01 +08:00 committed by 刘瑞斌
parent f2b1a77e10
commit a16927f966
2 changed files with 19 additions and 7 deletions

View File

@ -18,11 +18,12 @@ import java.util.Set;
public class LoadTestConsumer {
public static final String CONSUME_ID = "load-test-data";
private static Set<Class<? extends LoadTestFinishEvent>> subTypes;
@KafkaListener(id = CONSUME_ID, topics = "${kafka.test.topic}", groupId = "${spring.kafka.consumer.group-id}")
public void consume(ConsumerRecord<?, String> record) {
LoadTestReport loadTestReport = JSON.parseObject(record.value(), LoadTestReport.class);
Reflections reflections = new Reflections(Application.class);
Set<Class<? extends LoadTestFinishEvent>> subTypes = reflections.getSubTypesOf(LoadTestFinishEvent.class);
Set<Class<? extends LoadTestFinishEvent>> subTypes = getClasses();
subTypes.forEach(s -> {
try {
CommonBeanFactory.getBean(s).execute(loadTestReport);
@ -31,4 +32,13 @@ public class LoadTestConsumer {
}
});
}
private Set<Class<? extends LoadTestFinishEvent>> getClasses() {
if (subTypes != null) {
return subTypes;
}
Reflections reflections = new Reflections(Application.class);
subTypes = reflections.getSubTypesOf(LoadTestFinishEvent.class);
return subTypes;
}
}

View File

@ -31,7 +31,6 @@ import org.reflections8.Reflections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@ -47,8 +46,10 @@ public class EngineFactory {
private static TestResourcePoolService testResourcePoolService;
private static Class<? extends KubernetesTestEngine> kubernetesTestEngineClass;
@PostConstruct
public void init() {
public static Class<? extends KubernetesTestEngine> getKubernetesTestEngineClass() {
if (kubernetesTestEngineClass != null) {
return kubernetesTestEngineClass;
}
Reflections reflections = new Reflections(Application.class);
Set<Class<? extends KubernetesTestEngine>> implClass = reflections.getSubTypesOf(KubernetesTestEngine.class);
for (Class<? extends KubernetesTestEngine> aClass : implClass) {
@ -56,6 +57,7 @@ public class EngineFactory {
// 第一个
break;
}
return kubernetesTestEngineClass;
}
public static Engine createEngine(LoadTestReportWithBLOBs loadTestReport) {
@ -79,7 +81,7 @@ public class EngineFactory {
}
if (type == ResourcePoolTypeEnum.K8S) {
try {
return (Engine) ConstructorUtils.invokeConstructor(kubernetesTestEngineClass, loadTestReport);
return (Engine) ConstructorUtils.invokeConstructor(getKubernetesTestEngineClass(), loadTestReport);
} catch (Exception e) {
LogUtil.error(e);
return null;
@ -90,7 +92,7 @@ public class EngineFactory {
public static Engine createApiEngine(JmeterRunRequestDTO runRequest) {
try {
return (Engine) ConstructorUtils.invokeConstructor(kubernetesTestEngineClass, runRequest);
return (Engine) ConstructorUtils.invokeConstructor(getKubernetesTestEngineClass(), runRequest);
} catch (Exception e) {
LogUtil.error(e);
MSException.throwException(e.getMessage());