Search in sources :

Example 1 with EmailEvent

use of com.creditease.mspl.event.vo.EmailEvent in project uavstack by uavorg.

the class CEMailAction method buildEmailEvent.

/**
 * 装载消息
 *
 * @param notifyEvent
 * @return EmailEvent
 * @throws Exception
 */
private EmailEvent buildEmailEvent(NotificationEvent notifyEvent, String title, String mailTemplatePath) throws Exception {
    List<EmailEntity> list = new ArrayList<EmailEntity>();
    EmailEntity emailEntity = new EmailEntity();
    /**
     * 如果notifyEvent中含有目的地址,则用该地址,否则使用默认地址
     */
    String address = notifyEvent.getArg(cName);
    if (StringHelper.isEmpty(address)) {
        if (log.isTraceEnable()) {
            log.warn(this, "Send Mail FAIL as no any email addresses");
        }
        return null;
    }
    emailEntity.setToAddress(address);
    String html = IOHelper.readTxtFile(mailTemplatePath, "utf-8");
    if (StringHelper.isEmpty(html)) {
        log.err(this, "Send Mail FAIL as mail template is empty");
        return null;
    }
    /**
     * 变量替换
     */
    html = buildMailBody(html, notifyEvent);
    /**
     * 主题
     */
    emailEntity.setSubject(title);
    /**
     * 正文
     */
    emailEntity.setContent(html);
    /**
     * 添加附件
     */
    list.add(emailEntity);
    /**
     * 配置信息
     */
    EmailEvent emailEvent = new EmailEvent();
    BizProcess bizProcess = new BizProcess();
    bizProcess.setDealCode("mspl_ltn_0107");
    emailEvent.setBizProcess(bizProcess);
    emailEvent.setUserList(list);
    emailEvent.setActivityId(activeID);
    emailEvent.setChannelCode("channel 001");
    emailEvent.setMailType("消息类22");
    emailEvent.setSystemSign(systemSign);
    return emailEvent;
}
Also used : EmailEvent(com.creditease.mspl.event.vo.EmailEvent) ArrayList(java.util.ArrayList) EmailEntity(com.creditease.mspl.event.vo.EmailEntity) BizProcess(com.creditease.mspl.domain.BizProcess)

Example 2 with EmailEvent

use of com.creditease.mspl.event.vo.EmailEvent in project uavstack by uavorg.

the class CEMailAction method sendMail.

@Override
public boolean sendMail(String title, String mailTemplatePath, NotificationEvent notifyEvent) {
    if (log.isDebugEnable()) {
        log.debug(this, "Send Mail START: event=" + notifyEvent.toJSONString());
    }
    // ConnectionFactory :连接工厂,JMS 用它创建连接
    ActiveMQConnectionFactory connectionFactory;
    // Connection :JMS 客户端到JMS Provider 的连接
    Connection connection = null;
    // Session: 一个发送或接收消息的线程
    Session session = null;
    // Destination :消息的目的地;消息发送给谁.
    Queue destination;
    // MessageProducer:消息发送者
    javax.jms.MessageProducer producer = null;
    // TextMessage message;
    // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
    connectionFactory = new ActiveMQConnectionFactory(userName, password, brokerURL);
    try {
        // 构造从工厂得到连接对象
        connection = connectionFactory.createConnection();
        // 启动
        connection.start();
        // 获取操作连接
        session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
        destination = session.createQueue(queueName);
        // 得到消息生成者【发送者】
        producer = session.createProducer(destination);
        // 设置不持久化,此处学习,实际根据项目决定
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // 构造消息,此处写死,项目就是参数,或者方法获取
        EmailEvent emailEvent = buildEmailEvent(notifyEvent, title, mailTemplatePath);
        if (emailEvent == null) {
            return false;
        }
        // 序列化
        ActiveMQObjectMessage message = (ActiveMQObjectMessage) session.createObjectMessage();
        message.setObject(emailEvent);
        // 发送消息到目的地方
        producer.send(message);
        session.commit();
        return true;
    } catch (Exception e) {
        log.err(this, "Send Mail FAIL.", e);
    } finally {
        try {
            if (producer != null) {
                producer.close();
            }
        } catch (Throwable e) {
        // ignore
        }
        try {
            if (session != null) {
                session.close();
            }
        } catch (Throwable e) {
        // ignore
        }
        try {
            if (null != connection)
                connection.close();
        } catch (Throwable e) {
        // ignore
        }
    }
    return false;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) EmailEvent(com.creditease.mspl.event.vo.EmailEvent) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) Connection(javax.jms.Connection) Queue(javax.jms.Queue) Session(javax.jms.Session)

Aggregations

EmailEvent (com.creditease.mspl.event.vo.EmailEvent)2 BizProcess (com.creditease.mspl.domain.BizProcess)1 EmailEntity (com.creditease.mspl.event.vo.EmailEntity)1 ArrayList (java.util.ArrayList)1 Connection (javax.jms.Connection)1 Queue (javax.jms.Queue)1 Session (javax.jms.Session)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1 ActiveMQObjectMessage (org.apache.activemq.command.ActiveMQObjectMessage)1