jstrachan    02/05/07 08:59:44

  Modified:    messenger/src/java/org/apache/commons/messenger/task
                        ProducerTask.java
  Log:
  Added sleep property to the Producer task so that messages can be throttled with a 
simple sleep time between sends.
  
  Revision  Changes    Path
  1.3       +48 -3     
jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/task/ProducerTask.java
  
  Index: ProducerTask.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/task/ProducerTask.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ProducerTask.java 7 May 2002 11:52:32 -0000       1.2
  +++ ProducerTask.java 7 May 2002 15:59:43 -0000       1.3
  @@ -5,7 +5,7 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    * 
  - * $Id: ProducerTask.java,v 1.2 2002/05/07 11:52:32 jstrachan Exp $
  + * $Id: ProducerTask.java,v 1.3 2002/05/07 15:59:43 jstrachan Exp $
    */
   package org.apache.commons.messenger.task;
   
  @@ -39,7 +39,7 @@
    * using a given JMS Connection (Messenger) and a Destination
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public class ProducerTask extends Task {
   
  @@ -50,6 +50,9 @@
       private String subject;
       private MessengerManager messengerManager;    
   
  +    /** Holds value of property sleep. */
  +    private long sleep;
  +    
       // Properties
       //-------------------------------------------------------------------------
       
  @@ -140,6 +143,23 @@
       public void setConfiguration(String uri) throws JMSException {
           setMessengerManager( MessengerManager.load( uri ) );
       }
  +    
  +    /** Getter for property sleep, which defines the number of milliseconds to 
  +     * sleep for before each send.
  +     * @return Value of property sleep.
  +     */
  +    public long getSleep() {
  +        return sleep;
  +    }
  +    
  +    /** Setter for property sleep, which defines the number of milliseconds to 
  +     * sleep for before each send.
  +     * @param sleep New value of property sleep.
  +     */
  +    public void setSleep(long sleep) {
  +        this.sleep = sleep;
  +    }
  +    
       // Task interface
       //-------------------------------------------------------------------------
       
  @@ -159,8 +179,13 @@
               if ( destination == null ) {
                   throw new BuildException("Must specify a valid JMS Destination", 
location );
               }
  -
  +            
  +            if ( sleep > 0 ) {
  +                log( "Will sleep for: " + sleep + " (ms) between message sends" );
  +            }
  +                    
               // deal with the filesets
  +            boolean first = true;
               for (Iterator iter = filesets.iterator(); iter.hasNext(); ) {
                   FileSet fs = (FileSet) iter.next();
                   DirectoryScanner ds = fs.getDirectoryScanner(project);              
  
  @@ -168,7 +193,14 @@
   
                   File dir = ds.getBasedir();
                   String[] files = ds.getIncludedFiles();
  +                
                   for (int i = 0; i < files.length; i++) {
  +                    if ( first ) {
  +                        first = false;
  +                    }
  +                    else {
  +                        sleep();
  +                    }
                       sendFile( new File( dir, files[i]), messenger, destination );
                   }
               }
  @@ -221,6 +253,19 @@
           return buffer.toString();
       }
       
  +    /**
  +     * Sleeps for a configurable amount of time between each message send 
  +     */
  +    protected void sleep() {
  +        if ( sleep > 0 ) {
  +            try {
  +                Thread.currentThread().sleep(sleep);
  +            }
  +            catch (InterruptedException e) {
  +                // ignore interuptions
  +            }
  +        }
  +    }
   }
   
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to