Is it possible to create in a Mule 4 Module a Scope with access to a connection?

125 Views Asked by At

I'm following the examples included in the documentation related to the creation of a Module and its different components. I've been able to create Operations that can use connections, but now I'm trying to do something similar with a Scope instead.

What I've tried is adding @Connection MyConnection connection as one on the arguments of the methods in my module as seen below.

    public void logDecorator(@Connection MyConnection connection, Chain operations,
        CompletionCallback<Object, Object> callback) {
            logger.debug("Invoking child operations");
            operations.process(
                result -> {
                    logger.debug("Done: {}", result.getOutput());
                    callback.success(result);
                },
                (error, previous) -> {
                    logger.error(error.getMessage());
                    callback.error(error);
                });
        }

But when I build the module I get the error that this is not allowed.

Error executing: org.mule.runtime.extension.api.exception.IllegalOperationModelDefinitionException: Scope 'logDecorator' requires a connection, but that is not allowed, remove such parameter -> [Help 1]

Is there a way that I can add a reference to a connections that would allow me to use it inside logDecorator?

2

There are 2 best solutions below

4
aled On BEST ANSWER

Scopes can not receive a connection as explained in the documentation:

Scopes have some restrictions that differentiate them from Operations. By definition, Scopes are not allowed to depend on or receive a particular Configuration or Connection.

4
Jonathan On
public void logDecorator(Chain operations, CompletionCallback<Object, Object> callback) {
  logger.debug("Invoking child operations");

  // Get the connection that is defined for the scope
  MyConnection connection = getConnection();

  operations.process(
    result -> {
      logger.debug("Done: {}", result.getOutput());
      callback.success(result);
    },
    (error, previous) -> {
      logger.error(error.getMessage());
      callback.error(error);
    });
}

How about something like this?