Want to create a synchronized data download in servlet action

I want to create a servlet method like below. In this method I want to perform some data download.So if request for data download comes I just do the download. If already a download is going on I want somehow the second request to wait till the first thread is done with download. Once the first thread is done with download the second thread can start automatically.

DoTheDownloadAction(){

}

How can i achieve the above requirement?

Answers


Considering you have a DownloadHelper class and in your Servlet you have created one instance of that class then you can do something like this :

DoTheDownloadAction() {
   synchronized(downloadHelper) {
      //Downloading something
   }
}

Lets imagine you have a button called as "download" with id="download" in your jsp and you have this code in your javascript

var globalDownloadStatus = false;
jQuery(document).ready(function(){
   jQuery('#download'(.click(function(){
       if(globalDownloadStatus == true) {
           alert('download already in progress, please wait');
           return;
       }
       jQuery.get('yourservletpath', function(data){
          alert('Download Complete');
       });
   });
});

Sounds like the perfect candidate for a semaphore, or (depending on the complexity and the downstream effects) the simpler way to affect the same change would be to synchronize the download code to a relevant key for your application.


Take in account that usually web-servers could distributed for scalability. Usually the appropriate solution is to synchronize via database locks. However for you maybe just enough to use synchronized java keyword on an object you want wait to.

Also, you are asking for a pessimistic lock. It's usually bad architecture design.


Need Your Help

Changing static double in other classes

java inheritance static

So I am making a ticket booth that sales three tickets using inheritance and polymorphism. the output needs to look like this.

Jquery mouseover submenu slide-out HEIGHT

javascript jquery css jquery-hover

I'm a newbie; need some help! I created a left-aligned navigation menu with a slide-out submenu. I'm happy with everything except for the sub-menu's height. How can I make the entire slide-out menu...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.