chibenwa commented on a change in pull request #880:
URL: https://github.com/apache/james-project/pull/880#discussion_r803310706



##########
File path: 
server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/Requeue.java
##########
@@ -0,0 +1,118 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.transport.mailets;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Optional;
+
+import javax.inject.Inject;
+import javax.mail.MessagingException;
+
+import org.apache.james.lifecycle.api.LifecycleUtil;
+import org.apache.james.queue.api.MailQueue;
+import org.apache.james.queue.api.MailQueueFactory;
+import org.apache.james.queue.api.MailQueueName;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+import com.google.common.base.Preconditions;
+
+/**
+ * <p><b>Requeue</b> puts back the email in a queue.
+ * It can be used for throttling when combined with rate limiting mailet. 
{@link org.apache.james.transport.mailets.PerSenderRateLimit}</p>
+ *
+ * <ul>Here are supported configuration parameters:
+ *      <li><b>queue</b>: a Mail Queue name (optional, default to spool).</li>
+ *      <li><b>processor</b>: a target processor (optional, defaults to 
root).</li>
+ *      <li><b>delay</b>: a delay when en-queueing mail (optional, defaults to 
none). Supported units include: s (second), m (minute), h (hour), d (day).</li>
+ *      <li><b>consume</b>: a processor to which current email should be 
redirected to (optional, defaults to ghost).</li>
+ *  </ul>
+ *
+ *  <p>For instance, to apply all the examples given above:</p>
+ *
+ *  <pre><code>
+ *  &lt;mailet matcher=&quot;All&quot; class=&quot;Requeue&quot;&gt;
+ *      &lt;queue&gt;spool&lt;/queue&gt;
+ *      &lt;processor&gt;root&lt;/processor&gt;
+ *      &lt;delay&gt;2h&lt;/delay&gt;
+ *      &lt;consume&gt;ghost&lt;/consume&gt;
+ *  &lt;/mailet&gt;
+ *   </code></pre>
+ *
+ * <p>Note that this is a naive approach: if you have a large number of emails 
(say N) submitted at once,
+ * you would need O(N) re-queues to eventually send all your emails,
+ * where the constant is the number of mail allowed over the time window.
+ * So this gives an overall complexity of O(N2), while careful planning could 
achieve this in O(N). </p>
+ */
+public class Requeue extends GenericMailet {
+    private final MailQueueFactory<?> mailQueueFactory;
+
+    private MailQueue mailQueue;
+    private Optional<Duration> delayDuration;
+    private String processor;
+    private String consumeProcessor;
+
+    /**
+     *
+     * @param mailQueueFactory Allows to get mail queue by name.
+     */
+    @Inject
+    public Requeue(MailQueueFactory<?> mailQueueFactory) {
+        this.mailQueueFactory = mailQueueFactory;
+    }
+
+    @Override
+    public void init() throws MessagingException {
+        MailQueueName mailQueueName = 
Optional.ofNullable(getInitParameter("queue"))
+            .map(MailQueueName::of).orElse(MailQueueFactory.SPOOL);
+
+        mailQueue = mailQueueFactory.getQueue(mailQueueName)
+            .orElseThrow(() -> new IllegalArgumentException("Can not find 
queue " + mailQueueName.asString()));
+        delayDuration = Optional.ofNullable(getInitParameter("delay"))
+            .map(delayValue -> DurationParser.parse(delayValue, 
ChronoUnit.SECONDS));
+        processor = Optional.ofNullable(getInitParameter("processor"))
+            .orElse(Mail.DEFAULT);
+        consumeProcessor = Optional.ofNullable(getInitParameter("consume"))
+            .orElse(Mail.GHOST);
+
+        Preconditions.checkArgument(delayDuration.isEmpty() || 
!delayDuration.get().isNegative(),
+            "Duration should be non-negative");
+    }
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        mail.setState(consumeProcessor);

Review comment:
       > Does it make sense?
   
   Yes, if you want for some reason to duplicate the mail.
   
   >  While The purpose of this mailet is "throttling"?
   
   The purpose of this mailet is "Requeue". Period. It can be used for 
throttling but it is just one of the many possible usages.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to