cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1298007 - /cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java
Date Wed, 07 Mar 2012 16:19:00 GMT
Author: dkulp
Date: Wed Mar  7 16:19:00 2012
New Revision: 1298007

URL: http://svn.apache.org/viewvc?rev=1298007&view=rev
Log:
Speed up the initial loading of pages by using the async methods.   Drops the initial camel
load from 90 seconds to about 20.
Can likely do the same for rendering later.

Modified:
    cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java

Modified: cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java
URL: http://svn.apache.org/viewvc/cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java?rev=1298007&r1=1298006&r2=1298007&view=diff
==============================================================================
--- cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java (original)
+++ cxf/web/src/main/java/org/apache/cxf/cwiki/SiteExporter.java Wed Mar  7 16:19:00 2012
@@ -33,19 +33,25 @@ import java.io.Writer;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.xml.datatype.DatatypeFactory;
 import javax.xml.datatype.XMLGregorianCalendar;
 import javax.xml.namespace.QName;
+import javax.xml.ws.AsyncHandler;
 import javax.xml.ws.Dispatch;
+import javax.xml.ws.Response;
 import javax.xml.ws.Service;
 import javax.xml.ws.soap.SOAPBinding;
 
@@ -98,10 +104,11 @@ public class SiteExporter implements Run
     static File rootOutputDir = new File(".");
     static String loginToken;
     static Dispatch<Document> dispatch;
+    static AtomicInteger asyncCount = new AtomicInteger();
 
-    Map<String, Page> pages = new HashMap<String, Page>();
-    List<Page> modifiedPages = new LinkedList<Page>();
-    Set<String> globalPages = new HashSet<String>();
+    Map<String, Page> pages = new ConcurrentHashMap<String, Page>();
+    Collection<Page> modifiedPages = new ConcurrentLinkedQueue<Page>();
+    Set<String> globalPages = new CopyOnWriteArraySet<String>();
 
     String spaceKey = "CXF";
     String pageCacheFile = "pagesConfig.obj";
@@ -227,12 +234,14 @@ public class SiteExporter implements Run
         
         for (Page p : modifiedPages) {
             if (globalPages.contains(p.getTitle())) {
-                modifiedPages = new LinkedList<Page>(pages.values());
+                modifiedPages.clear();
+                modifiedPages.addAll(pages.values());
                 break;
             }
         }
         if (forceAll) {
-            modifiedPages = new LinkedList<Page>(pages.values());
+            modifiedPages.clear();
+            modifiedPages.addAll(pages.values());
         }
 
         
@@ -591,17 +600,22 @@ public class SiteExporter implements Run
         doc.appendChild(el);
         doc = getDispatch().invoke(doc);
         
-        Set<String> allPages = new HashSet<String>(pages.keySet());
-        Set<Page> newPages = new HashSet<Page>();
+        Set<String> allPages = new CopyOnWriteArraySet<String>(pages.keySet());
+        Set<Page> newPages = new CopyOnWriteArraySet<Page>();
+        List<Future<?>> futures = new ArrayList<Future<?>>(allPages.size());
         
         //XMLUtils.printDOM(doc.getDocumentElement());
         Node nd = doc.getDocumentElement().getFirstChild().getFirstChild();
         while (nd != null) {
             if (nd instanceof Element) {
-                loadPage((Element)nd, allPages, newPages);
+                futures.add(loadPage((Element)nd, allPages, newPages));
             }
             nd = nd.getNextSibling();
         }
+        for (Future<?> f : futures) {
+            //wait for all the pages to be done
+            f.get();
+        }
         for (Page p : newPages) {
             //pages have been added, need to check
             checkForChildren(p);
@@ -660,9 +674,9 @@ public class SiteExporter implements Run
         }
     }
     
-    public void loadPage(Element pageSumEl,
-                         Set<String> allPages,
-                         Set<Page> newPages) throws Exception {
+    public Future<?> loadPage(Element pageSumEl,
+                         final Set<String> allPages,
+                         final Set<Page> newPages) throws Exception {
         Document doc = XMLUtils.newDocument();
         Element el = doc.createElementNS(SOAPNS, "ns1:getPage");
         Element el2 = doc.createElement("in0");
@@ -672,20 +686,35 @@ public class SiteExporter implements Run
         el.appendChild(el2);
         el2.setTextContent(DOMUtils.getChildContent(pageSumEl, "id"));
         doc.appendChild(el);
-        doc = getDispatch().invoke(doc);
         
-        Page page = new Page(doc);
-        Page oldPage = pages.put(page.getId(), page);
-        if (oldPage == null || page.getModifiedTime().compare(oldPage.getModifiedTime())
> 0) {
-            modifiedPages.add(page);
-            if (oldPage == null) {
-                //need to check parents to see if it has a {children} tag so we can re-render
-                newPages.add(page);
+        //make sure we only fire off about 15-20 or confluence may get a bit overloaded
+        while (asyncCount.get() > 15) {
+            Thread.sleep(10);
+        }
+        asyncCount.incrementAndGet();
+        Future<?> f = getDispatch().invokeAsync(doc, new AsyncHandler<Document>()
{
+            public void handleResponse(Response<Document> doc) {
+                try {
+                    Page page = new Page(doc.get());
+                    Page oldPage = pages.put(page.getId(), page);
+                    if (oldPage == null || page.getModifiedTime().compare(oldPage.getModifiedTime())
> 0) {
+                        modifiedPages.add(page);
+                        if (oldPage == null) {
+                            //need to check parents to see if it has a {children} tag so
we can re-render
+                            newPages.add(page);
+                        }
+                    }
+                    if (allPages.contains(page.getId())) {
+                        allPages.remove(page.getId());
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                } finally {
+                    asyncCount.decrementAndGet();
+                }
             }
-        }
-        if (allPages.contains(page.getId())) {
-            allPages.remove(page.getId());
-        }
+        });
+        return f;
     }    
     
     private String updateContentLinks(Page page, String content,



Mime
View raw message