Sending Message from External Task to Engine (via specific method)

Hello team (@Niall and @jonathan.lukas )

According to this great post, sending a managed message will be important for the end-user client that need to work with Rest API.

Actually, we need a module for sending any kind of message to the Engine (from External Task) and it seems we could prepare it according to your suggestion ( use Rest API for POST /message).

we have developed another method related to sending status variables.

It’s our pleasure if we could prepare this method too (if you think its necessary for External Task Client)

The module could be like the below code (in ExternalTaskService.java interface).

  /**
   * Send Message
   *
   * @param message     which message string that you need to send to engine
   * @param correlationKeys indicates the master keys for correlation.
   * @param processVariables provides related variables to the process.
   * @param all      specifies which params should be send (all or not).
   *
   * @throws NotFoundException if the task has been canceled and therefore does not exist anymore
   * @throws NotAcquiredException if the task's most recent lock could not be acquired
   * @throws ConnectionLostException if the connection could not be established
   * @throws ValueMapperException
   * <ul>
   *   <li> if an object cannot be serialized
   *   <li> if no 'objectTypeName' is provided for non-null value
   *   <li> if value is of type abstract
   *   <li> if no suitable serializer could be found
   * </ul>
   */
  void sendMessage(ExternalTask externalTask, String message, VariableMap correlationKeys, VariableMap processVariables, Boolean all);

Let me know your feedback and suggestion.

Best Regards,

Taha Arian

1 Like

Hi tahaarian

We need this feature for send message to starting a engine process. This is useful for our requirement.

Thanks for suggestion!

Hello @tahaarian ,

thank you for opening a new Thread. For a precise implementation, you could have a look at this Dto from the Rest API:

org.camunda.bpm.engine.rest.dto.message.CorrelationMessageDto

This is the Dto containing the request data. The response is this Dto:

org.camunda.bpm.engine.rest.dto.message.MessageCorrelationResultWithVariableDto

I am missing the localCorrelationKeys as well as the processVariablesLocal, the boolean resultEnabled and the boolean variablesInResultEnabled for the request.

In my opinion, by overloading the method with different parameter combinations or by using a builder pattern, this could become a very cool feature.

Jonathan

1 Like

Hello @jonathan.lukas

Thank you for your great suggestion. I will check it out and will notify you the result.

Best Regards,
Taha Arian

1 Like

Hello again @jonathan.lukas

Thank you for your great point.

As we checked your suggestion out, it’s not necessary to modify(or override) Engine Rest (MessageCorrelationResultWithVariableDto). We could send our message with two VariableMap(for example processVariables and correlationKeys) easily. So just we need a new API method.

I have developed below source code on External Task project.
I have attached each modification separately

EngineClient.java

//   public static final String SEND_MESSAGE =  "/message";

  public void sendMessage(ExternalTask externalTask, String message, VariableMap correlationKeys, VariableMap processVariables, Boolean all) throws EngineClientException {
    SendMessageRequestDto payload = new SendMessageRequestDto(externalTask, message, correlationKeys, processVariables, all);
    String resourceUrl = baseUrl + SEND_MESSAGE;
    engineInteraction.postRequest(resourceUrl, payload, Void.class, xsrf);
  }

ExternalTaskServiceImpl.java

  @Override
  public void sendMessage(ExternalTask externalTask, String message, VariableMap correlationKeys, VariableMap processVariables, Boolean all) {
    try {
      engineClient.sendMessage(externalTask, message, correlationKeys, processVariables, all);
    } catch (EngineClientException e) {
      throw handleException(e, "send message", true);
    }
  }

ExternalTaskService.java (interface)

  /**
   * Send Message
   *
   * @param message     which message string that you need to send to engine
   * @param correlationKeys indicates the master keys for correlation.
   * @param processVariables provides related variables to the process.
   * @param all      specifies which params should be send (all or not).
   *
   * @throws NotFoundException if the task has been canceled and therefore does not exist anymore
   * @throws NotAcquiredException if the task's most recent lock could not be acquired
   * @throws ConnectionLostException if the connection could not be established
   * @throws ValueMapperException
   * <ul>
   *   <li> if an object cannot be serialized
   *   <li> if no 'objectTypeName' is provided for non-null value
   *   <li> if value is of type abstract
   *   <li> if no suitable serializer could be found
   * </ul>
   */
  void sendMessage(ExternalTask externalTask, String message, VariableMap correlationKeys, VariableMap processVariables, Boolean all);

In the end, I prepared a sample test and catch a successful response from the Engine.


   public static void main(String[] args) {

        // Personal Test
        ExternalTaskClient client = ExternalTaskClient.create()
                .baseUrl("http://10.1.2.51:8080/engine-rest")
                .asyncResponseTimeout(10000) // long polling timeout
                .build();

        client.subscribe("charge-card")
                .lockDuration(1000)
                .handler((externalTask, externalTaskService) -> {
                    VariableMap correlationKeys = new VariableMapImpl();
                    correlationKeys.putValueTyped("aVariable", Variables.stringValue("aValue"));

                   VariableMap processVariables = new VariableMapImpl();
                   processVariables.putValueTyped("aVariable", Variables.stringValue("aNewValue", true));
                   processVariables.putValueTyped("anotherVariable", Variables.booleanValue(true, true));

                   externalTaskService.sendMessage(externalTask, "aMessage", correlationKeys, processVariables, null);
                   externalTaskService.complete(externalTask);
                })
                .open();
    }

We could send our message (for example aMessage) with correlation and process variables from the External Task to the Engine successfully.

It’s our pleasure if we could contribute to the External Task project.

Let me know your feedback

Best Regards,

Taha Arian

Hello @tahaarian ,

this looks quite nice. @tasso94 , do you have any comments on this?

Jonathan

1 Like

Hello @tahaarian ,

after a short chat with Tasso, he told me that the most efficient way to get this to create a pull request in Github to where you forked from. Here, our Dev team will then take over :wink:

Jonathan

1 Like

Hello again @jonathan.lukas

Thank you for your quick response.
Ok great :slight_smile:, I will prepare a pull request and notify Mr. Tasso for reviewing.

Best Regards,
Taha Arian