java - 多线程将如何影响 Easy Rules 引擎?

标签 java multithreading rule-engine

我正在为我的 Web 应用程序寻找规则引擎,我发现了 Easy Rules。然而,在常见问题解答部分,它指出了线程安全的限制。

Web 容器是否被视为多线程环境?对于 HTTP 请求,可能是由应用程序服务器创建的工作线程处理的。

线程安全是如何实现的?

How to deal with thread safety?

If you run Easy Rules in a multi-threaded environment, you should take into account the following considerations:

Easy Rules engine holds a set of rules, it is not thread safe.
By design, rules in Easy Rules encapsulate the business object model they operate on, so they are not thread safe neither.
Do not try to make everything synchronized or locked down! 

Easy Rules 引擎是一个非常轻量级的对象,您可以为每个线程创建一个实例,这是迄今为止避免线程安全问题的最简单方法

根据这个例子,多线程将如何影响规则引擎?

public class AgeRule extends BasicRule {
    private static final int ADULT_AGE = 18;

    private Person person;

    public AgeRule(Person person) {
        super("AgeRule",
              "Check if person's age is > 18 and
               marks the person as adult", 1);
        this.person = person;
    }

    @Override
    public boolean evaluate() {
        return person.getAge() > ADULT_AGE;
    }

    @Override
    public void execute() {
        person.setAdult(true);
        System.out.printf("Person %s has been marked as adult",
                            person.getName());
    }

}

public class AlcoholRule extends BasicRule {

    private Person person;

    public AlcoholRule(Person person) {
        super("AlcoholRule", 
              "Children are not allowed to buy alcohol",
               2);
        this.person = person;
    }

    @Condition
    public boolean evaluate() {
        return !person.isAdult();
    }

    @Action
    public void execute(){
        System.out.printf("Shop: Sorry %s,
                you are not allowed to buy alcohol",
                 person.getName());
    }

}

public class Launcher {

    public void someMethod() {
        //create a person instance
        Person tom = new Person("Tom", 14);
        System.out.println("Tom:
                Hi! can I have some Vodka please?");

        //create a rules engine
        RulesEngine rulesEngine = aNewRulesEngine()
                .named("shop rules engine")
                .build();

        //register rules
        rulesEngine.registerRule(new AgeRule(tom));
        rulesEngine.registerRule(new AlcoholRule(tom));

        //fire rules
        rulesEngine.fireRules();
    }

}

最佳答案

是的,Web 应用程序是多线程的。正如您所期望的,有一个由服务器维护的线程池。当serversocket在它正在监听的端口上收到传入请求时,它会将请求委托(delegate)给池中的一个线程。通常,请求会在该线程上执行,直到响应完成。

如果您尝试创建单个规则引擎并让多个线程访问它,那么要么

  1. 规则引擎数据由于被多个线程操作而被损坏(因为并非线程安全的数据结构可以分多个步骤执行操作,这些操作在其他线程访问和访问时可能会受到干扰)更改相同的数据),或

  2. 您使用锁定来确保一次只有一个线程可以使用规则引擎,从而避免共享对象被损坏,但在此过程中会产生瓶颈。您的所有请求都需要等待规则引擎可用,并且一次只有一个线程可以取得进展。

最好为每个请求提供自己的规则引擎副本,这样它就不会被损坏,也不需要锁定。线程的理想情况是每个线程都能够独立执行,而不需要争用共享资源。

关于java - 多线程将如何影响 Easy Rules 引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39360276/

相关文章:

java - 将 web 添加到 java 应用程序的推荐技术

java - weblogic 集群环境中的线程被阻塞

java - 我的自定义事件应该在 GUI EDT 还是 "home-grown"EDT 上触发

java - 使用多线程更新GUI

simulation - 如何为基于代理的模型实现基于规则的决策者?

streaming - 为我们的流事件构建实时规则引擎的最佳方法

drools - 以原子方式更新事实的专家/规则引擎?

java - Hibernate网站显示版本3.5,maven只显示3.1rc2

java - android admob 在横向上调整大小

java - 如何解决 javax.mail.AuthenticationFailedException?