feat: use package aop

This commit is contained in:
tjq 2022-09-08 23:59:17 +08:00
parent a0cc5670d4
commit e23825c399
5 changed files with 38 additions and 42 deletions

View File

@ -15,7 +15,7 @@ import tech.powerjob.server.monitor.Event;
@Accessors(chain = true)
public class DatabaseEvent implements Event {
private DatabaseEventType type;
private DatabaseType type;
private String serviceName;

View File

@ -1,13 +0,0 @@
package tech.powerjob.server.monitor.events.db;
/**
* DatabaseEventType
*
* @author tjq
* @since 2022/9/6
*/
public enum DatabaseEventType {
H2,
CORE,
MONGO
}

View File

@ -1,20 +0,0 @@
package tech.powerjob.server.monitor.events.db;
import java.lang.annotation.*;
/**
* 数据库监控注解
*
* @author tjq
* @since 2022/9/6
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DatabaseMonitor {
/**
* 类型比如 H2 / CORE / MONGO
* @return 类型
*/
DatabaseEventType type();
}

View File

@ -23,13 +23,21 @@ public class DatabaseMonitorAspect {
@Resource
private ServerMonitor serverMonitor;
@Around(value = "@annotation(databaseMonitor))")
public Object execute(ProceedingJoinPoint point, DatabaseMonitor databaseMonitor) throws Throwable {
@Around("execution(* tech.powerjob.server.persistence.remote.repository..*.*(..))")
public Object monitorCoreDB(ProceedingJoinPoint joinPoint) throws Throwable {
return wrapperMonitor(joinPoint, DatabaseType.CORE);
}
@Around("execution(* tech.powerjob.server.persistence.local..*.*(..))")
public Object monitorLocalDB(ProceedingJoinPoint joinPoint) throws Throwable {
return wrapperMonitor(joinPoint, DatabaseType.LOCAL);
}
private Object wrapperMonitor(ProceedingJoinPoint point, DatabaseType type) throws Throwable {
final String className = point.getTarget().getClass().getSimpleName();
final String methodName = point.getSignature().getName();
DatabaseEvent event = new DatabaseEvent().setType(databaseMonitor.type())
DatabaseEvent event = new DatabaseEvent().setType(type)
.setServiceName(className)
.setMethodName(methodName)
.setStatus(DatabaseEvent.Status.SUCCESS);
@ -40,12 +48,11 @@ public class DatabaseMonitorAspect {
event.setRows(parseEffectRows(ret));
return ret;
} catch (Throwable t) {
long cost = System.currentTimeMillis() - startTs;
event.setCost(cost).setErrorMsg(t.getMessage()).setStatus(DatabaseEvent.Status.FAILED);
serverMonitor.record(event);
event.setErrorMsg(t.getMessage()).setStatus(DatabaseEvent.Status.FAILED);
throw t;
} finally {
long cost = System.currentTimeMillis() - startTs;
serverMonitor.record(event.setCost(cost));
}
}

View File

@ -0,0 +1,22 @@
package tech.powerjob.server.monitor.events.db;
/**
* DatabaseEventType
*
* @author tjq
* @since 2022/9/6
*/
public enum DatabaseType {
/**
* 本地存储库H2
*/
LOCAL,
/**
* 远程核心库
*/
CORE,
/**
* 扩展库
*/
EXTRA
}