Activiti 自定义事件监听和邮件通知

Activiti 自定义事件监听和邮件通知

参照Activiti官方文档https://www.activiti.org/userguide/#eventDispatcher

首先,建立我们的需求。即当有任务创建、分配、完成时,邮件通知对应用户。

完成步骤如下:

  1. Activiti为我们提供了事件监听接口– ActivitiEventListener

  2. org.activiti.engine.delegate.event.ActivitiEventType提供了各种可以监听到的事件类型

  3. 通过对应事件,获取有关联的用户,任务名称等。
public class OwnEventListener implements ActivitiEventListener {

    @Override
    public void onEvent(ActivitiEvent event) {
        System.out.println("Event received: " + event.getType());

        if (event.getType() == ActivitiEventType.TASK_CREATED
                || event.getType() == ActivitiEventType.TASK_ASSIGNED
                || event.getType() == ActivitiEventType.TASK_COMPLETED) {
            ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
            TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();
            MailWrapper.getInstance().sendTaskMail(taskEntity.getOwner(), taskEntity.getAssignee(), taskEntity.getName(), event.getType());
        }
    }

最重要的一步就是强制将ActivitiEvent转换为其sub-interface(ActivitiEntityEvent),再将获取的Entity强制转换为具体的Entity,最终获取相应信息。

在邮件发送模块,需要构建邮件模版,单例化邮件组件。

public class MailWrapper {
    private final Logger log = LoggerFactory.getLogger(MailWrapper.class);

    @Autowired
    private MailSender mailSender;

    private MailWrapper() {
    Velocity.init(PropertiesUtil.readPropertiesInEnv("vm.properties"));
    }

    private static class WrapperSingleton {
        static final MailWrapper mv = new MailWrapper();
    }

    public static MailWrapper getInstance() {
        return WrapperSingleton.mv;
    }

    public void sendTaskMail(String owner, String assignee, String taskName, ActivitiEventType eventType) {
        SimpleMailMessage msg = new SimpleMailMessage();
        String toEmailAddress = KeystoneClient.getInstance().findEmailByUserId(assignee);
        String ownerName = KeystoneClient.getInstance().findNameByUserId(owner);
        String assigneeName = KeystoneClient.getInstance().findNameByUserId(assignee);

        TaskMail tMail = new TaskMail(ownerName, assigneeName, taskName, eventType);
        VelocityContext model = new VelocityContext();
        model.put("task", tMail);

        Template template = Velocity.getTemplate("template/task_email.vm", "utf8");

        StringWriter w = new StringWriter();
        template.merge(model, w);

        msg.setSubject("hello world");
        msg.setTo(toEmailAddress);
        msg.setText(w.toString());

        try {
            mailSender.send(msg);

            log.info("mail has been sent to " + toEmailAddress);
        } catch (MailException e) {
            System.err.println(e.getMessage());
        }
    }
}

在构建模版时,使用的是org.apache.velocity组件,其资源配置文件如下:

resource.loader = class
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

告诉velocity,加载我们定义的模版文件时,使用classloader资源路径。当然,也可以配置物理路径,具体请参照官网,这里不加赘述。

你可能感兴趣的:(Activiti)