简体   繁体   English

设计Web服务的最佳方法,该服务必须将请求中的数据存储在列表或类似结构中

[英]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的处理

  • separate the "object" datacontainers into the http session of each user (if http session is available) 将“对象”数据容器分离到每个用户的http会话中(如果http会话可用)
  • add a synchronized List into you http session and on every request do the operations on a synchronized private List and add the web client details to this http session stored list 将同步列表添加到您的http会话中,并根据每个请求对同步的私有列表执行操作,并将Web客户端详细信息添加到此http会话存储列表中
  • add a reference of the httpsession list to the next mentioned SingletonEJB 将httpsession列表的引用添加到下一个提到的SingletonEJB

Singleton EJB 单例EJB

  • create a singleton EJB 创建一个单例EJB
  • add a List field for your extracted/filtered/processed Objects of YourObjectType type 为您的YourObjectType类型的提取/过滤/处理对象添加一个List字段
  • add a second Collection for the references of the HTTP-Session based and unfiltered but synchronized List 为基于HTTP会话且未过滤但已同步的List的引用添加第二个Collection

TimerEJB TimerEJB

  • create a timer EJB to run periodically eg every second 创建一个计时器EJB以定期运行,例如每秒运行一次
  • this function will go through all referenced Lists in the refCol and will extract and filter the needed objects and also ensure to make a cleanup of the http session based lists. 此功能将遍历refCol中的所有引用列表,并将提取和过滤所需的对象,并确保清除基于http会话的列表。

Benefit of the solution: 解决方案的好处:

  • push the mem load of the users to their sessions 将用户的内存负载推向他们的会话
  • do not get into synchronizing/mutex issues on very high load, since you operate only very short with your timerEJB on a central and filtered List in your Singleton bean. 不要在非常高的负载下陷入同步/互斥问题,因为在Singleton bean的中央和过滤列表上,使用timerEJB的操作非常短。
  • you are as long scaleable as long your sessions keep small 只要会话规模较小,您就可以扩展
  • the maybe CPU intensive solution to filter and the IO intensive solution to persist the filtered objects are outside the client requests and the clients will not face an performance issue. 可能需要占用大量CPU资源的解决方案以及用于保留已过滤对象的IO密集型解决方案不在客户端请求范围内,客户端不会遇到性能问题。
  • the timer schedules are changeable by your needs 您可以根据需要更改计时器时间表

Drawback: 退税:

  • a little higher complexity 复杂度高一点
  • you have to clean your references in your refColl to the Lists of the closed http sessions. 您必须在refColl中清除对封闭的http会话列表的引用。 Either by providing also the Session and the Reference to the List of the session in a composite class or by any other custom solution. 通过在复合类中提供会话和对会话列表的引用,或者通过任何其他自定义解决方案。 If you do not cleanup, your heap will get bloated by holding references to Lists of already "killed" http sessions. 如果不清理,则通过保留对已“终止”的HTTP会话列表的引用,会使堆膨胀。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM