Record Class ChannelMessage
- Record Components:
sendSync- whether sending the message should block the current thread until the message is flushed.prioritized- whether this channel message should be handled with priority over other channel messages.channel- the channel to which the channel message gets sent. Mostly for identification reasons.message- the message key of this channel message. Mostly for identification reasons.content- the content of this channel message (the actual data to send).sender- the sender of the channel message. Should be, but must not the current network component.targets- the targets to which the channel message should get send.
- All Implemented Interfaces:
AutoCloseable
A channel message has two main identification points. One is the channel to which the message gets sent. The channel is a string generally used to group channel messages together. This is, for example, useful to identify all channel messages that are sent by a specific module. Further narrowing of the message type is done by using the message key, which should uniquely identify the specific message. Each channel message must be composed of a unique channel and message to distinguish it from other messages being sent in the cluster.
The message contains a DataBuf containing the actual content of the message. There is no real way to identify
which types are in the buffer or not, therefore, it is crucial that a channel message gets identified via its channel
and message keys.
If targets were given that are not locatable in the network, they will get ignored silently.
Note: there is no guarantee that the sender of a channel message is the actual component sending the message, as the message can be modified on its way to the receiver.
The actual constructor of this class shouldn't get used. Use builder() instead.
- Since:
- 4.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder for a channel message. -
Field Summary
FieldsModifier and TypeFieldDescriptionThe field for thechannelrecord component.The field for thecontentrecord component.The field for themessagerecord component.private final booleanThe field for theprioritizedrecord component.private final @NonNull ChannelMessageSenderThe field for thesenderrecord component.private final booleanThe field for thesendSyncrecord component.private final @NonNull Collection<ChannelMessageTarget> The field for thetargetsrecord component. -
Constructor Summary
ConstructorsConstructorDescriptionChannelMessage(boolean sendSync, boolean prioritized, @NonNull String channel, @NonNull String message, @NonNull DataBuf content, @NonNull ChannelMessageSender sender, @NonNull Collection<ChannelMessageTarget> targets) Creates an instance of aChannelMessagerecord class. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull ChannelMessage.Builderbuilder()Constructs a new, empty builder for a ChannelMessage.static @NonNull ChannelMessage.BuilderConstructs a new builder which contains all necessary data to respond to a channel message.channel()Returns the value of thechannelrecord component.voidclose()content()Returns the value of thecontentrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.message()Returns the value of themessagerecord component.private @NonNull CloudMessengerUtil method to get the current messenger of the environment.booleanReturns the value of theprioritizedrecord component.voidsend()Sends this channel message using the current messenger of the environment.sender()Returns the value of thesenderrecord component.Sends this channel message as a query and blocks until all target components have responded to the query or the timeout ofCloudMessenger.SYNC_CHANNEL_MESSAGE_QUERY_TIMEOUT_MSis exceeded.Sends this channel message as a query and returns a future which waits for target component(s) to respond.Sends this channel message as a query and blocks until one of the target component responded to this message or the timeout ofCloudMessenger.SYNC_CHANNEL_MESSAGE_QUERY_TIMEOUT_MSis exceeded.Sends this channel message as a query and returns a future which waits for target component(s) to respond.booleansendSync()Returns the value of thesendSyncrecord component.targets()Returns the value of thetargetsrecord component.final StringtoString()Returns a string representation of this record class.
-
Field Details
-
sendSync
private final boolean sendSyncThe field for thesendSyncrecord component. -
prioritized
private final boolean prioritizedThe field for theprioritizedrecord component. -
channel
-
message
-
-
sender
The field for thesenderrecord component. -
targets
The field for thetargetsrecord component.
-
-
Constructor Details
-
ChannelMessage
public ChannelMessage(boolean sendSync, boolean prioritized, @NonNull @NonNull String channel, @NonNull @NonNull String message, @NonNull @NonNull DataBuf content, @NonNull @NonNull ChannelMessageSender sender, @NonNull @NonNull Collection<ChannelMessageTarget> targets) Creates an instance of aChannelMessagerecord class.- Parameters:
sendSync- the value for thesendSyncrecord componentprioritized- the value for theprioritizedrecord componentchannel- the value for thechannelrecord componentmessage- the value for themessagerecord componentcontent- the value for thecontentrecord componentsender- the value for thesenderrecord componenttargets- the value for thetargetsrecord component
-
-
Method Details
-
builder
Constructs a new, empty builder for a ChannelMessage.- Returns:
- a new, empty builder for a ChannelMessage.
-
buildResponseFor
@Contract("_ -> new") @NonNull public static @NonNull ChannelMessage.Builder buildResponseFor(@NonNull @NonNull ChannelMessage input) Constructs a new builder which contains all necessary data to respond to a channel message. As the channel message will get directly handled by the waiting future, there is no need to actually set the channel and message of the returned builder. The new builder will target the sender of the given input and has no data set.- Parameters:
input- the channel message to respond to.- Returns:
- a new builder for a channel message holding all base information to respond to the given source message.
- Throws:
NullPointerException- if the given input is null.
-
send
public void send()Sends this channel message using the current messenger of the environment. This is a shortcut method forCloudMessenger.sendChannelMessage(ChannelMessage). This method will not wait for the target component to respond (it doesn't even expect a response) but for the handling component to send the message.Note: once the channel message was sent, the backing buffer gets released. Therefore, the caller must acquire the content buffer if this channel message is sent multiple times.
-
sendQueryAsync
Sends this channel message as a query and returns a future which waits for target component(s) to respond. This method is a shortcut forCloudMessenger.sendChannelMessageQueryAsync(ChannelMessage). The future will be completed when the target component responds or the query future times out.Note: it is not possible for CloudNet to detect when a channel message query response was consumed. Therefore, it is crucial that the caller closes the responses to prevent memory leaks. Example:
ChannelMessage message = ...; message.sendQueryAsync().thenAccept(responses -> { for (var response : responses) { try (response) { // do something with the response } } }- Returns:
- a future completed with all responses of all components targeted by this channel message.
-
sendSingleQueryAsync
Sends this channel message as a query and returns a future which waits for target component(s) to respond. Only the first response of any target will get sent back to this component. This is in particular useful if there is only one target, or you are only expecting one of the target components to respond. This is a shortcut method forCloudMessenger.sendSingleChannelMessageQueryAsync(ChannelMessage). The future will be completed with the first received response of any target component (possibly null if no target responded).Note: it is not possible for CloudNet to detect when a channel message query response was consumed. Therefore, it is crucial that the caller closes the response to prevent memory leaks. Example:
ChannelMessage message = ...; message.sendSingleQueryAsync().thenAccept(response -> { if (response != null) { try (response) { // do something with the response } } }- Returns:
- a future completed with the first received response of any target component or null if no target responded.
-
sendQuery
Sends this channel message as a query and blocks until all target components have responded to the query or the timeout ofCloudMessenger.SYNC_CHANNEL_MESSAGE_QUERY_TIMEOUT_MSis exceeded. If more control over the timeout is required, an async method with a custom timeout applied must be used instead. This method is a shortcut forCloudMessenger.sendChannelMessageQuery(ChannelMessage).Note: it is not possible for CloudNet to detect when a channel message query response was consumed. Therefore, it is crucial that the caller closes the responses to prevent memory leaks. Example:
ChannelMessage message = ...; Collection<ChannelMessage> responses = message.sendQuery(); for (var response : responses) { try (response) { // do something with the response } }- Returns:
- all responses of all components this channel message is targeting.
- Throws:
CompletionException- if an exception occurred while waiting for the query responses.
-
sendSingleQuery
Sends this channel message as a query and blocks until one of the target component responded to this message or the timeout ofCloudMessenger.SYNC_CHANNEL_MESSAGE_QUERY_TIMEOUT_MSis exceeded. If more control over the timeout is required, an async method with a custom timeout applied must be used instead. This is in particular useful if there is only one target, or you are only expecting one of the target components to respond. This is a shortcut method forCloudMessenger.sendSingleChannelMessageQuery(ChannelMessage).Note: it is not possible for CloudNet to detect when a channel message query response was consumed. Therefore, it is crucial that the caller closes the response to prevent memory leaks. Example:
ChannelMessage message = ...; ChannelMessage response = message.sendSingleQuery(); if (response != null) { try (response) { // do something with the response } }- Returns:
- the first response of any component this message is targeting, null if no target responded.
- Throws:
CompletionException- if an exception occurred while waiting for the query response.
-
messenger
Util method to get the current messenger of the environment.- Returns:
- the current messenger of the environment.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
sendSync
-
prioritized
public boolean prioritized()Returns the value of theprioritizedrecord component.- Returns:
- the value of the
prioritizedrecord component
-
channel
-
message
-
content
-
sender
-
targets
-