|
| 1 | +package org.nhindirect.smtpmq.gateway.task; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Calendar; |
| 5 | + |
| 6 | +import javax.mail.Message; |
| 7 | +import javax.mail.Session; |
| 8 | +import javax.mail.internet.InternetAddress; |
| 9 | +import javax.mail.internet.MimeMessage; |
| 10 | + |
| 11 | +import org.nhindirect.common.mail.SMTPMailMessage; |
| 12 | +import org.nhindirect.smtpmq.gateway.streams.SmtpGatewayMessageSource; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.beans.factory.annotation.Value; |
| 17 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 18 | +import org.springframework.scheduling.annotation.Scheduled; |
| 19 | +import org.springframework.stereotype.Component; |
| 20 | + |
| 21 | +@ConditionalOnProperty(name="direct.smtpmqgateway.loadgen.rate", matchIfMissing=false) |
| 22 | +@Component |
| 23 | +public class SimulatedLoadTask |
| 24 | +{ |
| 25 | + private static final Logger LOGGER = LoggerFactory.getLogger(SimulatedLoadTask.class); |
| 26 | + |
| 27 | + @Value("${direct.smtpmqgateway.loadgen.sender}") |
| 28 | + protected String sender; |
| 29 | + |
| 30 | + @Value("${direct.smtpmqgateway.loadgen.recipient}") |
| 31 | + protected String recipient; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + protected SmtpGatewayMessageSource msgSource; |
| 35 | + |
| 36 | + @Scheduled(fixedRateString = "${direct.smtpmqgateway.loadgen.rate}", initialDelayString = "${direct.smtpmqgateway.loadgen.initialdelay:5000}") |
| 37 | + public void sendMessages() throws Exception |
| 38 | + { |
| 39 | + final MimeMessage msg = new MimeMessage((Session)null); |
| 40 | + msg.setFrom(new InternetAddress(sender)); |
| 41 | + msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); |
| 42 | + msg.setSentDate(Calendar.getInstance().getTime()); |
| 43 | + msg.setText("Load Test"); |
| 44 | + |
| 45 | + SMTPMailMessage smptMsg = new SMTPMailMessage(msg, Arrays.asList(new InternetAddress(recipient)), new InternetAddress(sender)); |
| 46 | + |
| 47 | + LOGGER.info("Sending generated load from " + sender + " to " + recipient); |
| 48 | + |
| 49 | + msgSource.forwardSMTPMessage(smptMsg); |
| 50 | + } |
| 51 | +} |
0 commit comments