changeset 6450:eb4d0950ae87

GWT client: prevent too many create artifact calls at the same time by limiting them to 5.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 26 Jun 2013 21:35:53 +0200
parents 00aa1bc72a65
children 1c7e3426207a
files gwt-client/src/main/java/org/dive4elements/river/client/server/ArtifactHelper.java
diffstat 1 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/ArtifactHelper.java	Wed Jun 26 20:50:58 2013 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/ArtifactHelper.java	Wed Jun 26 21:35:53 2013 +0200
@@ -16,6 +16,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.Semaphore;
 
 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils;
 import org.dive4elements.artifacts.common.utils.CreationFilter;
@@ -52,6 +53,12 @@
 
     private static final String SQ_RELATION_ARTIFACT = "staticsqrelation";
 
+    // To prevent pile up of create artifact calls only permit a limited
+    // number of parallel creates.
+    public static final int MAX_CREATE = 5;
+
+    private static final Semaphore CREATE_SEMAPHORE = new Semaphore(MAX_CREATE);
+
     private ArtifactHelper() {
     }
 
@@ -90,7 +97,6 @@
             factory, uuid, ids, filter, targetOut);
 
         return sendCreate(serverUrl, locale, create);
-
     }
 
     /**
@@ -170,16 +176,27 @@
             Document doc)
     throws ServerException
     {
-        HttpClient client = new HttpClientImpl(serverUrl, locale);
-
         try {
-            return (Artifact) client.create(doc, new FLYSArtifactCreator());
+            CREATE_SEMAPHORE.acquire();
         }
-        catch (ConnectionException ce) {
-            logger.error(ce, ce);
+        catch (InterruptedException ie) {
+            throw new ServerException(ERROR_CREATE_ARTIFACT);
         }
+        try {
+            HttpClient client = new HttpClientImpl(serverUrl, locale);
 
-        throw new ServerException(ERROR_CREATE_ARTIFACT);
+            try {
+                return (Artifact) client.create(doc, new FLYSArtifactCreator());
+            }
+            catch (ConnectionException ce) {
+                logger.error(ce, ce);
+            }
+
+            throw new ServerException(ERROR_CREATE_ARTIFACT);
+        }
+        finally {
+            CREATE_SEMAPHORE.release();
+        }
     }
 
 

http://dive4elements.wald.intevation.org