You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by George Christman <gc...@cardaddy.com> on 2013/11/04 19:41:59 UTC

A couple questions about the Tapestry5 How To Work Queue example

Hello, I found the following example WorkQueue example over on
http://wiki.apache.org/tapestry/Tapestry5HowToWorkQueue and I'm trying to
use Tapestry as much as possible, but I have a couple questions.

1. The first thing I noticed was RegistryShutdownListener has been
deprecated in 5.3, I'm wondering if there is any replacement in 5.4? I
didn't see anything in the API referencing any kind of replacement.

2. If I made a service out of MailQueue, and fired off a list of
MailQueue.queueEmails(); would they automatically be added to the
threadpool of the extended class?

3. Does anybody know how to detect the completion of all the threads? --
I'm not sure if Tapestry provides any assistance.

4. In a Tapestry stateless service, can I still create a static final
object?

Thanks guys.

 package tapestryutil.services;  import
java.util.concurrent.LinkedBlockingQueue; import
java.util.concurrent.ThreadPoolExecutor; import
java.util.concurrent.TimeUnit;  import
org.apache.tapestry5.ioc.services.PerthreadManager; import
org.apache.tapestry5.ioc.services.RegistryShutdownHub; import
org.apache.tapestry5.ioc.services.RegistryShutdownListener; import
org.slf4j.Logger;  /**WorkQueue implementation that is in-line with
tapestry practices regarding threads.  * It is important when using
tapestry services to call PerthreadManager.cleanup(); after  * a task
if the same thread that executed the task will be reused again.  *   *
@see {@link ThreadPoolExecutor}*/ public class WorkQueue extends
ThreadPoolExecutor implements RegistryShutdownListener{      protected
final PerthreadManager _perthreadManager;     private final Logger
_log;      public WorkQueue(PerthreadManager perthreadManager, Logger
log, RegistryShutdownHub hub){         super(1, 1, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
_perthreadManager = perthreadManager;         _log = log;
hub.addRegistryShutdownListener(this);     }          @Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);         _perthreadManager.cleanup();     }
   public void registryDidShutdown(){         int activeCount =
getActiveCount();         if(activeCount > 0)
_log.warn(String.format("Shutting down worker and waiting for %d tasks
to finish", activeCount));         shutdown();     }  }


package tapestryutil.services;import
org.apache.tapestry5.ioc.services.PerthreadManager;import
org.apache.tapestry5.ioc.services.RegistryShutdownHub;import
org.slf4j.Logger;
//Modified as a service. public class MailQueueImpl extends WorkQueue
implements MailQueue {    public MailQueue(PerthreadManager
perthreadManager, Logger log, RegistryShutdownHub hub) {
super(perthreadManager, log, hub);    }        public void
queueEmail(final String address, final String subject){
execute(new Runnable(){            public void run() {
sendEmail(address, subject);            }        });    }
private void sendEmail(String address, String subject){        //send
email    }}

public Interface MailQueue {

    public void queueEmail(final String address, final String subject);

}


-- 
George Christman
www.CarDaddy.com
P.O. Box 735
Johnstown, New York