很久没有写博客,承蒙诸位流程爱好者的关心和爱护,鄙人才疏学浅,但尽管如此,我也是依然让自己为社会做点贡献,帮助那些需要帮助的人能节约时间和少走弯路,这也是我的初衷。自立立人,自达达人。
flowable提供了在线配置监听器的形式,然而如何能把监听器配置用到最好,还是需要多思考,同时也需要储备一些技术知识点,在实际场景中,流程执行过程或结束的时候需要调用业务系统接口,怎么样才能一次配置多处使用呢,这也是我们今天要讲的话题。
动态配置监听器,一处配置多处使用
目标:尽可能少硬编码编写监听器,让程序更加健壮,减轻程序员的负担
- 学会动态配置
- 学会动态调用
@Component(value = "executionBusinessCallListener")
public class ExecutionBusinessCallListener implements ExecutionListener {
/**
* nacos的服务id
*/
private FixedValue serviceId;
/**
* 访问的url路径
*/
private FixedValue url;
/**
* 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
*/
private FixedValue params;
@Autowired
private IDynamicCallbackService dynamicCallbackService;
@Override
public void notify(DelegateExecution execution) {
String processInstanceId = execution.getProcessInstanceId();
String businessKey = execution.getProcessInstanceBusinessKey();
String serviceIdStr = null, urlStr = null, paramsStr = null;
if (serviceId != null) {
serviceIdStr = serviceId.getExpressionText();
}
if (url != null) {
urlStr = url.getExpressionText();
}
if (params != null) {
paramsStr = params.getExpressionText();
}
Map<String,String> params = new HashMap<>();
params.put("processInstanceId", processInstanceId);
params.put("params", paramsStr);
params.put("businessKey", businessKey);
dynamicCallbackService.postCallback(serviceIdStr, urlStr, params);
}
}
public class DynamicCallbackServiceImpl implements IDynamicCallbackService {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Override
public JSONObject postCallback(String serviceId, String url, Map<String, String> params) {
return this.callback(serviceId, url, HttpMethod.POST, params);
}
@Override
public JSONObject getCallback(String serviceId, String url, Map<String, String> params) {
return this.callback(serviceId, url, HttpMethod.GET, params);
}
@Override
public JSONObject callback(String serviceId, String url, HttpMethod method, Map<String, String> params) {
JSONObject forObject = null;
ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
if (serviceInstance != null) {
StringBuffer callbackurl = new StringBuffer("");
if (serviceInstance.isSecure()) {
callbackurl.append("https://");
} else {
callbackurl.append("http://");
}
callbackurl.append(serviceInstance.getHost()).append(":").append(serviceInstance.getPort());
if (url.substring(0, 1).equals("/")) {
callbackurl.append(url);
} else {
callbackurl.append("/").append(url);
}
if (HttpMethod.GET.equals(method)) {
forObject = restTemplate.getForObject(callbackurl.toString(), JSONObject.class, params);
} else if (HttpMethod.POST.equals(method)) {
HttpHeaders requestHeaders = getRequestHeaders();
HttpEntity<Map> requestEntity = new HttpEntity<Map>(params, requestHeaders);
forObject = restTemplate.postForObject(callbackurl.toString(), requestEntity, JSONObject.class);
}
}
return forObject;
}
private HttpHeaders getRequestHeaders(){
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
return requestHeaders;
}
}