1

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?

Berigoner
  • 93
  • 1
  • 7
  • Why do you need a connection inside a log decorator? – aled Dec 15 '22 at 12:45
  • This was just a simple example to test the functionality. The intent is to have a Scope that based on information retrieves from a connection (and input parameters) decides if it needs to execute the inner operations or just generate a Result directly. Something similar could be done with regular operations, Choice, etc, but I was looking to be able to encapsulate it in a single component for easy reuse across applications. – Berigoner Dec 15 '22 at 13:53
  • Looks like you are trying to implement a circuit breaker. Better make it independent of the connection anyway and pass only simple parameters to the scope. – aled Dec 15 '22 at 13:56

2 Answers2

0
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?

Jonathan
  • 2,318
  • 7
  • 25
  • 44
0

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.

aled
  • 21,330
  • 3
  • 27
  • 34
  • Thank you @aled. Hopefully this is something that is for future releases for additional flexibility. – Berigoner Dec 15 '22 at 13:54
  • It seems very intentional in the design. I don't know if this will change in the future. – aled Dec 15 '22 at 13:56
  • Some out of box scopes uses configuration, for example with `cache` scope we can pass object store configuration. Not sure why would they not allow that in custom scopes – Harshank Bansal Dec 21 '22 at 07:54
  • Core components are developed as part of the runtime and are not a module developed with the Mule SDK. Hence they don't have the same limitations. – aled Dec 21 '22 at 12:17