perf: 去掉反射,使用spring的获取子类的方式
This commit is contained in:
parent
55ef2a3382
commit
49089649ba
|
@ -5,52 +5,29 @@ import io.metersphere.base.domain.LoadTestReport;
|
||||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
import org.reflections.Reflections;
|
|
||||||
import org.reflections.scanners.Scanners;
|
|
||||||
import org.springframework.boot.ApplicationArguments;
|
|
||||||
import org.springframework.boot.ApplicationRunner;
|
|
||||||
import org.springframework.kafka.annotation.KafkaListener;
|
import org.springframework.kafka.annotation.KafkaListener;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public class LoadTestConsumer implements ApplicationRunner {
|
public class LoadTestConsumer {
|
||||||
public static final String CONSUME_ID = "load-test-data";
|
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}")
|
@KafkaListener(id = CONSUME_ID, topics = "${kafka.test.topic}", groupId = "${spring.kafka.consumer.group-id}")
|
||||||
public void consume(ConsumerRecord<?, String> record) {
|
public void consume(ConsumerRecord<?, String> record) {
|
||||||
LoadTestReport loadTestReport = JSON.parseObject(record.value(), LoadTestReport.class);
|
LoadTestReport loadTestReport = JSON.parseObject(record.value(), LoadTestReport.class);
|
||||||
Set<Class<? extends LoadTestFinishEvent>> subTypes = getSubTypes();
|
|
||||||
subTypes.forEach(s -> {
|
Map<String, LoadTestFinishEvent> subTypes = CommonBeanFactory.getBeansOfType(LoadTestFinishEvent.class);
|
||||||
|
subTypes.forEach((k, t) -> {
|
||||||
try {
|
try {
|
||||||
CommonBeanFactory.getBean(s).execute(loadTestReport);
|
t.execute(loadTestReport);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized Set<Class<? extends LoadTestFinishEvent>> getSubTypes() {
|
|
||||||
if (subTypes != null) {
|
|
||||||
return subTypes;
|
|
||||||
}
|
|
||||||
Reflections reflections = new Reflections("io.metersphere", Scanners.SubTypes);
|
|
||||||
subTypes = reflections.getSubTypesOf(LoadTestFinishEvent.class);
|
|
||||||
return subTypes;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
|
||||||
Executors.newSingleThreadExecutor().execute(() -> {
|
|
||||||
LogUtil.info("查询 LoadTestFinishEvent 实现类:");
|
|
||||||
getSubTypes();
|
|
||||||
LogUtil.info("查询 LoadTestFinishEvent 实现类: " + subTypes.size());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommonBeanFactory implements ApplicationContextAware {
|
public class CommonBeanFactory implements ApplicationContextAware {
|
||||||
private static ApplicationContext context;
|
private static ApplicationContext context;
|
||||||
|
|
||||||
|
@ -30,5 +32,9 @@ public class CommonBeanFactory implements ApplicationContextAware {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> Map<String, T> getBeansOfType(Class<T> className) {
|
||||||
|
return context.getBeansOfType(className);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue