feat: 添加各模块执行监听
This commit is contained in:
parent
cd3716b258
commit
54640dc32d
|
@ -0,0 +1,36 @@
|
||||||
|
package io.metersphere.listener;
|
||||||
|
|
||||||
|
import io.metersphere.api.event.APIEventSource;
|
||||||
|
import io.metersphere.plan.listener.ExecEventListener;
|
||||||
|
import io.metersphere.sdk.util.CommonBeanFactory;
|
||||||
|
import io.metersphere.sdk.util.LogUtils;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AppStartListener implements ApplicationRunner {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
LogUtils.info("================= 应用启动 =================");
|
||||||
|
// 注册所有监听源
|
||||||
|
LogUtils.info("初始化接口事件源");
|
||||||
|
APIEventSource apiEventSource = CommonBeanFactory.getBean(APIEventSource.class);
|
||||||
|
LogUtils.info("初始化性能测试事件源");
|
||||||
|
//LoadEventSource loadEventSource = CommonBeanFactory.getBean(LoadEventSource.class);
|
||||||
|
//todo: 注册其他事件源
|
||||||
|
|
||||||
|
|
||||||
|
// 创建监听器对象并注册到多个事件源
|
||||||
|
ExecEventListener listener = CommonBeanFactory.getBean(ExecEventListener.class);
|
||||||
|
apiEventSource.addListener(listener);
|
||||||
|
|
||||||
|
//todo: 注册其他监听器
|
||||||
|
//loadEventSource.addListener(listener);
|
||||||
|
|
||||||
|
// 触发事件
|
||||||
|
apiEventSource.fireEvent("API", "Event after removing the listener test.");
|
||||||
|
//loadEventSource.fireEvent("LOAD","Event after removing the listener.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package io.metersphere.sdk.listener;
|
||||||
|
|
||||||
|
public class Event {
|
||||||
|
private String message;
|
||||||
|
private String module;
|
||||||
|
|
||||||
|
public Event(String module, String message) {
|
||||||
|
this.module = module;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModule() {
|
||||||
|
return module;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package io.metersphere.sdk.listener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听所有执行结果
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public interface EventListener<T> {
|
||||||
|
void onEvent(T event);
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package io.metersphere.sdk.listener;
|
||||||
|
|
||||||
|
public interface EventSource {
|
||||||
|
/**
|
||||||
|
* @param listener
|
||||||
|
*/
|
||||||
|
void addListener(EventListener<Event> listener);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param module
|
||||||
|
* @param message
|
||||||
|
*/
|
||||||
|
void fireEvent(String module, String message);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package io.metersphere.api.event;
|
||||||
|
|
||||||
|
import io.metersphere.sdk.listener.Event;
|
||||||
|
import io.metersphere.sdk.listener.EventListener;
|
||||||
|
import io.metersphere.sdk.listener.EventSource;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class APIEventSource implements EventSource {
|
||||||
|
private EventListener<Event> listener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(EventListener<Event> listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fireEvent(String module, String message) {
|
||||||
|
Event event = new Event("API", message);
|
||||||
|
listener.onEvent(event);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package io.metersphere.api.controller;
|
||||||
|
|
||||||
|
import io.metersphere.api.event.APIEventSource;
|
||||||
|
import io.metersphere.sdk.listener.Event;
|
||||||
|
import io.metersphere.sdk.listener.EventListener;
|
||||||
|
import io.metersphere.sdk.util.CommonBeanFactory;
|
||||||
|
import io.metersphere.sdk.util.LogUtils;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class EventSourceTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(0)
|
||||||
|
public void eventSourceTest() throws Exception {
|
||||||
|
// 注册所有监听源
|
||||||
|
LogUtils.info("初始化接口事件源");
|
||||||
|
APIEventSource apiEventSource = CommonBeanFactory.getBean(APIEventSource.class);
|
||||||
|
apiEventSource.addListener(new EventListener<Event>() {
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event event) {
|
||||||
|
LogUtils.info("ExecEventListener: " + event.getModule() + ":" + event.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 触发事件
|
||||||
|
apiEventSource.fireEvent("API", "Event after removing the listener test.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package io.metersphere.plan.listener;
|
||||||
|
|
||||||
|
import io.metersphere.sdk.listener.Event;
|
||||||
|
import io.metersphere.sdk.listener.EventListener;
|
||||||
|
import io.metersphere.sdk.util.LogUtils;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ExecEventListener implements EventListener<Event> {
|
||||||
|
@Override
|
||||||
|
public void onEvent(Event event) {
|
||||||
|
// todo: 测试计划事件处理逻辑
|
||||||
|
LogUtils.info("ExecEventListener: " + event.getModule() + ":" + event.getMessage());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package io.metersphere.plan.controller;
|
||||||
|
|
||||||
|
import io.metersphere.plan.listener.ExecEventListener;
|
||||||
|
import io.metersphere.sdk.listener.Event;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class EventListenerTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(0)
|
||||||
|
public void eventSourceTest() throws Exception {
|
||||||
|
new ExecEventListener().onEvent(new Event("TEST", "Event after removing the listener test."));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue