前言

在现代后端开发中,经常需要在某个函数执行时自动触发其他相关函数,比如日志记录、缓存更新、消息通知等。这种需求在Java Spring和Node.js开发中都很常见。本文将详细介绍在这两个技术栈中实现函数自动执行的多种模式和最佳实践。

一、Java Spring实现方案

(一)AOP切面编程(推荐)

1. 基础AOP实现

第一步:添加依赖

1
2
3
4
5
<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

第二步:启用AOP

1
2
3
4
5
6
7
@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

第三步:创建自定义注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AutoExecute {
/**
* 自动执行的类型
*/
String[] value() default {};

/**
* 是否异步执行
*/
boolean async() default false;

/**
* 执行顺序
*/
int order() default 0;
}

第四步:实现切面类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@Aspect
@Component
@Slf4j
public class AutoExecuteAspect {

@Autowired
private ApplicationEventPublisher eventPublisher;

@Autowired
private TaskExecutor taskExecutor;

/**
* 方法执行前自动执行
*/
@Before("@annotation(autoExecute)")
public void beforeExecute(JoinPoint joinPoint, AutoExecute autoExecute) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();

log.info("方法执行前:{}, 参数:{}", methodName, Arrays.toString(args));

// 执行前置逻辑
executeAutoFunctions("before", methodName, args, autoExecute);
}

/**
* 方法执行后自动执行
*/
@AfterReturning(value = "@annotation(autoExecute)", returning = "result")
public void afterExecute(JoinPoint joinPoint, Object result, AutoExecute autoExecute) {
String methodName = joinPoint.getSignature().getName();

log.info("方法执行后:{}, 结果:{}", methodName, result);

// 执行后置逻辑
executeAutoFunctions("after", methodName, result, autoExecute);
}

/**
* 方法执行异常时自动执行
*/
@AfterThrowing(value = "@annotation(autoExecute)", throwing = "exception")
public void afterThrowing(JoinPoint joinPoint, Exception exception, AutoExecute autoExecute) {
String methodName = joinPoint.getSignature().getName();

log.error("方法执行异常:{}, 异常:{}", methodName, exception.getMessage());

// 执行异常处理逻辑
executeAutoFunctions("error", methodName, exception, autoExecute);
}

/**
* 环绕通知 - 完全控制方法执行
*/
@Around("@annotation(autoExecute)")
public Object aroundExecute(ProceedingJoinPoint joinPoint, AutoExecute autoExecute) throws Throwable {
String methodName = joinPoint.getSignature().getName();
long startTime = System.currentTimeMillis();

try {
// 执行前置逻辑
executeAutoFunctions("before", methodName, joinPoint.getArgs(), autoExecute);

// 执行原方法
Object result = joinPoint.proceed();

// 执行后置逻辑
executeAutoFunctions("after", methodName, result, autoExecute);

return result;
} catch (Exception e) {
// 执行异常逻辑
executeAutoFunctions("error", methodName, e, autoExecute);
throw e;
} finally {
// 执行清理逻辑
long endTime = System.currentTimeMillis();
log.info("方法执行耗时:{}ms", endTime - startTime);
executeAutoFunctions("finally", methodName, endTime - startTime, autoExecute);
}
}

/**
* 执行自动函数
*/
private void executeAutoFunctions(String phase, String methodName, Object data, AutoExecute autoExecute) {
for (String type : autoExecute.value()) {
if (autoExecute.async()) {
// 异步执行
taskExecutor.execute(() -> {
executeByType(phase, type, methodName, data);
});
} else {
// 同步执行
executeByType(phase, type, methodName, data);
}
}
}

/**
* 根据类型执行对应的自动函数
*/
private void executeByType(String phase, String type, String methodName, Object data) {
switch (type) {
case "log":
logFunction(phase, methodName, data);
break;
case "cache":
cacheFunction(phase, methodName, data);
break;
case "notification":
notificationFunction(phase, methodName, data);
break;
case "audit":
auditFunction(phase, methodName, data);
break;
default:
log.warn("未知的自动执行类型:{}", type);
}
}

private void logFunction(String phase, String methodName, Object data) {
log.info("自动日志记录 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data);
}

private void cacheFunction(String phase, String methodName, Object data) {
log.info("自动缓存操作 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data);
// 实际的缓存操作逻辑
}

private void notificationFunction(String phase, String methodName, Object data) {
log.info("自动通知发送 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data);
// 发送通知的逻辑
}

private void auditFunction(String phase, String methodName, Object data) {
log.info("自动审计记录 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data);
// 审计日志记录逻辑
}
}

2. 业务服务使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@Service
@Slf4j
public class OrderService {

@Autowired
private OrderRepository orderRepository;

/**
* 创建订单 - 自动执行日志、缓存、通知
*/
@AutoExecute(value = {"log", "cache", "notification"}, async = true)
public Order createOrder(CreateOrderRequest request) {
log.info("开始创建订单:{}", request);

// 业务逻辑
Order order = new Order();
order.setUserId(request.getUserId());
order.setProductId(request.getProductId());
order.setQuantity(request.getQuantity());
order.setStatus(OrderStatus.PENDING);
order.setCreateTime(LocalDateTime.now());

// 保存订单
Order savedOrder = orderRepository.save(order);

log.info("订单创建成功:{}", savedOrder.getId());
return savedOrder;
}

/**
* 更新订单状态 - 自动执行审计和通知
*/
@AutoExecute(value = {"audit", "notification"})
public Order updateOrderStatus(Long orderId, OrderStatus newStatus) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new OrderNotFoundException("订单不存在:" + orderId));

OrderStatus oldStatus = order.getStatus();
order.setStatus(newStatus);
order.setUpdateTime(LocalDateTime.now());

Order updatedOrder = orderRepository.save(order);

log.info("订单状态更新:{} {} -> {}", orderId, oldStatus, newStatus);
return updatedOrder;
}

/**
* 删除订单 - 自动执行缓存清理和审计
*/
@AutoExecute(value = {"cache", "audit"})
public void deleteOrder(Long orderId) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new OrderNotFoundException("订单不存在:" + orderId));

orderRepository.delete(order);
log.info("订单删除成功:{}", orderId);
}
}

(二)Spring事件机制

1. 自定义事件类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* 基础业务事件
*/
public abstract class BusinessEvent extends ApplicationEvent {
private final String eventType;
private final Object data;
private final LocalDateTime timestamp;

public BusinessEvent(Object source, String eventType, Object data) {
super(source);
this.eventType = eventType;
this.data = data;
this.timestamp = LocalDateTime.now();
}

// getters...
}

/**
* 订单事件
*/
public class OrderEvent extends BusinessEvent {
public OrderEvent(Object source, String eventType, Order order) {
super(source, eventType, order);
}

public Order getOrder() {
return (Order) getData();
}
}

/**
* 用户事件
*/
public class UserEvent extends BusinessEvent {
public UserEvent(Object source, String eventType, User user) {
super(source, eventType, user);
}

public User getUser() {
return (User) getData();
}
}

2. 事件监听器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@Component
@Slf4j
public class BusinessEventListener {

@Autowired
private NotificationService notificationService;

@Autowired
private CacheService cacheService;

@Autowired
private AuditService auditService;

/**
* 监听订单创建事件
*/
@EventListener
@Async
public void handleOrderCreated(OrderEvent event) {
if ("ORDER_CREATED".equals(event.getEventType())) {
Order order = event.getOrder();
log.info("处理订单创建事件:{}", order.getId());

// 自动执行相关操作
notificationService.sendOrderCreatedNotification(order);
cacheService.updateOrderCache(order);
auditService.recordOrderCreation(order);
}
}

/**
* 监听订单状态更新事件
*/
@EventListener
public void handleOrderStatusUpdated(OrderEvent event) {
if ("ORDER_STATUS_UPDATED".equals(event.getEventType())) {
Order order = event.getOrder();
log.info("处理订单状态更新事件:{}", order.getId());

// 根据不同状态执行不同操作
switch (order.getStatus()) {
case PAID:
notificationService.sendPaymentConfirmation(order);
break;
case SHIPPED:
notificationService.sendShippingNotification(order);
break;
case COMPLETED:
notificationService.sendOrderCompletedNotification(order);
auditService.recordOrderCompletion(order);
break;
}
}
}

/**
* 监听用户注册事件
*/
@EventListener
@Async
public void handleUserRegistered(UserEvent event) {
if ("USER_REGISTERED".equals(event.getEventType())) {
User user = event.getUser();
log.info("处理用户注册事件:{}", user.getId());

// 自动执行欢迎流程
notificationService.sendWelcomeEmail(user);
notificationService.sendWelcomeSMS(user);
auditService.recordUserRegistration(user);
}
}

/**
* 通用事件处理器 - 处理所有业务事件
*/
@EventListener
public void handleAllBusinessEvents(BusinessEvent event) {
log.info("记录业务事件:类型={}, 时间={}", event.getEventType(), event.getTimestamp());

// 通用的事件记录逻辑
auditService.recordBusinessEvent(event);
}
}

3. 在业务服务中发布事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Service
@Slf4j
public class OrderService {

@Autowired
private ApplicationEventPublisher eventPublisher;

@Autowired
private OrderRepository orderRepository;

public Order createOrder(CreateOrderRequest request) {
// 创建订单的业务逻辑
Order order = new Order();
order.setUserId(request.getUserId());
order.setProductId(request.getProductId());
order.setQuantity(request.getQuantity());
order.setStatus(OrderStatus.PENDING);
order.setCreateTime(LocalDateTime.now());

Order savedOrder = orderRepository.save(order);

// 发布事件,自动触发相关操作
eventPublisher.publishEvent(new OrderEvent(this, "ORDER_CREATED", savedOrder));

return savedOrder;
}

public Order updateOrderStatus(Long orderId, OrderStatus newStatus) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new OrderNotFoundException("订单不存在:" + orderId));

order.setStatus(newStatus);
order.setUpdateTime(LocalDateTime.now());

Order updatedOrder = orderRepository.save(order);

// 发布状态更新事件
eventPublisher.publishEvent(new OrderEvent(this, "ORDER_STATUS_UPDATED", updatedOrder));

return updatedOrder;
}
}

(三)Spring Boot自动配置

1. 自动执行配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Configuration
@EnableAsync
@EnableScheduling
public class AutoExecuteConfiguration {

/**
* 异步任务执行器
*/
@Bean("autoExecuteTaskExecutor")
public TaskExecutor autoExecuteTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("auto-execute-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}

/**
* 自动执行管理器
*/
@Bean
public AutoExecuteManager autoExecuteManager() {
return new AutoExecuteManager();
}
}

2. 自动执行管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@Component
@Slf4j
public class AutoExecuteManager {

private final Map<String, List<AutoExecuteHandler>> handlers = new ConcurrentHashMap<>();

@Autowired
private TaskExecutor autoExecuteTaskExecutor;

/**
* 注册自动执行处理器
*/
public void registerHandler(String type, AutoExecuteHandler handler) {
handlers.computeIfAbsent(type, k -> new ArrayList<>()).add(handler);
log.info("注册自动执行处理器:类型={}, 处理器={}", type, handler.getClass().getSimpleName());
}

/**
* 执行自动函数
*/
public void execute(String type, Object data, boolean async) {
List<AutoExecuteHandler> typeHandlers = handlers.get(type);
if (typeHandlers == null || typeHandlers.isEmpty()) {
log.warn("未找到类型为 {} 的自动执行处理器", type);
return;
}

for (AutoExecuteHandler handler : typeHandlers) {
if (async) {
autoExecuteTaskExecutor.execute(() -> {
try {
handler.handle(data);
} catch (Exception e) {
log.error("异步执行自动函数失败:{}", e.getMessage(), e);
}
});
} else {
try {
handler.handle(data);
} catch (Exception e) {
log.error("同步执行自动函数失败:{}", e.getMessage(), e);
}
}
}
}

/**
* 自动执行处理器接口
*/
public interface AutoExecuteHandler {
void handle(Object data);
}
}

二、Node.js实现方案

(一)EventEmitter事件驱动模式

1. 基础EventEmitter实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const EventEmitter = require('events');
const util = require('util');

/**
* 业务事件发射器
*/
class BusinessEventEmitter extends EventEmitter {
constructor() {
super();
this.setMaxListeners(50); // 设置最大监听器数量
}

/**
* 执行函数并自动触发事件
*/
async executeWithEvents(eventName, mainFunction, ...args) {
try {
// 触发执行前事件
this.emit(`${eventName}:before`, { args, timestamp: new Date() });

// 执行主函数
const result = await mainFunction(...args);

// 触发执行后事件
this.emit(`${eventName}:after`, { result, args, timestamp: new Date() });

return result;
} catch (error) {
// 触发错误事件
this.emit(`${eventName}:error`, { error, args, timestamp: new Date() });
throw error;
}
}

/**
* 批量注册事件监听器
*/
registerAutoExecuteListeners(eventName, listeners) {
Object.keys(listeners).forEach(phase => {
const phaseListeners = listeners[phase];
if (Array.isArray(phaseListeners)) {
phaseListeners.forEach(listener => {
this.on(`${eventName}:${phase}`, listener);
});
} else if (typeof phaseListeners === 'function') {
this.on(`${eventName}:${phase}`, phaseListeners);
}
});
}
}

// 创建全局事件发射器实例
const businessEvents = new BusinessEventEmitter();

module.exports = { BusinessEventEmitter, businessEvents };

2. 自动执行装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* 自动执行装饰器
*/
function autoExecute(options = {}) {
const {
eventName,
async: isAsync = false,
beforeHandlers = [],
afterHandlers = [],
errorHandlers = []
} = options;

return function(target, propertyKey, descriptor) {
const originalMethod = descriptor.value;

descriptor.value = async function(...args) {
const context = {
target: this,
methodName: propertyKey,
args,
timestamp: new Date()
};

try {
// 执行前置处理器
if (isAsync) {
await Promise.all(beforeHandlers.map(handler => handler(context)));
} else {
beforeHandlers.forEach(handler => handler(context));
}

// 执行原方法
const result = await originalMethod.apply(this, args);

// 更新上下文
context.result = result;

// 执行后置处理器
if (isAsync) {
await Promise.all(afterHandlers.map(handler => handler(context)));
} else {
afterHandlers.forEach(handler => handler(context));
}

return result;
} catch (error) {
context.error = error;

// 执行错误处理器
if (isAsync) {
await Promise.all(errorHandlers.map(handler => handler(context)));
} else {
errorHandlers.forEach(handler => handler(context));
}

throw error;
}
};

return descriptor;
};
}

module.exports = { autoExecute };

3. 业务服务实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const { businessEvents } = require('./BusinessEventEmitter');
const { autoExecute } = require('./AutoExecuteDecorator');

/**
* 订单服务
*/
class OrderService {
constructor() {
this.orders = new Map();
this.setupEventListeners();
}

/**
* 设置事件监听器
*/
setupEventListeners() {
// 订单创建事件监听器
businessEvents.on('order:created', this.handleOrderCreated.bind(this));
businessEvents.on('order:updated', this.handleOrderUpdated.bind(this));
businessEvents.on('order:deleted', this.handleOrderDeleted.bind(this));

// 通用事件监听器
businessEvents.on('order:before', this.logBefore.bind(this));
businessEvents.on('order:after', this.logAfter.bind(this));
businessEvents.on('order:error', this.logError.bind(this));
}

/**
* 创建订单 - 使用装饰器自动执行
*/
@autoExecute({
eventName: 'order',
async: true,
beforeHandlers: [
(context) => console.log('准备创建订单:', context.args),
(context) => this.validateOrderData(context.args[0])
],
afterHandlers: [
(context) => this.sendOrderCreatedNotification(context.result),
(context) => this.updateOrderCache(context.result),
(context) => this.recordOrderAudit(context.result)
],
errorHandlers: [
(context) => console.error('创建订单失败:', context.error.message),
(context) => this.sendErrorNotification(context.error)
]
})
async createOrder(orderData) {
const order = {
id: Date.now().toString(),
...orderData,
status: 'pending',
createdAt: new Date(),
updatedAt: new Date()
};

this.orders.set(order.id, order);

// 发布订单创建事件
businessEvents.emit('order:created', order);

return order;
}

/**
* 更新订单状态
*/
async updateOrderStatus(orderId, newStatus) {
const order = this.orders.get(orderId);
if (!order) {
throw new Error(`订单不存在: ${orderId}`);
}

const oldStatus = order.status;
order.status = newStatus;
order.updatedAt = new Date();

// 使用事件发射器自动执行相关操作
await businessEvents.executeWithEvents('orderStatusUpdate',
async (id, status) => {
this.orders.set(id, order);
return order;
},
orderId, newStatus
);

// 发布订单更新事件
businessEvents.emit('order:updated', { order, oldStatus, newStatus });

return order;
}

/**
* 删除订单
*/
async deleteOrder(orderId) {
const order = this.orders.get(orderId);
if (!order) {
throw new Error(`订单不存在: ${orderId}`);
}

this.orders.delete(orderId);

// 发布订单删除事件
businessEvents.emit('order:deleted', order);

return true;
}

// 事件处理方法
async handleOrderCreated(order) {
console.log('自动处理订单创建:', order.id);
// 自动执行的业务逻辑
}

async handleOrderUpdated({ order, oldStatus, newStatus }) {
console.log(`自动处理订单状态更新: ${order.id} ${oldStatus} -> ${newStatus}`);

// 根据状态变化执行不同操作
switch (newStatus) {
case 'paid':
await this.handleOrderPaid(order);
break;
case 'shipped':
await this.handleOrderShipped(order);
break;
case 'completed':
await this.handleOrderCompleted(order);
break;
}
}

async handleOrderDeleted(order) {
console.log('自动处理订单删除:', order.id);
// 清理相关数据
await this.clearOrderCache(order.id);
await this.recordOrderDeletion(order);
}

// 辅助方法
validateOrderData(orderData) {
if (!orderData.userId || !orderData.productId) {
throw new Error('订单数据不完整');
}
}

async sendOrderCreatedNotification(order) {
console.log('发送订单创建通知:', order.id);
// 实际的通知发送逻辑
}

async updateOrderCache(order) {
console.log('更新订单缓存:', order.id);
// 实际的缓存更新逻辑
}

async recordOrderAudit(order) {
console.log('记录订单审计:', order.id);
// 实际的审计记录逻辑
}

logBefore(context) {
console.log('方法执行前:', context);
}

logAfter(context) {
console.log('方法执行后:', context);
}

logError(context) {
console.error('方法执行错误:', context);
}
}

module.exports = OrderService;

(二)Express中间件模式

1. 自动执行中间件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* 自动执行中间件工厂
*/
function createAutoExecuteMiddleware(options = {}) {
const {
beforeHandlers = [],
afterHandlers = [],
errorHandlers = [],
async: isAsync = false
} = options;

return function autoExecuteMiddleware(req, res, next) {
const originalSend = res.send;
const originalJson = res.json;

// 执行前置处理器
const executeHandlers = async (handlers, context) => {
if (isAsync) {
await Promise.all(handlers.map(handler => handler(context)));
} else {
handlers.forEach(handler => handler(context));
}
};

// 创建上下文
const context = {
req,
res,
startTime: Date.now(),
method: req.method,
url: req.url,
ip: req.ip
};

// 执行前置处理器
executeHandlers(beforeHandlers, context).then(() => {
// 重写响应方法以捕获响应数据
res.send = function(data) {
context.responseData = data;
context.endTime = Date.now();
context.duration = context.endTime - context.startTime;

// 执行后置处理器
executeHandlers(afterHandlers, context).then(() => {
originalSend.call(this, data);
}).catch(error => {
console.error('后置处理器执行失败:', error);
originalSend.call(this, data);
});
};

res.json = function(data) {
context.responseData = data;
context.endTime = Date.now();
context.duration = context.endTime - context.startTime;

// 执行后置处理器
executeHandlers(afterHandlers, context).then(() => {
originalJson.call(this, data);
}).catch(error => {
console.error('后置处理器执行失败:', error);
originalJson.call(this, data);
});
};

next();
}).catch(error => {
context.error = error;

// 执行错误处理器
executeHandlers(errorHandlers, context).then(() => {
next(error);
}).catch(handlerError => {
console.error('错误处理器执行失败:', handlerError);
next(error);
});
});
};
}

module.exports = { createAutoExecuteMiddleware };

2. Express路由中使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express');
const { createAutoExecuteMiddleware } = require('./AutoExecuteMiddleware');

const app = express();
const router = express.Router();

// 自动执行处理器
const logHandler = (context) => {
console.log(`请求日志: ${context.method} ${context.url} - ${context.ip}`);
};

const auditHandler = (context) => {
console.log(`审计记录: ${JSON.stringify({
method: context.method,
url: context.url,
ip: context.ip,
timestamp: new Date(),
duration: context.duration
})}`);
};

const cacheHandler = (context) => {
if (context.method === 'GET') {
console.log(`缓存操作: ${context.url}`);
}
};

const notificationHandler = (context) => {
if (context.responseData && context.responseData.success) {
console.log('发送成功通知');
}
};

// 应用自动执行中间件
router.use(createAutoExecuteMiddleware({
beforeHandlers: [logHandler],
afterHandlers: [auditHandler, cacheHandler, notificationHandler],
errorHandlers: [(context) => console.error('请求处理失败:', context.error)],
async: true
}));

// 订单相关路由
router.post('/orders', async (req, res) => {
try {
const orderData = req.body;

// 创建订单的业务逻辑
const order = {
id: Date.now().toString(),
...orderData,
status: 'pending',
createdAt: new Date()
};

res.json({
success: true,
data: order,
message: '订单创建成功'
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message
});
}
});

router.put('/orders/:id/status', async (req, res) => {
try {
const { id } = req.params;
const { status } = req.body;

// 更新订单状态的业务逻辑
const updatedOrder = {
id,
status,
updatedAt: new Date()
};

res.json({
success: true,
data: updatedOrder,
message: '订单状态更新成功'
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message
});
}
});

app.use('/api', router);

module.exports = app;

(三)Promise链式自动执行

1. Promise链管理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Promise链自动执行管理器
*/
class PromiseChainManager {
constructor() {
this.chains = new Map();
}

/**
* 注册执行链
*/
registerChain(name, chain) {
this.chains.set(name, chain);
}

/**
* 执行指定的链
*/
async executeChain(name, initialData) {
const chain = this.chains.get(name);
if (!chain) {
throw new Error(`未找到名为 ${name} 的执行链`);
}

let result = initialData;

for (const step of chain) {
try {
if (typeof step === 'function') {
result = await step(result);
} else if (step.handler && typeof step.handler === 'function') {
const stepResult = await step.handler(result);

// 如果步骤配置了后续处理器,则执行
if (step.afterHandlers) {
for (const afterHandler of step.afterHandlers) {
await afterHandler(stepResult, result);
}
}

result = stepResult;
}
} catch (error) {
// 如果步骤配置了错误处理器,则执行
if (step.errorHandlers) {
for (const errorHandler of step.errorHandlers) {
await errorHandler(error, result);
}
}
throw error;
}
}

return result;
}

/**
* 创建自动执行的Promise链
*/
createAutoExecuteChain(mainFunction, autoFunctions = []) {
return async (data) => {
// 执行主函数
const result = await mainFunction(data);

// 自动执行其他函数
const autoResults = await Promise.all(
autoFunctions.map(func => func(result, data))
);

return {
mainResult: result,
autoResults
};
};
}
}

// 使用示例
const chainManager = new PromiseChainManager();

// 注册订单处理链
chainManager.registerChain('orderProcessing', [
{
handler: async (orderData) => {
console.log('步骤1: 验证订单数据');
if (!orderData.userId || !orderData.productId) {
throw new Error('订单数据不完整');
}
return orderData;
},
afterHandlers: [
async (result) => console.log('验证完成:', result)
],
errorHandlers: [
async (error) => console.error('验证失败:', error.message)
]
},
{
handler: async (orderData) => {
console.log('步骤2: 创建订单');
const order = {
id: Date.now().toString(),
...orderData,
status: 'pending',
createdAt: new Date()
};
return order;
},
afterHandlers: [
async (order) => console.log('订单创建成功:', order.id),
async (order) => console.log('发送创建通知'),
async (order) => console.log('更新缓存')
]
},
{
handler: async (order) => {
console.log('步骤3: 处理支付');
// 模拟支付处理
await new Promise(resolve => setTimeout(resolve, 1000));
order.status = 'paid';
order.paidAt = new Date();
return order;
},
afterHandlers: [
async (order) => console.log('支付完成:', order.id),
async (order) => console.log('发送支付通知'),
async (order) => console.log('更新订单状态')
]
}
]);

module.exports = { PromiseChainManager, chainManager };

2. 业务服务中使用Promise链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { chainManager } = require('./PromiseChainManager');

class OrderService {
constructor() {
this.setupAutoExecuteChains();
}

/**
* 设置自动执行链
*/
setupAutoExecuteChains() {
// 创建订单自动执行链
const createOrderChain = chainManager.createAutoExecuteChain(
// 主函数:创建订单
async (orderData) => {
const order = {
id: Date.now().toString(),
...orderData,
status: 'pending',
createdAt: new Date()
};
console.log('主函数:订单创建完成', order.id);
return order;
},
// 自动执行的函数数组
[
async (order) => {
console.log('自动执行:发送邮件通知', order.id);
return { type: 'email', sent: true };
},
async (order) => {
console.log('自动执行:更新缓存', order.id);
return { type: 'cache', updated: true };
},
async (order) => {
console.log('自动执行:记录审计日志', order.id);
return { type: 'audit', recorded: true };
},
async (order) => {
console.log('自动执行:发送短信通知', order.id);
return { type: 'sms', sent: true };
}
]
);

chainManager.registerChain('createOrder', [createOrderChain]);
}

/**
* 创建订单
*/
async createOrder(orderData) {
try {
const result = await chainManager.executeChain('createOrder', orderData);
console.log('订单创建流程完成:', result);
return result.mainResult;
} catch (error) {
console.error('订单创建失败:', error.message);
throw error;
}
}

/**
* 处理订单(使用预定义的处理链)
*/
async processOrder(orderData) {
try {
const result = await chainManager.executeChain('orderProcessing', orderData);
console.log('订单处理完成:', result);
return result;
} catch (error) {
console.error('订单处理失败:', error.message);
throw error;
}
}
}

module.exports = OrderService;

三、最佳实践与性能优化

(一)性能优化策略

1. Java Spring优化

异步执行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Configuration
@EnableAsync
public class AsyncConfiguration implements AsyncConfigurer {

@Override
@Bean(name = "autoExecuteTaskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("auto-execute-");
executor.setKeepAliveSeconds(60);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setAwaitTerminationSeconds(30);
executor.initialize();
return executor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}

条件化自动执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
public class ConditionalAutoExecuteAspect {

@Around("@annotation(autoExecute)")
public Object conditionalExecute(ProceedingJoinPoint joinPoint, AutoExecute autoExecute) throws Throwable {
// 根据条件决定是否执行自动函数
if (shouldExecuteAutoFunctions(joinPoint, autoExecute)) {
return executeWithAutoFunctions(joinPoint, autoExecute);
} else {
return joinPoint.proceed();
}
}

private boolean shouldExecuteAutoFunctions(ProceedingJoinPoint joinPoint, AutoExecute autoExecute) {
// 实现条件判断逻辑
// 例如:根据环境、用户权限、系统负载等条件
return !isHighLoadPeriod() && isFeatureEnabled("auto-execute");
}
}

2. Node.js优化

事件监听器优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class OptimizedEventEmitter extends EventEmitter {
constructor() {
super();
this.executionQueue = [];
this.isProcessing = false;
this.maxConcurrency = 10;
}

/**
* 批量处理事件
*/
async processBatch() {
if (this.isProcessing || this.executionQueue.length === 0) {
return;
}

this.isProcessing = true;

try {
const batch = this.executionQueue.splice(0, this.maxConcurrency);
await Promise.all(batch.map(task => task()));
} catch (error) {
console.error('批量处理失败:', error);
} finally {
this.isProcessing = false;

// 如果还有待处理的任务,继续处理
if (this.executionQueue.length > 0) {
setImmediate(() => this.processBatch());
}
}
}

/**
* 添加到执行队列
*/
queueExecution(task) {
this.executionQueue.push(task);

// 如果当前没有在处理,立即开始处理
if (!this.isProcessing) {
setImmediate(() => this.processBatch());
}
}
}

(二)错误处理与监控

1. Java Spring错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Component
@Slf4j
public class AutoExecuteErrorHandler {

@Autowired
private MeterRegistry meterRegistry;

@EventListener
public void handleAutoExecuteError(AutoExecuteErrorEvent event) {
// 记录错误指标
Counter.builder("auto.execute.error")
.tag("method", event.getMethodName())
.tag("type", event.getExecuteType())
.register(meterRegistry)
.increment();

// 记录详细错误日志
log.error("自动执行失败: method={}, type={}, error={}",
event.getMethodName(),
event.getExecuteType(),
event.getError().getMessage(),
event.getError());

// 发送告警
sendAlert(event);
}

private void sendAlert(AutoExecuteErrorEvent event) {
// 实现告警逻辑
}
}

2. Node.js错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class ErrorHandlingEventEmitter extends EventEmitter {
constructor() {
super();
this.errorCount = new Map();
this.maxErrors = 5;
this.errorWindow = 60000; // 1分钟

// 监听未捕获的错误
this.on('error', this.handleError.bind(this));
}

handleError(error, context = {}) {
const errorKey = `${context.method || 'unknown'}:${error.message}`;
const now = Date.now();

// 错误计数
if (!this.errorCount.has(errorKey)) {
this.errorCount.set(errorKey, []);
}

const errors = this.errorCount.get(errorKey);
errors.push(now);

// 清理过期的错误记录
const validErrors = errors.filter(time => now - time < this.errorWindow);
this.errorCount.set(errorKey, validErrors);

// 如果错误频率过高,触发熔断
if (validErrors.length >= this.maxErrors) {
this.emit('circuit-breaker', { errorKey, count: validErrors.length });
}

// 记录错误日志
console.error('自动执行错误:', {
error: error.message,
context,
count: validErrors.length,
timestamp: new Date()
});
}
}

(三)监控与指标

1. Java Spring监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Component
public class AutoExecuteMetrics {

private final Timer executionTimer;
private final Counter successCounter;
private final Counter errorCounter;

public AutoExecuteMetrics(MeterRegistry meterRegistry) {
this.executionTimer = Timer.builder("auto.execute.duration")
.description("自动执行耗时")
.register(meterRegistry);

this.successCounter = Counter.builder("auto.execute.success")
.description("自动执行成功次数")
.register(meterRegistry);

this.errorCounter = Counter.builder("auto.execute.error")
.description("自动执行失败次数")
.register(meterRegistry);
}

public void recordExecution(String type, long duration, boolean success) {
executionTimer.record(duration, TimeUnit.MILLISECONDS);

if (success) {
successCounter.increment(Tags.of("type", type));
} else {
errorCounter.increment(Tags.of("type", type));
}
}
}

2. Node.js监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const promClient = require('prom-client');

// 创建指标
const executionDuration = new promClient.Histogram({
name: 'auto_execute_duration_seconds',
help: '自动执行耗时',
labelNames: ['method', 'type', 'status']
});

const executionCounter = new promClient.Counter({
name: 'auto_execute_total',
help: '自动执行总次数',
labelNames: ['method', 'type', 'status']
});

class MonitoringEventEmitter extends EventEmitter {
constructor() {
super();
this.setupMonitoring();
}

setupMonitoring() {
this.on('execution:start', this.recordStart.bind(this));
this.on('execution:end', this.recordEnd.bind(this));
this.on('execution:error', this.recordError.bind(this));
}

recordStart(context) {
context.startTime = Date.now();
}

recordEnd(context) {
const duration = (Date.now() - context.startTime) / 1000;

executionDuration
.labels(context.method, context.type, 'success')
.observe(duration);

executionCounter
.labels(context.method, context.type, 'success')
.inc();
}

recordError(context) {
const duration = (Date.now() - context.startTime) / 1000;

executionDuration
.labels(context.method, context.type, 'error')
.observe(duration);

executionCounter
.labels(context.method, context.type, 'error')
.inc();
}
}

四、总结

函数自动执行是现代后端开发中的重要模式,通过合理的设计和实现,可以大大提高代码的可维护性和系统的可扩展性。

(一)技术选择建议

Java Spring项目

  1. 简单场景:使用AOP切面编程
  2. 复杂业务:结合Spring事件机制
  3. 高性能要求:使用异步执行和条件化执行
  4. 企业级应用:完整的监控和错误处理机制

Node.js项目

  1. 事件驱动:使用EventEmitter模式
  2. Web应用:结合Express中间件
  3. 复杂流程:使用Promise链管理器
  4. 高并发:优化事件处理和批量执行

(二)最佳实践总结

  1. 性能优先:合理使用异步执行,避免阻塞主流程
  2. 错误隔离:自动执行的失败不应影响主业务逻辑
  3. 监控完善:建立完整的指标监控和告警机制
  4. 可配置化:支持动态开关和条件执行
  5. 文档清晰:明确标注自动执行的副作用和依赖关系

通过这些模式和实践,可以构建出既高效又可维护的自动执行系统,为业务开发提供强有力的技术支撑。

参考资料