0

I am trying to build a custom connector using Mule's Java SDK. Basically what I am trying to do is to add functionality to the current cache scope to let the developers to be able to add a condition upon which the scope should store something in the cache based on the final result (and some other features too, but this is what I am mainly stuck at). For example only store a database query response if the result is not empty. To do this I am thinking to pass a function as an argument in the operation which will return a Boolean and on the basis of the result the operation will store the result in the cache. The final function definition of the operation would look something like this

public void cache(String key, Function(or something else?) storeInCache, Chain connectors, CompletionCallback<Object, Object> callback) {}

Is it possible to do what I am trying to do. I am open to any suggestions. May be there is any alternative way to do this?

Thanks

Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22

1 Answers1

0

I'm not sure that you can 'extend' a built-in component like the cache scope. Maybe you can just use an expression for that argument. If that doesn't work for your use case, other methods could be either use a dynamic configuration or alternatively define storeInCache to be a ParameterResolver<> to defer the execution of an expression.

aled
  • 21,330
  • 3
  • 27
  • 34
  • I can not use expression for that argument because that will be calculated before calling the java method, and will pass the final value, what I am looking is that it accepts the expression, but it executes at the very end of the flow, and on the basis of that I can decide if I store the result in cache or not. – Harshank Bansal Nov 20 '20 at 15:35
  • Did you check the documentation for ParameterResolver? "ParameterResolver allows you to defer the resolution of an expression on an operation. This is useful when a parameter that takes an expression as a value is not required. You can improve performance by resolving such an expression on demand from within the connectors’s code instead of resolving it automatically." – aled Nov 20 '20 at 16:00
  • Yes, I am trying that out. I mostly want to check if it will execute on the new Payload or on the previous payload only. But yes, it looks something that could work. – Harshank Bansal Nov 21 '20 at 02:38
  • I tried it, it gives the ability to execute the expression but it still executes on the payload which was before entering the scope – Harshank Bansal Nov 21 '20 at 17:19
  • I'm sorry I'm not sure I understand what is the new payload here. Perhaps you can expand in the question adding some code example and clarifications. – aled Nov 22 '20 at 14:20
  • By new payload I mean the response from DB call, on which I want to check if it is empty then not store in cache. But the ParamenterResolver is getting executed on the payload which was invoked prior to the cache scope. – Harshank Bansal Nov 24 '20 at 07:53