Add method to send message to Camunda engine 50/102050/4
authorHarry Huang <huangxiangyu5@huawei.com>
Thu, 20 Feb 2020 07:37:56 +0000 (15:37 +0800)
committerHarry Huang <huangxiangyu5@huawei.com>
Fri, 21 Feb 2020 02:56:13 +0000 (10:56 +0800)
Issue-ID: SO-2368

Modify CamundaRequestHandler to add method to send
message to Camunda engine. Use non-blocking webclient
considering workflow may take long time to finish.

Change-Id: I70a8b38881ac40b4d65a3d7305f57a56829e6f1d
Signed-off-by: Harry Huang <huangxiangyu5@huawei.com>
mso-api-handlers/mso-api-handler-infra/pom.xml
mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/CamundaRequestHandler.java

index 84a80e6..9944984 100644 (file)
       <groupId>org.springframework.retry</groupId>
       <artifactId>spring-retry</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-webflux</artifactId>
+    </dependency>
     <dependency>
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
index 17377d8..f30b66c 100644 (file)
@@ -9,9 +9,11 @@ import javax.ws.rs.core.UriBuilder;
 import javax.xml.bind.DatatypeConverter;
 import org.camunda.bpm.engine.impl.persistence.entity.HistoricActivityInstanceEntity;
 import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
+import org.json.JSONObject;
 import org.onap.logging.filter.spring.SpringClientPayloadFilter;
 import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter;
 import org.onap.so.utils.CryptoUtils;
+import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -29,6 +31,9 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.client.ResourceAccessException;
 import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Flux;
 
 @Component
 public class CamundaRequestHandler {
@@ -202,6 +207,20 @@ public class CamundaRequestHandler {
         return retryTemplate;
     }
 
+    protected void sendCamundaMessages(JSONObject msgJson) {
+        String url = env.getProperty("mso.camundaURL") + "/sobpmnengine/message";
+        HttpHeaders headers =
+                setCamundaHeaders(env.getRequiredProperty("mso.camundaAuth"), env.getRequiredProperty("mso.msoKey"));
+        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
+        // Workflow may take a long time so use non-blocking request
+        Flux<String> flux = WebClient.create().post().uri(url).headers(httpHeaders -> {
+            httpHeaders.set(httpHeaders.AUTHORIZATION, headers.get(httpHeaders.AUTHORIZATION).get(0));
+            httpHeaders.set(httpHeaders.ACCEPT, headers.get(httpHeaders.ACCEPT).get(0));
+            httpHeaders.set(httpHeaders.CONTENT_TYPE, headers.get(httpHeaders.CONTENT_TYPE).get(0));
+        }).body(BodyInserters.fromObject(msgJson.toString())).retrieve().bodyToFlux(String.class);
+        flux.subscribe(res -> logger.debug("Send Camunda Message: " + res));
+    }
+
     protected RestTemplate getRestTemplate(boolean retry) {
         int timeout;
         if (retry) {