开发者

Wicket: how to synchronize requests within session

开发者 https://www.devze.com 2022-12-13 07:53 出处:网络
Scenario: Apache Wicket based web application running on Tomcat server. User opens URL in browser, session开发者_如何学运维 is created and simple main page is shown in browser, user clicks on button

Scenario: Apache Wicket based web application running on Tomcat server.

User opens URL in browser, session开发者_如何学运维 is created and simple main page is shown in browser, user clicks on button and AJAX call is invoked. Application gets request and doing some stuff preparing response. In same time user or JavaScript in browser invokes another AJAX call -- this second requests is processed in another thread in application, and because most parts of application are session specific and aren't thread-safe (because one user = one session), exception throws.

Possible solutions:

  1. make all application classes thread-safe (very costly)

  2. adapt GUI so no simultaneously runs two AJAX calls in one session (not possible due nature of browser GUI)

  3. synchronize all requests in one session on Wicket or Tomcat level (but how?)

  4. another practice or technique ???

Thanks


Requests to pages or components within the same PageMap in a single Session are already synchronous - only one thread at a time. Requests for resources like images, javascript, css files etc. are handled asynchronously. (Different clients never block each other as each client has its own Session and PageMap).

However, access to items in the Session itself, I believe, are not explicitly synchronized.

Incidentally, accessing the Session/Pages from a thread which is not a Request Thread is not a good idea, as the container is free to do anything with your Session/Page between requests - e.g. write it out to disk etc.


What is the exception that is thrown? If an exception is thrown then I would assume that there is a critical section of the Session object that needs to be synchronized or handled with more care and maybe not the entire session.

I have not had reason to utilize them much, but I know there are channels as part of the wicket-ajax.js (found in org.apache.wicket.ajax package). These control how multiple AJAX calls are handled. They might be worth a look. In this file is the following comment:

/**
 * Channel management
 *
 * Wicket Ajax requests are organized in channels. A channel maintain the order of 
 * requests and determines, what should happen when a request is fired while another 
 * one is being processed. The default behavior (stack) puts the all subsequent requests 
 * in a queue, while the drop behavior limits queue size to one, so only the most
 * recent of subsequent requests is executed.
 * The name of channel determines the policy. E.g. channel with name foochannel|s is 
 * a stack channel, while barchannel|d is a drop channel.
 *
 * The Channel class is supposed to be used through the ChannelManager.
 */
0

精彩评论

暂无评论...
验证码 换一张
取 消