Search in sources :

Example 1 with Sender

use of com.topcom.cms.yuqing.task.sender.Sender in project topcom-cloud by 545314690.

the class WarningJob method sendEmail.

private void sendEmail(Page page, CustomSubject subject, Long wangningLogId) {
    WarningTask warningTask = subject.getWarning();
    // logger.info("开始推送Email消息:" + subject.getName());
    List<Contact> contacts = warningTask.getContacts();
    /**
     * 获取所有联系人
     */
    Set<String> emailSet = contacts.stream().filter(contact -> Contact.Type.EMAIL.equals(contact.getType())).map(contact -> contact.getAccount()).collect(Collectors.toSet());
    logger.info("开始推送Email消息:" + subject.getName() + emailSet.toString());
    if (emailSet.size() > 0) {
        /**
         *先保存预警日志,否则下面warningLog.getId()取到的日志Id总为0,保存日志的时候,不要调用save
         */
        // 发送邮件
        SenderFactory emailSenderFactory = new EmailSenderFactory();
        Sender emailSender = emailSenderFactory.create();
        String[] emails = {};
        emails = emailSet.toArray(emails);
        try {
            WarningEmailBody warningEmailBody = new WarningEmailBody();
            warningEmailBody.setContent(page.getContent());
            warningEmailBody.setSpecialNumber(String.valueOf(page.getTotalElements()));
            warningEmailBody.setSpecialName(subject.getName());
            User user = userManager.findById(subject.getUserId());
            warningEmailBody.setUser(user.getFullName() == null ? user.getUsername() : user.getFullName());
            // warningEmailBody.setSubject("");
            warningEmailBody.setSpecialUrl(warningEmailSetting.getSubjectUrl() + subject.getId());
            warningEmailBody.setWarningLogUrl(warningEmailSetting.getLogUrl() + wangningLogId);
            WarningEmail warningEmail = new WarningEmail().create(emails, warningEmailBody);
            emailSender.send(warningEmail);
            // 更新上次预警时间
            warningTask.setLastWarningDate(new Date());
            warningManager.save(warningTask);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : WarningEmailBody(com.topcom.cms.yuqing.vo.email.WarningEmailBody) Date(java.util.Date) TemplateException(freemarker.template.TemplateException) Logger(org.jboss.logging.Logger) Autowired(org.springframework.beans.factory.annotation.Autowired) MessagingException(javax.mail.MessagingException) Job(org.quartz.Job) com.topcom.cms.yuqing.domain(com.topcom.cms.yuqing.domain) SubscriptionFollowerManager(com.topcom.cms.yuqing.service.SubscriptionFollowerManager) Map(java.util.Map) Pageable(org.springframework.data.domain.Pageable) CustomSubjectManager(com.topcom.cms.yuqing.service.CustomSubjectManager) Sender(com.topcom.cms.yuqing.task.sender.Sender) ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) WechatUtil(com.topcom.cms.yuqing.utils.WechatUtil) WebUtils(org.springframework.web.util.WebUtils) JobExecutionContext(org.quartz.JobExecutionContext) UserManager(com.topcom.cms.service.UserManager) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) User(com.topcom.cms.domain.User) WechatSendRequest(com.topcom.cms.yuqing.vo.request.WechatSendRequest) WarningEmailSetting(com.topcom.cms.yuqing.vo.email.WarningEmailSetting) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) IOException(java.io.IOException) Page(org.springframework.data.domain.Page) WarningManager(com.topcom.cms.yuqing.service.WarningManager) Collectors(java.util.stream.Collectors) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) JobExecutionException(org.quartz.JobExecutionException) WarningLogManager(com.topcom.cms.yuqing.service.WarningLogManager) Component(org.springframework.stereotype.Component) List(java.util.List) WeChat(com.topcom.cms.data.domain.WeChat) WarningEmail(com.topcom.cms.yuqing.vo.email.WarningEmail) PostConstruct(javax.annotation.PostConstruct) JSONObject(net.sf.json.JSONObject) WarningEmailBody(com.topcom.cms.yuqing.vo.email.WarningEmailBody) User(com.topcom.cms.domain.User) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) WarningEmail(com.topcom.cms.yuqing.vo.email.WarningEmail) Date(java.util.Date) TemplateException(freemarker.template.TemplateException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) Sender(com.topcom.cms.yuqing.task.sender.Sender) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory)

Example 2 with Sender

use of com.topcom.cms.yuqing.task.sender.Sender in project topcom-cloud by 545314690.

the class BriefingJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    Long briefingTaskId = (Long) context.getMergedJobDataMap().get("briefingTaskId");
    BriefingTask briefingTask = briefingTaskManager.findById(briefingTaskId);
    logger.info("任务运行:" + briefingTask.getBriefingType() + ":" + briefingTaskId);
    /**
     * 生成月报
     */
    JSONObject briefing = null;
    try {
        briefing = (JSONObject) briefingCreator.create(briefingTask).get();
        logger.info("任务运行:生成报告成功");
    } catch (BusinessException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
        logger.info("任务运行:生成报告失败");
    }
    List<Contact> contacts = briefingTask.getContacts();
    // 过滤出email联系人
    Set<String> emailSet = contacts.stream().filter(contact -> Contact.Type.EMAIL.equals(contact.getType())).map(contact -> contact.getAccount()).collect(Collectors.toSet());
    if (emailSet.size() > 0) {
        SenderFactory emailSenderFactory = new EmailSenderFactory();
        Sender emailSender = emailSenderFactory.create();
        String[] emails = {};
        emails = emailSet.toArray(emails);
        if (briefing != null) {
            try {
                BriefingEmail briefingEmail = new BriefingEmail().create(emails, briefing);
                briefingEmail.setSubject(SUBJECT);
                emailSender.send(briefingEmail);
            } catch (IOException | TemplateException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) TemplateException(freemarker.template.TemplateException) Logger(org.jboss.logging.Logger) BusinessException(com.topcom.cms.exception.BusinessException) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) Job(org.quartz.Job) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) JobExecutionException(org.quartz.JobExecutionException) ExecutionException(java.util.concurrent.ExecutionException) BriefingEmail(com.topcom.cms.yuqing.vo.email.BriefingEmail) Component(org.springframework.stereotype.Component) List(java.util.List) BriefingTask(com.topcom.cms.yuqing.domain.BriefingTask) BriefingTaskManager(com.topcom.cms.yuqing.service.BriefingTaskManager) JSONObject(net.sf.json.JSONObject) Contact(com.topcom.cms.yuqing.domain.Contact) Sender(com.topcom.cms.yuqing.task.sender.Sender) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) BriefingEmail(com.topcom.cms.yuqing.vo.email.BriefingEmail) TemplateException(freemarker.template.TemplateException) BusinessException(com.topcom.cms.exception.BusinessException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Contact(com.topcom.cms.yuqing.domain.Contact) Sender(com.topcom.cms.yuqing.task.sender.Sender) BusinessException(com.topcom.cms.exception.BusinessException) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) JSONObject(net.sf.json.JSONObject) SenderFactory(com.topcom.cms.yuqing.task.sender.factory.SenderFactory) EmailSenderFactory(com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory) BriefingTask(com.topcom.cms.yuqing.domain.BriefingTask) JobExecutionException(org.quartz.JobExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Sender (com.topcom.cms.yuqing.task.sender.Sender)2 EmailSenderFactory (com.topcom.cms.yuqing.task.sender.factory.EmailSenderFactory)2 SenderFactory (com.topcom.cms.yuqing.task.sender.factory.SenderFactory)2 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 JSONObject (net.sf.json.JSONObject)2 Logger (org.jboss.logging.Logger)2 Job (org.quartz.Job)2 JobExecutionContext (org.quartz.JobExecutionContext)2 JobExecutionException (org.quartz.JobExecutionException)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 WeChat (com.topcom.cms.data.domain.WeChat)1 User (com.topcom.cms.domain.User)1 BusinessException (com.topcom.cms.exception.BusinessException)1 UserManager (com.topcom.cms.service.UserManager)1 com.topcom.cms.yuqing.domain (com.topcom.cms.yuqing.domain)1