<http:listener ... /> <!-- Obtains an InputStream payload -->
<object-to-string/> <!-- consume the stream and convert it to a String -->
<logger message="#[payload]"/> <!-- log the String -->
<http:request .../> <!-- send the String somewhere else-->
Introduction to Mule 4: Transformations and Streaming
The way to think about data in Mule 4 is fundamentally different in Mule 3. In Mule 3, you often need to know about the underlying Java types, how to manage InputStreams, whether a payload is larger-than-memory, and how to convert data into Java objects so they can be accessed by MEL.
For Mule 4, these best practices are recommended:
-
Avoid unnecessary conversions to Java data types, and work with data directly.
-
Let the runtime handle streaming for you.
-
Do not convert binary data types to
String
,byte[]
, orInputStream
unless you are performing a direct integration with custom Java code.
Transparent Streaming
Introduction to Mule 4: DataWeave Expression Language showed how to work with data directly using DataWeave. This ability to access different data formats, combined with Mule 4 streaming capabilities, means that you no longer need to convert your data to Java objects first. This improvement greatly simplifies working with data in the runtime because:
-
Data can be read multiple times or accessed randomly using the DataWeave expression language, without side effects.
-
Data can be sent to multiple places without the need to cache that data in memory first.
-
You can transparently access larger-than-memory data.
For more information on how streaming works in Mule 4, please see About Streaming in Mule 4.0.
Data Types and Object to String/Byte/InputStream Transformers
Because Mule now manages the data streams for you, data access becomes simpler. You do not need to be concerned with the underlying Java representation of that type whether it is a byte[]
, InputStream
, String
, or some other format.
A common pattern observed in Mule 3 applications is that users would convert things to strings in order to log them or send them through some transport. This was necessary because streams cannot be consumed twice, so a Transformer was needed:
In Mule 4, you can simply log or send the data without worrying about the underlying type or how many times the stream is consumed. Instead of thinking about the InputStream
, you should just think about the payload as binary data. Or, if you know the content type, you can think about it directly as JSON, XML,
or whatever data type corresponds to the content type.
<http:listener ... /> <!-- payload is JSON document -->
<logger message="#[payload]"/> <!-- log the JSON document -->
<http:request .../> <!-- send the JSON document somewhere else-->