The base implementation of the Message
interface is
GenericMessage<T>
, and it provides two constructors:
new GenericMessage<T>(T payload); new GenericMessage<T>(T payload, Map<String, Object> headers)
When a Message is created, a random unique id will be generated. The constructor that accepts a Map of headers will copy the provided headers to the newly created Message.
There are also two convenient subclasses available: StringMessage
and
ErrorMessage
. The former accepts a String as its payload:
StringMessage message = new StringMessage("hello world"); String s = message.getPayload();
And, the latter accepts any Throwable
object as its payload:
ErrorMessage message = new ErrorMessage(someThrowable);
Throwable t = message.getPayload();
Notice that these implementations take advantage of the fact that the GenericMessage
base class is parameterized. Therefore, as shown in both examples, no casting is necessary when retrieving
the Message payload Object.