The Result object represents the result of a component’s execution. It is used for cases in which the component not only needs to return a value to be set on the message payload but also will specify message attributes and/or a mime type.
The class has two generic types, one for the type of the value that goes into the payload and another for the type of the returned message attributes.
For example, consider an oversimplified read operation from the File connector. This operation sets the payload to an InputStream with the file’s content. It also sets the attributes to a FileAttributes object that contains information, such as file name, size, timestamp, permissions, and so on.
public Result<InputStream, FileAttributes> read(String path) { (1)
InputStream content = getContent(path);
FileAttributes attributes = getAttributes(path);
return Result.<InputStream, FileAttributes>builder() (2)
.output(content) (3)
.attributes(attributes) (4)
.build(); (5)
}
| 1 |
The operation returns a Result<InputStream, FileAttributes>. This tells the runtime that the operation returns an InputStream as payload and FileAttributes as attributes. |
| 2 |
Once it obtains the result values, it uses Result.builder() to create a new instance. Note that this is strongly typed, so you need to specify the generics when creating the builder. |
| 3 |
The output(Object) method in the builder is used to set the payload. |
| 4 |
The attributes(Object) is used to set the attributes. |
| 5 |
The example calls the build() method and returns the created Result. |