bpmn signal

一、信號事件作用:

信號事件是一個在某階段一直處於監聽某個信號的作用,當接收到某個正在監聽的信號時,可執行某項操作

二、簡單使用場景:

現需要實現這樣一個場景:一個定時的操作,在到某個事件點是執行某個操做,但沒到這個時間點前,隨時可終止此流程,測試若要實現這個沒到某時間點前一直處於監聽停止操作的功能就需要用到信號事件(SignalCatchEvent)。要使未接收到信號前流程執行器一直處於監聽停止信號的狀態,需要用到事件網管Event Gateway),具體配置如下:

1.在流程上添加一個自定義的signals,name與id定義好


2.在流程需要區別到點操作的子流程與信號捕捉事件直接用事件網管講兩者分爲兩個不併行的分支,在信號捕捉控件的Mainconfig的signal ref中選中之前流程中添加好的signal信號的id

3.使用提供的restfulAPI調用:

private HttpClient getHttpClient() {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient = (DefaultHttpClient) WebClientDevWrapper.wrapClient(httpClient);
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials("admin", "admin")
        );
        return httpClient;
    }

    private HttpGet getSafeHttpGetObj(String fun_url) {
        HttpGet getRequest = new HttpGet(fun_url);
        getRequest.addHeader("Content-type", "application/json");
        //設置身份認證
        String auth = "admin" + ":" + "admin";
        String authorizationHeaderValue = java.util.Base64.getEncoder().encodeToString(auth.getBytes(Charset.forName("US-ASCII")));
        getRequest.addHeader("Authorization", "Basic " + authorizationHeaderValue);
        return getRequest;
    }

    private HttpPost getsafeHttpPostObj(String fun_url) {
        HttpPost getRequestPost = new HttpPost(fun_url);
        getRequestPost.addHeader("Content-type", "application/json");
        //設置身份認證
        String auth = "admin" + ":" + "admin";
        String authorizationHeaderValue = java.util.Base64.getEncoder().encodeToString(auth.getBytes(Charset.forName("US-ASCII")));
        getRequestPost.addHeader("Authorization", "Basic " + authorizationHeaderValue);
        return getRequestPost;
    }

    private HttpPut getsafeHttpPutObj(String fun_url) {
        HttpPut getRequestPut = new HttpPut(fun_url);
        getRequestPut.addHeader("Content-type", "application/json");
        //設置身份認證
        String auth = "admin" + ":" + "admin";
        String authorizationHeaderValue = java.util.Base64.getEncoder().encodeToString(auth.getBytes(Charset.forName("US-ASCII")));
        getRequestPut.addHeader("Authorization", "Basic " + authorizationHeaderValue);
        return getRequestPut;
    }
 public void testSendSignalToProcess() {
        String processInstanceId = "2575";
        String signalName = "stopSend";
        String executionId = "2581";
        try {
            String fun_Url = SEVER_URL + "/runtime/executions/" + executionId;
            HttpClient httpClient = getHttpClient();
            HttpPut requestHttpPut = getsafeHttpPutObj(fun_Url);

            Map<String, Object> processMap = new HashMap<String, Object>();
            processMap.put("signalName", signalName);
            processMap.put("action", "signalEventReceived");
            String headParamsJson = this.getGson().toJson(processMap);
            StringEntity entity = new StringEntity(headParamsJson, "UTF-8");
            entity.setContentType("application/json");
            requestHttpPut.setEntity(entity);
            HttpResponse response = httpClient.execute(requestHttpPut);
            int sta = response.getStatusLine().getStatusCode();//狀態碼
            String bodyStr = EntityUtils.toString(response.getEntity());//body
            if (sta < 200 || sta > 206) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + sta + ",requestUrl is :" + fun_Url);
            }
            System.out.println("=======" + sta);
            System.out.println("result=" + bodyStr);
        } catch (IOException ex) {
            Logger.getLogger(TestBPMNRestByCommonHttp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String formatParam(Map<String, Object> map, String procKey, String tenantId) {
        List<Object> variables = new ArrayList<Object>();
        for (Map.Entry entry : map.entrySet()) {
            Map<String, Object> variable = new HashMap<String, Object>();
            String key = entry.getKey().toString();
            String reKey = key;
            variable.put("name", reKey);
            variable.put("value", entry.getValue());
            variables.add(variable);
        }
        Map<String, Object> processMap = new HashMap<String, Object>();
        processMap.put("variables", variables);
        processMap.put("processDefinitionKey", procKey);
        processMap.put("tenantId", tenantId);
        String headParamsJson = this.getGson().toJson(processMap);
        return headParamsJson;
    }
    
    
    
    

    //runtime/process-instances
//    @Test
    public void testStartProcessBykey() {
        String processKey = "MessageCenterProcess_Msg";
        String tenantId = "-1234";
        String username = "sh000024";
        String pwd = "welcome123";
        String messageId = "d8f8ec6b-132e-41c1-a374-93210eec7898";
        String sendTime = "2016-09-24T16:50:00";
        String fun_urlString = SEVER_URL + "/runtime/process-instances";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", username);
        map.put("password", pwd);
        map.put("sendTime", sendTime);
        map.put("messageId", messageId);
        String headParamsJson = this.formatParam(map, processKey, tenantId);
        System.out.print("=======" + headParamsJson);
        HttpClient httpClient = getHttpClient();
        HttpPost httpPost = getsafeHttpPostObj(fun_urlString);
        try {
            StringEntity entity = new StringEntity(headParamsJson, "UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            int sta = response.getStatusLine().getStatusCode();//狀態碼
            String bodyStr = EntityUtils.toString(response.getEntity());//body
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 206) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }
            System.out.println("testStart====" + sta);
            System.out.println("result=" + bodyStr);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(TestBPMNRestByCommonHttp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestBPMNRestByCommonHttp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章