Re: Multithreaded construction of pages

2010-12-05 Thread 7zark7

Would love to see your code, are you using scala.actors.Futures?


On 12/4/10 4:26 AM, NielsBo wrote:


Hi

I would like to share my experience with implementing multithreading in my
Wicket application.
The problem was pages containing many independent panels each fething data
from external services, and the result being slow pages because each panel
is processed one at a time in Wicket.

The solution involved creating a AsyncLoadableDetachableModel class that
does the loading in a separate thread. I use the
java.util.concurrent.ExecutorService threadpool and the
AsyncLoadableDetachableModel then contains a Future object that synchronizes
with the main thread when getObject() is called.
The threads are started by an IComponentOnBeforeRenderListener on the
application.

Session data is attached/detached to the Threads so they execute the loading
in a context of the end user. I don't use the Wicket Session for this, but a
separate class that implements a MySession interface with properties like
userid, locale etc. that are copied from the Wicket session before starting
the threads.

I think this turned out very nice, and I can now simply replace a use of
LoadableDetachableModel with my new AsyncLoadableDetachableModel where data
is loaded from external services. And it works perfectly! Response time on
the pages are now the time of the slowest panel. Even with just one panel
use the Async model, there can still be an effect if another slow panel is
before the async one in the page/panel hierachy.

Best regards
Niels Bo






-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Multithreaded construction of pages

2010-12-05 Thread NielsBo
 = CoreSession.createServiceSession();
 started = System.currentTimeMillis();
 future = executor.submit(this);
}

public T call() throws Exception {
session.set();
T t = loadable.asyncLoad();
session.remove();
return t;
}

public T get() {
try {
if(TRACE) {
long runtime = System.currentTimeMillis() - started;
log.info(Async loading +runtime+ ms before sync
+loadable.getClass().getSimpleName());
}
return future.get();
} catch (Exception e) {
e.printStackTrace();
}
return null;

}

/**
 * Start the loading of all async components and models
 * @param app
 */
public static void enableOnApplication(Application app) {

app.addPostComponentOnBeforeRenderListener(
new IComponentOnBeforeRenderListener() {
public void onBeforeRender(Component component) {
if(component.isVisible()) {
if (component.getDefaultModel() instanceof
AsyncLoadableDetachableModel) {
((AsyncLoadableDetachableModel)
component.getDefaultModel()).startloading();
} 
if (component instanceof DataTable) {
IDataProvider dataProvider = ((DataTable)
component).getDataProvider();
if(dataProvider instanceof AsyncDataProvider) {
   
((AsyncDataProvider)dataProvider).startloading();
}
}}
}
});

}

/**
 * Shut doen the the executor service
 */
public static void shutdown() {
executor.shutdown();
}

public static void enableTrace(boolean trace) {
TRACE = trace;
}
}


interface IAsyncLoadableT {
/**
 *
 * @return
 */
T asyncLoad();
}

-- 
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Multithreaded-construction-of-pages-tp3072354p3073186.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Multithreaded construction of pages

2010-12-04 Thread NielsBo

Hi

I would like to share my experience with implementing multithreading in my
Wicket application.
The problem was pages containing many independent panels each fething data
from external services, and the result being slow pages because each panel
is processed one at a time in Wicket. 

The solution involved creating a AsyncLoadableDetachableModel class that
does the loading in a separate thread. I use the
java.util.concurrent.ExecutorService threadpool and the
AsyncLoadableDetachableModel then contains a Future object that synchronizes
with the main thread when getObject() is called. 
The threads are started by an IComponentOnBeforeRenderListener on the
application.

Session data is attached/detached to the Threads so they execute the loading
in a context of the end user. I don't use the Wicket Session for this, but a
separate class that implements a MySession interface with properties like
userid, locale etc. that are copied from the Wicket session before starting
the threads.

I think this turned out very nice, and I can now simply replace a use of
LoadableDetachableModel with my new AsyncLoadableDetachableModel where data
is loaded from external services. And it works perfectly! Response time on
the pages are now the time of the slowest panel. Even with just one panel
use the Async model, there can still be an effect if another slow panel is
before the async one in the page/panel hierachy. 

Best regards
Niels Bo



-- 
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Multithreaded-construction-of-pages-tp3072354p3072354.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: Multithreaded construction of pages

2010-12-04 Thread robert.mcguinness

Can you show some code?  I created something similar but I'm curious about
your implementation.  

https://gist.github.com/706011 https://gist.github.com/706011 
-- 
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Multithreaded-construction-of-pages-tp3072354p3072948.html
Sent from the Users forum mailing list archive at Nabble.com.

-
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org