Differences between revisions 1 and 2
Revision 1 as of 2008-09-23 16:23:59
Size: 4391
Editor: DavorHrg
Comment:
Revision 2 as of 2009-09-20 23:20:03
Size: 4393
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
One fine implementation already exists in java.concurrent package [http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html ThreadPoolExecutor] One fine implementation already exists in java.concurrent package [[http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html|ThreadPoolExecutor]]

for occasional tasks that need a separate thread Tapestry5HowToRunTaskInThread is a nice example.

But if you want to create a work queue, then the requirement for PerThreadManager.cleanup() changes.

In this case you are strongly recommended to call PerThreadManager.cleanup() after each task so that every task can have clean Threaded services (hibernate session for example).

If you start a long running thread and use hibernate session, after some time you will face "strange" problems with entities. Unlike code in pages and components that gets a fresh hibernate session for each request, you will soon end up with stale data.

One fine implementation already exists in java.concurrent package ThreadPoolExecutor

Only small ammount of code is needed to make it work well within tapestry

   1 package tapestryutil.services;
   2 
   3 import java.util.concurrent.LinkedBlockingQueue;
   4 import java.util.concurrent.ThreadPoolExecutor;
   5 import java.util.concurrent.TimeUnit;
   6 
   7 import org.apache.tapestry5.ioc.services.PerthreadManager;
   8 import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
   9 import org.apache.tapestry5.ioc.services.RegistryShutdownListener;
  10 import org.slf4j.Logger;
  11 
  12 /**WorkQueue implementation that is in-line with tapestry practices regarding threads.
  13  * It is important when using tapestry services to call PerthreadManager.cleanup(); after
  14  * a task if the same thread that executed the task will be reused again.
  15  * 
  16  * @see {@link ThreadPoolExecutor}*/
  17 public class WorkQueue extends ThreadPoolExecutor implements RegistryShutdownListener{
  18 
  19     protected final PerthreadManager _perthreadManager;
  20     private final Logger _log;
  21 
  22     public WorkQueue(PerthreadManager perthreadManager, Logger log, RegistryShutdownHub hub){
  23         super(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
  24         _perthreadManager = perthreadManager;
  25         _log = log;
  26         hub.addRegistryShutdownListener(this);
  27     }
  28     
  29     @Override
  30     protected void afterExecute(Runnable r, Throwable t) {
  31         super.afterExecute(r, t);
  32         _perthreadManager.cleanup();
  33     }
  34 
  35     public void registryDidShutdown(){
  36         int activeCount = getActiveCount();
  37         if(activeCount > 0) _log.warn(String.format("Shutting down worker and waiting for %d tasks to finish", activeCount));
  38         shutdown();
  39     }
  40 
  41 }

This is just a basic example from which you can work further. The limit for threads running is 1 but you can change this, or set the limits later on.

If you want to have less:  new Runnable(){...}  arround your code you, and you want to create a service that handles some data (but with a queue).... do this:

   1 package tapestryutil.services;
   2 
   3 import org.apache.tapestry5.ioc.services.PerthreadManager;
   4 import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
   5 import org.slf4j.Logger;
   6 
   7 public abstract class DataWorkerQueue<T> extends WorkQueue{
   8 
   9 
  10     public DataWorkerQueue(PerthreadManager perthreadManager, Logger log, RegistryShutdownHub hub){
  11         super(perthreadManager, log, hub);
  12     }
  13     
  14     public void queueData(final T data){
  15         execute(new Runnable(){
  16             public void run() {
  17                 handleData(data);
  18             }
  19         });
  20     }
  21     
  22     protected abstract void handleData(T data);
  23 
  24 }

Now all you need to make a WorkerQueue for some operation on some data is to extend this class and implement handleData()

If you don't mind Runnables .. you can use it like this

   1 package tapestryutil.services;
   2 
   3 import org.apache.tapestry5.ioc.services.PerthreadManager;
   4 import org.apache.tapestry5.ioc.services.RegistryShutdownHub;
   5 import org.slf4j.Logger;
   6 
   7 public class MailQueue extends WorkQueue{
   8 
   9     public MailQueue(PerthreadManager perthreadManager, Logger log, RegistryShutdownHub hub) {
  10         super(perthreadManager, log, hub);
  11     }
  12     
  13     public void queueEmail(final String address, final String subject){
  14         execute(new Runnable(){
  15             public void run() {
  16                 sendEmail(address, subject);
  17             }
  18         });
  19     }
  20     
  21     private void sendEmail(String address, String subject){
  22         //send email
  23     }
  24 
  25 }

Tapestry5HowToWorkQueue (last edited 2009-09-20 23:20:03 by localhost)