简体   繁体   中英

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. 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. Removal request will contain values of object instance variables. Probably it will be unique ID or other one or more instance variables. There will be always one object to remove (or 0 if request is not valid). Object with equal instance variable(s) value will be removed.

My thought is to use two EJBs. First would be @Stateless and exposed as web service. It would extract object from request and call second EJB to add or remove requested object from data structure. Second EJB would be @Singleton and would have instance variable ArrayList<> of objects extracted from requests. As I said it doesn't have to be ArrayList or List at all.

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."

Arjan, your requirement is not fully clear. Assuming you are building a high load application > 1000 request per second I would do:

Servlet-processing

  • separate the "object" datacontainers into the http session of each user (if http session is available)
  • 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
  • add a reference of the httpsession list to the next mentioned SingletonEJB

Singleton EJB

  • create a singleton EJB
  • add a List field for your extracted/filtered/processed Objects of YourObjectType type
  • add a second Collection for the references of the HTTP-Session based and unfiltered but synchronized List

TimerEJB

  • create a timer EJB to run periodically eg every second
  • 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.

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.
  • 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.
  • 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. 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.

You cannot use @Singleton on your WS as the container may initilize more than one instance to deal wiht heavy load of requests.

Your @Singleton bean cannot use ArrayList<> as there will be concurrent access. You should use ConcurrentLinkedQueue instead. How your requests will be identified for the remove operation? Maybe a ConcurrentHashMap would be better for it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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