Design pattern for single request webservice
I plan to create a webservice (in Tomcat) that works with a certain file on the system and make some system calls. The challenge am having is to make sure only 1 request can be processed at a given time and the previous request开发者_运维技巧 needs to be pre-empted. I currently have a singleton class that works on the request. And requesting threads somehow wait until the running thread is done. But what is the general design pattern for such problems?
Any ideas how this can be achieved?
Thanks, V
Since there may be too many requests calling to this service, the synchronous approach may not be achieved. There also may be a chance that some clients waiting so that it is time-out. Then I prefer the asynchronous as
- The Service-A receives the request and put it to queue-A or DB table-A, together with generating the ref-id.
- The Service-A returns the ref-id to the client for further monitoring.
- There is a back-end process monitoring the queue-A or DB table-A and perform the request one-by-one.
- After finish, put the result to another queue-B or DB table-B.
- The client keep monitoring periodically via another Service-B by using the ref-id, if the request is done.
I hope this may help to achieve your requirement.
Regard,
Charlee Ch.
I would place the queues mentioned by Charlee in the ServletContext. It is initialized when your WebApplication is started.
You can initialize and destroy these queues an the backend process in an implementation of ServletContextListener
. Store them with setAttribute
on the ServletContext.
Now you're able to access these queues via ServletContext in your Servlet.
精彩评论