de.otto.synapse.message.Message Maven / Gradle / Ivy
Show all versions of synapse-core Show documentation
package de.otto.synapse.message;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Objects;
import static de.otto.synapse.message.Header.of;
/**
* A Message is an atomic packet of data that can be transmitted on a channel.
*
*
*
*
*
* Thus to transmit data, an application must break the data into one or more packets,
* wrap each packet as a message, and then send the message on a channel. Likewise, a receiver
* application receives a message and must extract the data from the message to process it.
*
*
* The message system will try repeatedly to deliver the message (e.g., transmit it from the
* sender to the receiver) until it succeeds.
*
*
* @param The type of the Message payload
* @see EIP: Message
*/
public class Message
implements Serializable {
private static final long serialVersionUID = -2723145950340871252L;
public static
Message
message(final @Nonnull Key key,
final @Nullable P payload) {
return new Message<>(key, of(), payload);
}
public static
Message
message(final @Nonnull Key key,
final @Nonnull Header header,
final @Nullable P payload) {
return new Message<>(key, header, payload);
}
public static
Message
message(final @Nonnull String key,
final @Nullable P payload) {
return new Message<>(Key.of(key), of(), payload);
}
public static
Message
message(final @Nonnull String key,
final @Nonnull Header header,
final @Nullable P payload) {
return new Message<>(Key.of(key), header, payload);
}
private final Key key;
private final Header header;
private final P payload;
protected Message(final @Nonnull Key key,
final @Nonnull Header header,
final @Nullable P payload) {
this.key = key;
this.payload = payload;
this.header = header;
}
@Nonnull
public Key getKey() {
return key;
}
@Nullable
public P getPayload() {
return payload;
}
@Nonnull
public Header getHeader() {
return header;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Message)) return false;
Message> message = (Message>) o;
return key.equals(message.key) &&
header.equals(message.header) &&
Objects.equals(payload, message.payload);
}
@Override
public int hashCode() {
return Objects.hash(key, header, payload);
}
@Override
public String toString() {
return "Message{" +
"of='" + key + '\'' +
", payload=" + payload +
", header=" + header +
'}';
}
public static
Builder
builder(final Class
payloadType) {
return new Builder<>();
}
public static
Builder
copyOf(final Message
message) {
return new Builder
()
.withKey(message.getKey())
.withHeader(message.getHeader())
.withPayload(message.getPayload());
}
public static class Builder
{
private Key key = Key.of();
private Header header;
private P payload;
public Builder
withKey(final String key) {
this.key = Key.of(key);
return this;
}
public Builder
withKey(final Key key) {
this.key = key;
return this;
}
public Builder
withHeader(final Header header) {
this.header = header;
return this;
}
public Builder
withPayload(final P payload) {
this.payload = payload;
return this;
}
public Message
build() {
return new Message<>(key, header, payload);
}
}
}