Kindly check out this module
https://github.com/mulesoft/mule-requester-module
It can request a resource at any point in a flow. It's for resources originally only requested by message sources.
use cases are:
- Load a file in the middle of a flow
- Consume messages (one, N, all) from a queue in the middle of a flow.
- Pull messages from a mail server on demand, to use its data in an enricher for example.
Its a simple devkit wrapper around the typical muleContext.getClient() call
public Object request(String resource, @Optional @Default("1000") long timeout, @Optional String returnClass, @Optional Boolean throwExceptionOnTimeout) throws MuleException {
MuleMessage message = muleContext.getClient().request(resource, timeout);
Object result = null;
if (message != null)
{
result = message.getPayload();
if (returnClass != null)
{
try {
Transformer transformer = muleContext.getRegistry().lookupTransformer(DataTypeFactory.create(result.getClass()), DataTypeFactory.create(Class.forName(returnClass)));
result = transformer.transform(result);
} catch (ClassNotFoundException e) {
throw new DefaultMuleException(e);
}
}
} else if (Boolean.TRUE.equals(throwExceptionOnTimeout))
{
throw new DefaultMuleException("No message received in the configured timeout - " + timeout);
}
return result;
}