Search in sources :

Example 11 with SendingMessage

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

the class Emailer method persistMessageIfPossible.

/*
     * Try to persist message and catch all errors to allow actual delivery
     * in case of database or file storage failure.
     */
@Nullable
protected SendingMessage persistMessageIfPossible(SendingMessage sendingMessage) {
    // to avoid additional overhead to load body and attachments back from FS
    try {
        SendingMessage clonedMessage = createClone(sendingMessage);
        persistMessages(Collections.singletonList(clonedMessage), SendingStatus.SENDING);
        return clonedMessage;
    } catch (Exception e) {
        log.error("Failed to persist message " + sendingMessage.getCaption(), e);
        return null;
    }
}
Also used : SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException) Nullable(javax.annotation.Nullable)

Example 12 with SendingMessage

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

the class Emailer method convertToSendingMessage.

protected SendingMessage convertToSendingMessage(String address, String from, String caption, String body, @Nullable List<EmailHeader> headers, @Nullable EmailAttachment[] attachments, @Nullable Integer attemptsCount, @Nullable Date deadline) {
    SendingMessage sendingMessage = metadata.create(SendingMessage.class);
    sendingMessage.setAddress(address);
    sendingMessage.setFrom(from);
    sendingMessage.setContentText(body);
    sendingMessage.setCaption(caption);
    sendingMessage.setAttemptsCount(attemptsCount);
    sendingMessage.setDeadline(deadline);
    sendingMessage.setAttemptsMade(0);
    if (attachments != null && attachments.length > 0) {
        StringBuilder attachmentsName = new StringBuilder();
        List<SendingAttachment> sendingAttachments = new ArrayList<>(attachments.length);
        for (EmailAttachment ea : attachments) {
            attachmentsName.append(ea.getName()).append(";");
            SendingAttachment sendingAttachment = toSendingAttachment(ea);
            sendingAttachment.setMessage(sendingMessage);
            sendingAttachments.add(sendingAttachment);
        }
        sendingMessage.setAttachments(sendingAttachments);
        sendingMessage.setAttachmentsName(attachmentsName.toString());
    } else {
        sendingMessage.setAttachments(Collections.<SendingAttachment>emptyList());
    }
    if (headers != null && !headers.isEmpty()) {
        StringBuilder headersLine = new StringBuilder();
        for (EmailHeader header : headers) {
            headersLine.append(header.toString()).append(SendingMessage.HEADERS_SEPARATOR);
        }
        sendingMessage.setHeaders(headersLine.toString());
    } else {
        sendingMessage.setHeaders(null);
    }
    replaceRecipientIfNecessary(sendingMessage);
    return sendingMessage;
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 13 with SendingMessage

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

the class Emailer method markAsNonSent.

protected void markAsNonSent(SendingMessage sendingMessage) {
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        SendingMessage msg = em.merge(sendingMessage);
        msg.setStatus(SendingStatus.NOTSENT);
        msg.setAttemptsMade(msg.getAttemptsMade() + 1);
        tx.commit();
    } catch (Exception e) {
        log.error("Error marking message to '{}' as not sent", sendingMessage.getAddress(), e);
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) AddressException(javax.mail.internet.AddressException) MailSendException(org.springframework.mail.MailSendException)

Example 14 with SendingMessage

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

the class Emailer method loadEmailsToSend.

protected List<SendingMessage> loadEmailsToSend() {
    Date sendTimeoutTime = DateUtils.addSeconds(timeSource.currentTimestamp(), -config.getSendingTimeoutSec());
    List<SendingMessage> emailsToSend = new ArrayList<>();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<SendingMessage> query = em.createQuery("select sm from sys$SendingMessage sm" + " where sm.status = :statusQueue or (sm.status = :statusSending and sm.updateTs < :time)" + " order by sm.createTs", SendingMessage.class);
        query.setParameter("statusQueue", SendingStatus.QUEUE.getId());
        query.setParameter("time", sendTimeoutTime);
        query.setParameter("statusSending", SendingStatus.SENDING.getId());
        View view = metadata.getViewRepository().getView(SendingMessage.class, "sendingMessage.loadFromQueue");
        // because SendingAttachment.content has FetchType.LAZY
        view.setLoadPartialEntities(true);
        query.setView(view);
        query.setMaxResults(config.getMessageQueueCapacity());
        List<SendingMessage> resList = query.getResultList();
        for (SendingMessage msg : resList) {
            if (shouldMarkNotSent(msg)) {
                msg.setStatus(SendingStatus.NOTSENT);
            } else {
                msg.setStatus(SendingStatus.SENDING);
                emailsToSend.add(msg);
            }
        }
        tx.commit();
    }
    for (SendingMessage message : emailsToSend) {
        loadBodyAndAttachments(message);
    }
    return emailsToSend;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SendingMessage(com.haulmont.cuba.core.entity.SendingMessage)

Example 15 with SendingMessage

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

the class Emailer method createClone.

protected SendingMessage createClone(SendingMessage srcMessage) {
    SendingMessage clonedMessage = metadata.getTools().copy(srcMessage);
    List<SendingAttachment> clonedList = new ArrayList<>();
    for (SendingAttachment srcAttach : srcMessage.getAttachments()) {
        SendingAttachment clonedAttach = (SendingAttachment) metadata.getTools().copy(srcAttach);
        clonedAttach.setMessage(null);
        clonedAttach.setMessage(clonedMessage);
        clonedList.add(clonedAttach);
    }
    clonedMessage.setAttachments(clonedList);
    return clonedMessage;
}
Also used : SendingAttachment(com.haulmont.cuba.core.entity.SendingAttachment) 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