我正在为我的 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)给池中的一个线程。通常,请求会在该线程上执行,直到响应完成。
如果您尝试创建单个规则引擎并让多个线程访问它,那么要么
规则引擎数据由于被多个线程操作而被损坏(因为并非线程安全的数据结构可以分多个步骤执行操作,这些操作在其他线程访问和访问时可能会受到干扰)更改相同的数据),或
您使用锁定来确保一次只有一个线程可以使用规则引擎,从而避免共享对象被损坏,但在此过程中会产生瓶颈。您的所有请求都需要等待规则引擎可用,并且一次只有一个线程可以取得进展。
最好为每个请求提供自己的规则引擎副本,这样它就不会被损坏,也不需要锁定。线程的理想情况是每个线程都能够独立执行,而不需要争用共享资源。
关于java - 多线程将如何影响 Easy Rules 引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39360276/
我已经创建了移动应用程序,并希望使用 Easy Table 和 Easy API。但是,每当我访问该位置时,它都会显示“不受支持的服务”。 下面是我的 Azure 帐户控制台的图像链接。 提前致谢。
选择图像后,我在两个地方渲染它,一个在 react-easy-crop(4:3 宽高比)中,另一个在单独的 div(960w*510h) 中。所以每当我改变我的裁剪位置时在 react-easy-cr
*来源:*blog.csdn.net/zhan107876/article/ details/120815560 总览 使用flowable自带的flowable-ui制作流程图 使用springbo
**摘要:**使用Docker容器,将带UI的程序,直接转换为Web应用。很方便,跟大家分享一下。 本文分享自华为云社区《使用Docker容器,将带UI的程序,直接转为Web应用》,作者:tsjsdb
我正在尝试使用 meteor-easy-search 实现两个数据库字段的搜索 包,但是,我似乎找不到这样做的方法。这是问题所在: 我有一个架构: { name: String, locati
我目前正在努力让下面的 calcRatio 函数正确计算。这可能是基础数学!以下函数按预期工作: function calcRatio(){ var r = frontRing.value/b
我正在尝试这样做: SELECT Filmai.Pagrind_atlik, ( COUNT(Filmai.Pagrind_atlik)) AS Kiek_kartu_filmavosi FROM F
1.1 框架功能 1.1.1 数据加载 1.1.2 扩展网络 1.1.3 优化器 1.1.4 学习率衰减
1.背景 2.验证码识别 2.1 生成训练数据 2.2 编写训练脚本 2.3 训练结果展示 2.4 预测应用
先来一份完整的爬虫工程师面试考点: 1、 python 基本功 1、简述python 的特点和优点 python 是一门开源的解释性语言,相比 java c++ 等语言,python 具
SpringMVC 源码分析系列最后一篇,和大家聊一聊 Theme。 Theme,就是主题,点一下就给网站更换一个主题,相信大家都用过类似功能,这个其实和前面所说的国际化功能很像,代码其实也很
在傻瓜式部署方式出现问题之后,我们该通过什么方法搭建网站呢?其实京东云擎本身已经给出了答案,那就是git。关于git是什么我们就不做过多介绍了,它与以前的flashfxp这类ftp工具的作用很相似,
https://azure.microsoft.com/en-us/documentation/articles/mobile-services-using-soft-delete/ 所以我正在阅读这
我正在尝试使用 table.Where(item => item.Name == Entry.Text) 检查 Azure Easy Tables 中是否存在行然后我检查保存它的列表是否为空。但表仍然
我正在使用以下代码来获取 azure 移动服务中的用户详细信息。我正在尝试迁移到 azure 移动应用程序,并在 easy api 中获取用户详细信息。如何做到这一点? getIdentity({
是否有某种方法可以从 Azure Easy Tables 中仅获取一行的一个数据列? 例如,Xamarin.Forms 应用程序会将项目名称发送到 Azure,并仅获取项目创建日期时间。 最佳答案 下
我正在尝试将一些数据导出到 PDF 文件中。我正在使用: Django 1.9.12 django-easy-pdf 0.1.0 python 2.7 导出工作正常(我的观点没有问题)但我正在努力将页
我正在使用以下代码来获取 azure 移动服务中的用户详细信息。我正在尝试迁移到 azure 移动应用程序,并在 easy api 中获取用户详细信息。如何做到这一点? getIdentity({
是否有某种方法可以从 Azure Easy Tables 中仅获取一行的一个数据列? 例如,Xamarin.Forms 应用程序会将项目名称发送到 Azure,并仅获取项目创建日期时间。 最佳答案 下
我正在尝试将一些数据导出到 PDF 文件中。我正在使用: Django 1.9.12 django-easy-pdf 0.1.0 python 2.7 导出工作正常(我的观点没有问题)但我正在努力将页
我是一名优秀的程序员,十分优秀!