Search in sources :

Example 16 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class Emailer method migrateMessagesBatch.

protected int migrateMessagesBatch() {
    List<SendingMessage> resultList;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String qstr = "select m from sys$SendingMessage m where m.contentText is not null";
        TypedQuery<SendingMessage> query = em.createQuery(qstr, SendingMessage.class);
        query.setMaxResults(50);
        query.setViewName(View.MINIMAL);
        resultList = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    if (!resultList.isEmpty()) {
        emailer.migrateEmailsToFileStorage(resultList);
    }
    return resultList.size();
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 17 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class EmailerTest method doTestAsynchronous.

private void doTestAsynchronous(boolean useFs) throws Exception {
    emailerConfig.setFileStorageUsed(useFs);
    testMailSender.clearBuffer();
    String body = "Test Email Body";
    EmailInfo myInfo = new EmailInfo("recipient@example.com", "Test", body);
    List<SendingMessage> messages = emailer.sendEmailAsync(myInfo);
    assertEquals(1, messages.size());
    // not sent yet
    assertTrue(testMailSender.isEmpty());
    SendingMessage sendingMsg = reload(messages.get(0));
    assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
    // run scheduler
    emailer.processQueuedEmails();
    // check
    assertEquals(1, testMailSender.getBufferSize());
    MimeMessage msg = testMailSender.fetchSentEmail();
    assertEquals(1, msg.getAllRecipients().length);
    assertEquals("recipient@example.com", msg.getAllRecipients()[0].toString());
    assertEquals("Test", msg.getSubject());
    assertEquals(body, getBody(msg));
    assertTrue(getBodyContentType(msg).startsWith("text/plain;"));
    sendingMsg = reload(messages.get(0));
    assertEquals(SendingStatus.SENT, sendingMsg.getStatus());
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 18 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class EmailerTest method testAsynchronousAttemptLimit.

@Test
public void testAsynchronousAttemptLimit() throws Exception {
    testMailSender.clearBuffer();
    String body = "Test Email Body";
    EmailInfo myInfo = new EmailInfo("recipient@example.com", "Test", body);
    List<SendingMessage> messages = emailer.sendEmailAsync(myInfo, 2, getDeadlineWhichDoesntMatter());
    assertEquals(1, messages.size());
    // not sent yet
    assertTrue(testMailSender.isEmpty());
    SendingMessage sendingMsg = reload(messages.get(0));
    assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
    // will fail
    testMailSender.failPlease();
    try {
        // try once
        emailer.processQueuedEmails();
        sendingMsg = reload(sendingMsg);
        assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
        // try second time
        emailer.processQueuedEmails();
        sendingMsg = reload(sendingMsg);
        assertEquals(SendingStatus.QUEUE, sendingMsg.getStatus());
    } finally {
        testMailSender.workNormallyPlease();
    }
    // marks as not-sent in the next tick
    emailer.processQueuedEmails();
    sendingMsg = reload(sendingMsg);
    assertEquals(SendingStatus.NOTSENT, sendingMsg.getStatus());
    assertEquals(2, sendingMsg.getAttemptsCount().intValue());
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) Test(org.junit.Test)

Example 19 with SendingMessage

use of com.haulmont.cuba.core.entity.SendingMessage in project cuba by cuba-platform.

the class EmailerTest method doTestLoadBody.

private void doTestLoadBody(boolean useFs) throws Exception {
    emailerConfig.setFileStorageUsed(useFs);
    String body = "Hi! This is test email. Bye.";
    EmailInfo emailInfo = new EmailInfo("test@example.com", "Test", body);
    List<SendingMessage> messages = emailer.sendEmailAsync(emailInfo);
    SendingMessage msg = reload(messages.get(0));
    String actualBody = emailer.loadContentText(msg);
    assertEquals(body, actualBody);
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Aggregations

SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)19 EntityManager (com.haulmont.cuba.core.EntityManager)8 Transaction (com.haulmont.cuba.core.Transaction)8 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5 AddressException (javax.mail.internet.AddressException)5 MailSendException (org.springframework.mail.MailSendException)5 SendingAttachment (com.haulmont.cuba.core.entity.SendingAttachment)3 MimeMessage (javax.mail.internet.MimeMessage)2 Test (org.junit.Test)2 Nullable (javax.annotation.Nullable)1 Address (javax.mail.Address)1