chibenwa commented on code in PR #2469: URL: https://github.com/apache/james-project/pull/2469#discussion_r1815147874
########## server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/MailToAllUsers.java: ########## @@ -0,0 +1,116 @@ +/**************************************************************** + * 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.util.Optional; + +import jakarta.inject.Inject; +import jakarta.mail.MessagingException; + +import org.apache.james.core.MailAddress; +import org.apache.james.core.Username; +import org.apache.james.lifecycle.api.LifecycleUtil; +import org.apache.james.user.api.UsersRepository; +import org.apache.mailet.Mail; +import org.apache.mailet.base.GenericMailet; + +import com.github.fge.lambdas.Throwing; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +/** + * A mailet that helps to email all users in the system. The emails are sent in batches to manage + * the load. The first batch is sent directly, while the remaining batches are sent asynchronously. + * The batch size can be configured via the <b>batchSize</b> parameter (optional, defaults to 100). + * + * <h3>Configuration</h3> + * <pre><code> + * {@code + * <matcher name="notify-matcher" match="org.apache.james.mailetcontainer.impl.matchers.And"> + * <matcher match="SenderIs=ad...@gov.org"/> + * <matcher match="RecipientIs=a...@gov.org"/> + * </matcher> + * <mailet match="notify-matcher" class="MailToAllUsers"> + * <batchSize>100</batchSize> + * </mailet> + * } + * </code></pre> + * + */ +public class MailToAllUsers extends GenericMailet { + private static final int DEFAULT_BATCH_SIZE = 100; + private final UsersRepository usersRepository; + private int batchSize; + + @Inject + public MailToAllUsers(UsersRepository usersRepository) { + this.usersRepository = usersRepository; + } + + @Override + public void init() throws MessagingException { + batchSize = Integer.parseInt(Optional.ofNullable(getInitParameter("batchSize")) + .orElse(String.valueOf(DEFAULT_BATCH_SIZE))); + } + + @Override + public void service(Mail mail) throws MessagingException { + Flux<Flux<MailAddress>> recipientsBatches = Flux.from(usersRepository.listReactive()) + .map(Throwing.function(Username::asMailAddress)) + .window(batchSize); + + Flux.merge(sendMailToFirstRecipientsBatchDirectly(mail, recipientsBatches), + sendMailToRemainingRecipientsBatchAsynchronously(mail, recipientsBatches)) Review Comment: Yes but because it is subscribed twice data may defer. -- 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: notifications-unsubscr...@james.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org