[英]Best way to design web service, which have to store data from requests in list or similar structure
My goal is to create Java EE web service, which will take concurrently thousands (or more in future) requests and must store data from requests in list or similar structure. 我的目标是创建Java EE Web服务,该服务将同时处理数千个(或将来更多)请求,并且必须将请求中的数据存储在列表或类似结构中。 I want to store objects from requests in memory, but this is not obligatory.
我想将请求中的对象存储在内存中,但这不是必须的。 Some object will be extracted from every request and added to chosen data structure.
将从每个请求中提取一些对象,并将其添加到所选的数据结构中。 Web service will have two operations: add to structure and remove from structure.
Web服务将具有两个操作:添加到结构和从结构中删除。 Removal request will contain values of object instance variables.
删除请求将包含对象实例变量的值。 Probably it will be unique ID or other one or more instance variables.
可能是唯一ID或其他一个或多个实例变量。 There will be always one object to remove (or 0 if request is not valid).
始终有一个要删除的对象(如果请求无效,则为0)。 Object with equal instance variable(s) value will be removed.
具有相同实例变量值的对象将被删除。
My thought is to use two EJBs. 我的想法是使用两个EJB。 First would be @Stateless and exposed as web service.
首先是@Stateless并公开为Web服务。 It would extract object from request and call second EJB to add or remove requested object from data structure.
它将从请求中提取对象,并调用第二个EJB从数据结构中添加或删除请求的对象。 Second EJB would be @Singleton and would have instance variable ArrayList<> of objects extracted from requests.
第二个EJB将是@Singleton,并将具有从请求中提取的对象的实例变量ArrayList <>。 As I said it doesn't have to be ArrayList or List at all.
正如我所说的,它根本不必是ArrayList或List。
I have also thought about using one EJB, which would have to be @WebService @Singleton, but documentation says that this combination is "possible, but ... not defined by this specification." 我还考虑过使用一个EJB,它必须是@WebService @Singleton,但是文档说这种组合是“可能的,但是……未由本规范定义”。
Arjan, your requirement is not fully clear. Arjan,您的要求尚不完全清楚。 Assuming you are building a high load application > 1000 request per second I would do:
假设您正在构建每秒处理1000个请求的高负载应用程序,我将这样做:
Servlet-processing Servlet的处理
Singleton EJB 单例EJB
TimerEJB TimerEJB
Benefit of the solution: 解决方案的好处:
Drawback: 退税:
You cannot use @Singleton on your WS as the container may initilize more than one instance to deal wiht heavy load of requests. 您不能在WS上使用@Singleton,因为容器可能会初始化多个实例来处理大量请求。
Your @Singleton bean cannot use ArrayList<> as there will be concurrent access. 您的@Singleton bean无法使用ArrayList <>,因为将存在并发访问。 You should use ConcurrentLinkedQueue instead.
您应该改用ConcurrentLinkedQueue。 How your requests will be identified for the remove operation?
如何识别您的删除操作请求? Maybe a ConcurrentHashMap would be better for it.
也许ConcurrentHashMap会更好。
Sooner or later you will run out of memory if you want to have "thousands (or more in future)" unless you dump the requests into DB or file. 除非您将请求转储到数据库或文件中,否则迟早如果要“成千上万(或将来会有更多)”,将耗尽内存。 DB with JPA will be the easiest, you can achieve it with few annotations on your WS and Request object.
使用JPA的数据库将是最简单的,您只需在WS和Request对象上添加少量注释即可实现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.