Search code examples
javaactiviticamunda

REST Service Task


I am following an example REST Service Task

I start my process engine using

val configuration = new StandaloneProcessEngineConfiguration(); configuration.setProcessEngineName(processEngineName)

Here is my bpmn file snippet

<process id="approve-loan" name="Loan Approval" isExecutable="true">
<serviceTask id="process_task" activiti:class="com.noggin.bpm.loan.ProcessRequestDelegate" activiti:exclusive="true" name="compute&#xA;Task">
  <extensionElements>
    <activiti:connector>
      <activiti:connectorId>http-connector</activiti:connectorId>
      <activiti:inputOutput>
        <activiti:inputParameter name="url">http://127.0.0.1:5004/Hello/sayhello</activiti:inputParameter>
        <activiti:inputParameter name="method">POST</activiti:inputParameter>
        <activiti:inputParameter name="headers">
          <activiti:map>
            <activiti:entry key="Accept">application/json</activiti:entry>
            <activiti:entry key="Content-type">application/json</activiti:entry>
          </activiti:map>
        </activiti:inputParameter>
        <activiti:inputParameter name="payload"><![CDATA[{"bundleId":"101","script":"def greet = {\n        \"Hello World\"\n      }\n      greet()"}]]></activiti:inputParameter>
        <activiti:outputParameter name="isActive">Result</activiti:outputParameter>
      </activiti:inputOutput>
    </activiti:connector>
  </extensionElements>

I start the process like this

    val processEngine   =  ProcessEngines.getProcessEngine(processEngineName)
    val runtime         =  processEngine.getRuntimeService
    val processInstance =  runtime.startProcessInstanceByKey(processInstanceKey)

Successfully, I am able to send the payload to ( http://127.0.0.1:5004/Hello/sayhello ).

My question is how to retrieve the response message from the location i started the instance. Since the response will be in a Json message which should be sent back to process initiator.


Solution

  • I believe I saw a similar question from you posted to the Camunda forum yesterday. Either way, I believe the question and answer is the same.

    Let me make sure I understand what you are asking. 1. You are starting the instance using the Java API 2. Your process definition includes a single Service Task that makes a REST call. 3. Your JavaDelegate class populates the "Result" process variable with the response of the REST call. 4. You want to capture the response.

    If I have captured your requirement, then I think the problem is in your understanding of how he BPMN engine works.

    With the process as you have it modeled, the process instance will start, make the REST call, populate the Response variable and then immediately end.

    As you have currently modeled the process, you will not be able to capture the response during process execution.

    Your options: 1. Change your model to either send the "Result" using a message service of some sort, or add a wait state where you can retrieve the response. 2. Use the Historical query REST API (or the equivalent Java API) to retrieve the Result payload from the completed instance.

    It really depends on your use case as to the most appropriate option to take.

    Cheers, Greg