From 49089649ba6247db656b0a6bd8b5beb628c11c8c Mon Sep 17 00:00:00 2001 From: CaptainB Date: Thu, 21 Apr 2022 18:19:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=8E=BB=E6=8E=89=E5=8F=8D=E5=B0=84?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8spring=E7=9A=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=AD=90=E7=B1=BB=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/consumer/LoadTestConsumer.java | 35 ++++--------------- .../commons/utils/CommonBeanFactory.java | 6 ++++ 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/consumer/LoadTestConsumer.java b/backend/src/main/java/io/metersphere/commons/consumer/LoadTestConsumer.java index c29b364cae..26cc8aab7f 100644 --- a/backend/src/main/java/io/metersphere/commons/consumer/LoadTestConsumer.java +++ b/backend/src/main/java/io/metersphere/commons/consumer/LoadTestConsumer.java @@ -5,52 +5,29 @@ import io.metersphere.base.domain.LoadTestReport; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; 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.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Set; -import java.util.concurrent.Executors; +import java.util.Map; @Service @Transactional(rollbackFor = Exception.class) -public class LoadTestConsumer implements ApplicationRunner { +public class LoadTestConsumer { public static final String CONSUME_ID = "load-test-data"; - private static Set> subTypes; @KafkaListener(id = CONSUME_ID, topics = "${kafka.test.topic}", groupId = "${spring.kafka.consumer.group-id}") public void consume(ConsumerRecord record) { LoadTestReport loadTestReport = JSON.parseObject(record.value(), LoadTestReport.class); - Set> subTypes = getSubTypes(); - subTypes.forEach(s -> { + + Map subTypes = CommonBeanFactory.getBeansOfType(LoadTestFinishEvent.class); + subTypes.forEach((k, t) -> { try { - CommonBeanFactory.getBean(s).execute(loadTestReport); + t.execute(loadTestReport); } catch (Exception e) { LogUtil.error(e); } }); } - - private synchronized Set> 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()); - }); - } } diff --git a/backend/src/main/java/io/metersphere/commons/utils/CommonBeanFactory.java b/backend/src/main/java/io/metersphere/commons/utils/CommonBeanFactory.java index 4e1bc54b52..1653b98429 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/CommonBeanFactory.java +++ b/backend/src/main/java/io/metersphere/commons/utils/CommonBeanFactory.java @@ -5,6 +5,8 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import java.util.Map; + public class CommonBeanFactory implements ApplicationContextAware { private static ApplicationContext context; @@ -30,5 +32,9 @@ public class CommonBeanFactory implements ApplicationContextAware { return null; } } + + public static Map getBeansOfType(Class className) { + return context.getBeansOfType(className); + } }