Solution to the long running query problem in a web application (asynchronous request)

后端 未结 4 1423
忘了有多久
忘了有多久 2020-12-28 22:51

Here is the problem

A user of an enterprise web application is performing a task that results in a long (very long) database query (or other long processing intensiv

4条回答
  •  渐次进展
    2020-12-28 23:45

    I would do a combination of your so called "bad solution" and the "j2ee solution":

    1. The original UI thread sends a asynchronous JMS message to the "backend" and returns.
    2. The backend receives the asynchronous request and processes it.
    3. When a result (or error) is reached in the backend, it is returned to the controller layer.
    4. The UI, still doing AJAX polling or using Bayeux/Cometed, receives and displays the result.

    The trick is to match the request / response pair. I would do it like this:

    1. Create a "AsyncManagerService" (AMS) that has SESSION, maybe even APPLICATION wide scope so all threads talk to the same instance.
    2. The AMS holds a map with an id as key and any object as value.
    3. When creating the request JMS message, generate a unique, random key and put it into the jmsCorrelationId of the message as well as into the map, with NULL as a value. Also pass that id to the UI.
    4. Let the UI thread poll the AMS with the previously generated id as long as the value in the map is NULL.
    5. When the result is ready, let your JMS receiver put it into the AMS' map at the given id.
    6. Next time the UI thread polls the map, it will receive the answer and stop polling.

    This is clean and well abstracted from any concrete domain. A purely technical solution.

    Even if you don't like polling, HTTP is stateless by design and I think this way polling only happens at well defined intervals.

    Anyway, I implemented a system with exactly this pattern and it runs just fine...

提交回复
热议问题