WebcatEE 帮助文档


本文档的目标用户是 JAVA 开发人员。

本文档描述如何进行状态监听。

版本要求:1.2.8+

稳定接口

状态监听

状态监听用来监听工单状态变化。


1. 开发状态监听实现类,实现类必须实现 com.skin.webcat.api.order.WorkOrderEventListener 接口,接口定义参见下方附录;
2. 注入状态监听实现类,~/WEB-INF/webcat/application.xml,示例:

<?xml version="1.0" encoding="utf-8"?>
<webcatee version="1">
    <!-- 允许注册多个监听器。 -->
    <!-- 默认的监听器在用户提交工单时会向审核人发送邮件。 -->
    <!--
    com.skin.webcat.api.order.DefaultOrderEventListener 在 2.4.6 版本中已废弃,即使有配置也不会再加载。
    低版本中建议删除该配置,请基于 WorkOrderEventListener 接口自行实现。
    -->

    <listener class="com.skin.webcat.api.order.DefaultOrderEventListener"/>
</webcatee>

# 注:
1. 所有的监听器都在异步线程中执行。
2. 监听器是单例的,实现方需保证线程安全。

WebcatEE 提供的监听器,可根据情况使用


# 仅监听工单提交事件,向审核人发送审核邮件通知
<listener class="com.skin.webcat.api.order.DefaultOrderEventListener"/>

# 监听并处理所有事件, 调用本地应用程序,需要指定本地应用程序的绝对路径和工作目录
<listener class="com.skin.webcat.api.order.action.DefaultOrderChangeAction">
    <parameter name="work">D:\mytest</parameter>
    <parameter name="command">D:\mytest\bin\mytest.bat</parameter>
</listener>

当有工单事件时执行:
D:\mytest\bin\mytest.bat ORDER_ID EVENT_TYPE

例如:
D:\mytest\bin\mytest.bat 100 SUBMIT

工单事件分为五种:


# 参见 OrderEvent.SUBMIT
SUBMIT      新工单提交
CANCEL      工单被取消
APPROVED    工单被审核通过
REJECTED    工单被审核拒绝
EXECUTED    工单执行完成

附录


public interface WorkOrderEventListener {
    /**
     * @param orderEvent
     */
    void fire(OrderEvent orderEvent);
}


public class OrderEvent {
    public static final int SUBMIT   = 1;
    public static final int CANCEL   = 2;
    public static final int APPROVED = 3;
    public static final int REJECTED = 4;
    public static final int EXECUTED = 5;

    private int eventType;
    private User user;
    private Object source;

    // getter & setter
    ...
}

示例


/**
 * AbstractOrderEventListener 类将各种事件和工单区分
 * 继承该类只需实现具体的事件即可
 */
public abstract class AbstractOrderEventListener implements WorkOrderEventListener {
    private static final Logger logger = LoggerFactory.getLogger(AbstractOrderEventListener.class);

    /**
     * @param user
     * @param workOrder
     */
    public void submit(User user, DatabaseOrder workOrder) {
        logger.info("order.submit: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void submit(User user, TableOrder workOrder) {
        logger.info("order.submit: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void submit(User user, BaseChangeOrder workOrder) {
        logger.info("order.submit: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void submit(User user, DataChangeOrder workOrder) {
        logger.info("order.submit: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void submit(User user, DataExportOrder workOrder) {
        logger.info("order.submit: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void cancel(User user, DatabaseOrder workOrder) {
        logger.info("order.cancel: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void cancel(User user, TableOrder workOrder) {
        logger.info("order.cancel: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void cancel(User user, BaseChangeOrder workOrder) {
        logger.info("order.cancel: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void cancel(User user, DataChangeOrder workOrder) {
        logger.info("order.cancel: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void cancel(User user, DataExportOrder workOrder) {
        logger.info("order.cancel: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void approve(User user, DatabaseOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void approve(User user, TableOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void approve(User user, BaseChangeOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void approve(User user, DataChangeOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void approve(User user, DataExportOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void reject(User user, DatabaseOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void reject(User user, TableOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void reject(User user, BaseChangeOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void reject(User user, DataChangeOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void reject(User user, DataExportOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void execute(User user, DatabaseOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void execute(User user, TableOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void execute(User user, BaseChangeOrder workOrder) {
        logger.info("order.approve: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void execute(User user, DataChangeOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }

    /**
     * @param user
     * @param workOrder
     */
    public void execute(User user, DataExportOrder workOrder) {
        logger.info("order.reject: {}, {}", user.getUserId(), workOrder.getId());
    }
}

/**
 * 默认的实现,当工单提交时向审核人发送审核邮件
 */
public DefaultOrderEventListener extends AbstractOrderEventListener {
    /**
     * 当工单提交时触发
     * @param user
     * @param workOrder
     */
    @Override
    public void submit(User user, DatabaseOrder workOrder) {
        // 获取工单审核人
        List<User> auditors = WorkOrderService.getAuditors(workOrder.getDatabaseId());

        // 向审核人依次发送邮件通知
        for(...) {
            address += ...
        }
        MailService.send(address, "标题", "邮件内容");
    }

    // 其他方法
    ...
}