mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: use package aop
This commit is contained in:
parent
a0cc5670d4
commit
e23825c399
@ -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;
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
package tech.powerjob.server.monitor.events.db;
|
||||
|
||||
/**
|
||||
* DatabaseEventType
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2022/9/6
|
||||
*/
|
||||
public enum DatabaseEventType {
|
||||
H2,
|
||||
CORE,
|
||||
MONGO
|
||||
}
|
@ -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();
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user