|
@@ -1,4 +1,4 @@
|
|
|
-package com.chelvc.framework.group.context;
|
|
|
+package com.chelvc.framework.group;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
@@ -11,14 +11,14 @@ import java.util.stream.Stream;
|
|
|
import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
import com.chelvc.framework.base.context.ThreadContextHolder;
|
|
|
+import com.chelvc.framework.common.model.BiPair;
|
|
|
import com.chelvc.framework.common.model.Caps;
|
|
|
import com.chelvc.framework.common.model.Invoking;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
|
-import com.chelvc.framework.group.GroupHandler;
|
|
|
-import com.chelvc.framework.group.GroupStore;
|
|
|
import com.chelvc.framework.group.annotation.Group;
|
|
|
import com.chelvc.framework.group.annotation.Groups;
|
|
|
+import com.chelvc.framework.group.context.GroupContextHolder;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
@@ -31,7 +31,7 @@ import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
|
- * 场景分组方法拦截器
|
|
|
+ * 场景分组拦截器
|
|
|
*
|
|
|
* @author Woody
|
|
|
* @date 2024/11/24
|
|
@@ -40,7 +40,7 @@ import org.springframework.stereotype.Component;
|
|
|
@Aspect
|
|
|
@Component
|
|
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
-public class GroupMethodInterceptor {
|
|
|
+public class GroupingInterceptor {
|
|
|
private final GroupStore store;
|
|
|
private final ApplicationContext applicationContext;
|
|
|
private final Map<Class<?>, GroupHandler> handlers = Maps.newConcurrentMap();
|
|
@@ -80,28 +80,46 @@ public class GroupMethodInterceptor {
|
|
|
* @param args 分组执行参数
|
|
|
*/
|
|
|
private void execute(List<Group> groups, Object... args) {
|
|
|
- Map<String, Caps> stores = null;
|
|
|
+ // 执行场景分组
|
|
|
+ List<BiPair<Group, GroupHandler, Caps>> values = Lists.newLinkedList();
|
|
|
for (Group group : groups) {
|
|
|
try {
|
|
|
GroupHandler handler = this.initializeGroupHandler(group.handler());
|
|
|
Caps value = handler.execute(group.scene(), args);
|
|
|
- if (value != null && group.storing()) {
|
|
|
- if (stores == null) {
|
|
|
- stores = Maps.newHashMapWithExpectedSize(groups.size());
|
|
|
- }
|
|
|
- stores.put(group.scene(), value);
|
|
|
- } else {
|
|
|
+ if (!group.storing()) {
|
|
|
GroupContextHolder.setGroup(group.scene(), ObjectUtils.ifNull(value, Caps.NONE));
|
|
|
}
|
|
|
+ if (value != null) {
|
|
|
+ values.add(BiPair.of(group, handler, value));
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- log.error("Group handle failed: {}", group.scene(), e);
|
|
|
+ log.error("Group execute failed: {}", group.scene(), e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 保存场景分组,以最终结果为准
|
|
|
+ Map<String, Caps> stores = values.stream().filter(pair -> pair.getLeft().storing())
|
|
|
+ .collect(Collectors.toMap(pair -> pair.getLeft().scene(), BiPair::getRight));
|
|
|
try {
|
|
|
stores = ObjectUtils.ifEmpty(stores, this.store::set, Collections::emptyMap);
|
|
|
stores.forEach((scene, group) -> GroupContextHolder.setGroup(scene, ObjectUtils.ifNull(group, Caps.NONE)));
|
|
|
} catch (Exception e) {
|
|
|
- log.error("Group handle failed", e);
|
|
|
+ log.error("Group storing failed", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行分组完成回调方法
|
|
|
+ for (BiPair<Group, GroupHandler, Caps> pair : values) {
|
|
|
+ Group group = pair.getLeft();
|
|
|
+ Caps value = pair.getRight();
|
|
|
+ GroupHandler handler = pair.getMiddle();
|
|
|
+ if (!group.storing() || value == stores.get(group.scene())) {
|
|
|
+ try {
|
|
|
+ handler.complete(group.scene(), value, args);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Group complete failed: {}", group.scene(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -170,7 +188,7 @@ public class GroupMethodInterceptor {
|
|
|
}
|
|
|
});
|
|
|
} catch (Exception e) {
|
|
|
- log.error("Group handle failed", e);
|
|
|
+ log.error("Group loading failed", e);
|
|
|
return point.proceed();
|
|
|
}
|
|
|
|