Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过注解(即生成代理类)形式接入,多线程共享Rules时存在线程安全问题 #411

Open
1ufy opened this issue Apr 19, 2023 · 1 comment

Comments

@1ufy
Copy link

1ufy commented Apr 19, 2023

java.util.ConcurrentModificationException: null
at java.util.TreeMap$PrivateEntryIterator.nextEntry(TreeMap.java:1207)
at java.util.TreeMap$KeyIterator.next(TreeMap.java:1261)
at org.jeasy.rules.core.RuleProxy.appendActionMethodsNames(RuleProxy.java:353)
at org.jeasy.rules.core.RuleProxy.getRuleDescription(RuleProxy.java:334)
at org.jeasy.rules.core.RuleProxy.invoke(RuleProxy.java:101)
at com.sun.proxy.$Proxy160.getDescription(Unknown Source)
at org.jeasy.rules.core.DefaultRulesEngine.log(DefaultRulesEngine.java:146)
at org.jeasy.rules.core.DefaultRulesEngine.doFire(DefaultRulesEngine.java:80)
at org.jeasy.rules.core.DefaultRulesEngine.fire(DefaultRulesEngine.java:70)

private Set getActionMethodBeans() {
if (this.actionMethods == null) {
this.actionMethods = new TreeSet<>();
Method[] methods = getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Action.class)) {
Action actionAnnotation = method.getAnnotation(Action.class);
int order = actionAnnotation.order();
this.actionMethods.add(new ActionMethodOrderBean(method, order));
}
}
}
return this.actionMethods;
}

内部actionMethods对象是TreeSet非线程安全,多线程并发时,遍历时会抛出ConcurrentModificationException

解决思路:

  1. 不共享Rules对象,缺点会生成重复代理对象
  2. 采用继承非注解的形式接入
@zpshowhan
Copy link

RuleProxy创建的时候优先把rule的元数据获取到并赋值就行了
RuleProxy#RuleProxy()
private RuleProxy(final Object target) { this.target = target; // load meta data this.init(); } /** * init meta data */ private void init() { try { getRuleName(); getRuleDescription(); getRulePriority(); }catch (Throwable e){ LOGGER.error("init meta data error", e); } }
然后自己打一个hotfix包

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants