flowable任务监听器 事件监听器

任务监听器:针对UserTask节点
事件监听器:针对任意节点
使用场景:实际情况下可能会在节点开始或结束之后调用业务系统的接口去做同步数据之类的业务处理。


一、定义任务监听器

@Scope
@Component(value = "taskBusinessCallListener")
public class TaskBusinessCallListener extends BusinessCallListener implements TaskListener {
    /**
     * rest接口
     */
    private FixedValue restUrl;
    /**
     * 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
     */
    private FixedValue params;

    @Override
    public void notify(DelegateTask delegateTask) {
        String processInstanceId = delegateTask.getProcessInstanceId();
        String restUrlStr = null, paramsStr = null;
        if (restUrl != null) {
            restUrlStr = restUrl.getExpressionText();
        }
        if (params != null) {
            paramsStr = params.getExpressionText();
        }
        //执行回调
        restUrlStr = "http://localhost:8501/hznet-gn/floodDispatch/syncDispatch";
        paramsStr = "status:1";
        this.callBack(processInstanceId, restUrlStr, paramsStr);
    }
}

二、接口调用

public void callBack(String pocessInstanceId, String restUrl, String params) {
        String paramsJson = null;
        try {
            //Map<String, Object> paramMap = new HashMap<String, Object>();
            //获取变量
            Map<String, Object> paramMap = runtimeService.getVariables(pocessInstanceId);
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pocessInstanceId).singleResult();
            paramMap.put("businessKey", processInstance.getBusinessKey());
            this.setParams(params, paramMap);
            paramsJson = JSON.toJSONString(paramMap);
            //设置超时时间,否则会报超时错误
            HttpComponentsClientHttpRequestFactory requestFactory=new HttpComponentsClientHttpRequestFactory();
            requestFactory.setReadTimeout(30000);
            requestFactory.setConnectionRequestTimeout(30000);
            requestFactory.setConnectTimeout(30000);
            restTemplate=new RestTemplate(requestFactory);
            //调用业务方法
            logger.info("开始调用业务系统接口" + restUrl + ",业务参数:" + paramsJson);
            restTemplate.postForObject(restUrl, paramsJson, String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

三、配置监听

流程定义xml中的delegateExpression与监听类对应!

flowable任务监听器 事件监听器

上一篇:@Bean使用


下一篇:Eclipse中配置weka,以及添加算法