Package club.minnced.discord.webhook
Class WebhookCluster
- java.lang.Object
-
- club.minnced.discord.webhook.WebhookCluster
-
- All Implemented Interfaces:
AutoCloseable
public class WebhookCluster extends Object implements AutoCloseable
Collection of webhooks, useful for subscriber pattern.
Register several webhooks and broadcast to all of them with a single call.Webhook created by the cluster through
buildWebhook(long, String)
are initialized with defaults specified by
-
-
Constructor Summary
Constructors Constructor Description WebhookCluster()
Default initializes a new WebhookCluster.WebhookCluster(int initialCapacity)
Creates a webhook cluster with the specified capacity.WebhookCluster(@NotNull Collection<? extends WebhookClient> initialClients)
Creates a new WebhookCluster with the provided clients
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull WebhookCluster
addWebhooks(@NotNull WebhookClient... clients)
Adds the provided webhooks to the cluster.@NotNull WebhookCluster
addWebhooks(@NotNull Collection<WebhookClient> clients)
Adds the provided webhooks to the cluster.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull WebhookEmbed first, @NotNull WebhookEmbed... embeds)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull WebhookMessage message)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull File file)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull String content)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull String fileName, @org.jetbrains.annotations.NotNull byte[] data)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull String fileName, @NotNull File file)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull String fileName, @NotNull InputStream data)
Sends a message to all registered clients.@NotNull List<CompletableFuture<ReadonlyMessage>>
broadcast(@NotNull Collection<WebhookEmbed> embeds)
Sends a message to all registered clients.@NotNull WebhookCluster
buildWebhook(long id, @NotNull String token)
Builds aWebhookClient
instance with the provided components and specified default configurations.void
close()
Performs cascade closing on current webhook clients, all clients will be closed and removed after this returns.@NotNull List<WebhookClient>
closeIf(@NotNull Predicate<WebhookClient> predicate)
Closes and removes webhook clients based on the specified filter.@NotNull List<WebhookClient>
getWebhooks()
Unmodifiable list of currently registered clients@NotNull List<CompletableFuture<ReadonlyMessage>>
multicast(@NotNull Predicate<WebhookClient> filter, @NotNull WebhookMessage message)
Sends a message to a filtered set of clients.@NotNull WebhookClientBuilder
newBuilder(long id, @NotNull String token)
Creates aWebhookClientBuilder
instance with the provided components and specified default configurations.@NotNull List<WebhookClient>
removeIf(@NotNull Predicate<WebhookClient> predicate)
Removes webhooks from the cluster based on the specified filter.@NotNull WebhookCluster
removeWebhooks(@NotNull WebhookClient... clients)
Removes the provided webhooks from the cluster.@NotNull WebhookCluster
removeWebhooks(@NotNull Collection<WebhookClient> clients)
Removes the provided webhooks from the cluster.@NotNull WebhookCluster
setAllowedMentions(@Nullable AllowedMentions allowedMentions)
The mention whitelist to use by default for every webhook client.@NotNull WebhookCluster
setDefaultDaemon(boolean isDaemon)
Configures whetherWebhookClient
instances should be daemon by default.@NotNull WebhookCluster
setDefaultExecutorService(@Nullable ScheduledExecutorService executorService)
Configures the default executor service that will be used to buildWebhookClient
instances.@NotNull WebhookCluster
setDefaultHttpClient(@Nullable OkHttpClient defaultHttpClient)
Configures the default http client that will be used to buildWebhookClient
instances.@NotNull WebhookCluster
setDefaultThreadFactory(@Nullable ThreadFactory factory)
Configures the default thread factory that will be used to buildWebhookClient
instances.
-
-
-
Constructor Detail
-
WebhookCluster
public WebhookCluster(@NotNull @NotNull Collection<? extends WebhookClient> initialClients)
Creates a new WebhookCluster with the provided clients- Parameters:
initialClients
- Clients to add to the cluster- Throws:
NullPointerException
- If provided with null
-
WebhookCluster
public WebhookCluster(int initialCapacity)
Creates a webhook cluster with the specified capacity.
Note that this capacity can be expanded dynamically by building/adding more clients.- Parameters:
initialCapacity
- The initial capacity- Throws:
IllegalArgumentException
- If the capacity is illegal- See Also:
ArrayList(int)
-
WebhookCluster
public WebhookCluster()
Default initializes a new WebhookCluster.
This cluster will be empty.
-
-
Method Detail
-
setDefaultHttpClient
@NotNull public @NotNull WebhookCluster setDefaultHttpClient(@Nullable @Nullable OkHttpClient defaultHttpClient)
Configures the default http client that will be used to buildWebhookClient
instances.- Parameters:
defaultHttpClient
- The default http client- Returns:
- WebhookCluster instance for chaining convenience
- See Also:
WebhookClientBuilder.setHttpClient(okhttp3.OkHttpClient)
-
setDefaultExecutorService
@NotNull public @NotNull WebhookCluster setDefaultExecutorService(@Nullable @Nullable ScheduledExecutorService executorService)
Configures the default executor service that will be used to buildWebhookClient
instances.- Parameters:
executorService
- The default executor service- Returns:
- WebhookCluster instance for chaining convenience
- See Also:
WebhookClientBuilder.setExecutorService(java.util.concurrent.ScheduledExecutorService)
-
setDefaultThreadFactory
@NotNull public @NotNull WebhookCluster setDefaultThreadFactory(@Nullable @Nullable ThreadFactory factory)
Configures the default thread factory that will be used to buildWebhookClient
instances.- Parameters:
factory
- The default thread factory- Returns:
- WebhookCluster instance for chaining convenience
- See Also:
WebhookClientBuilder.setThreadFactory(java.util.concurrent.ThreadFactory)
-
setAllowedMentions
@NotNull public @NotNull WebhookCluster setAllowedMentions(@Nullable @Nullable AllowedMentions allowedMentions)
The mention whitelist to use by default for every webhook client.
SeeAllowedMentions
for more details.- Parameters:
allowedMentions
- The default mention whitelist- Returns:
- WebhookCluster instance for chaining convenience
-
setDefaultDaemon
@NotNull public @NotNull WebhookCluster setDefaultDaemon(boolean isDaemon)
Configures whetherWebhookClient
instances should be daemon by default.- Parameters:
isDaemon
- True, if clients should be daemon- Returns:
- WebhookCluster instance for chaining convenience
- See Also:
WebhookClientBuilder.setDaemon(boolean)
-
buildWebhook
@NotNull public @NotNull WebhookCluster buildWebhook(long id, @NotNull @NotNull String token)
Builds aWebhookClient
instance with the provided components and specified default configurations.- Parameters:
id
- The id of the webhooktoken
- The token of the webhook- Returns:
- WebhookCluster instance for chaining convenience
- Throws:
NullPointerException
- If the token is null
-
newBuilder
@NotNull public @NotNull WebhookClientBuilder newBuilder(long id, @NotNull @NotNull String token)
Creates aWebhookClientBuilder
instance with the provided components and specified default configurations.
The webhook client must be explicitly added to the cluster after building.- Parameters:
id
- The id of the webhooktoken
- The token of the webhook- Returns:
- WebhookClientBuilder instance
- Throws:
NullPointerException
- If the token is null
-
addWebhooks
@NotNull public @NotNull WebhookCluster addWebhooks(@NotNull @NotNull WebhookClient... clients)
Adds the provided webhooks to the cluster.- Parameters:
clients
- The clients to add- Returns:
- WebhookCluster instance for chaining convenience
- Throws:
NullPointerException
- If provided with nullIllegalArgumentException
- If at least one of the clients is already shutdown
-
addWebhooks
@NotNull public @NotNull WebhookCluster addWebhooks(@NotNull @NotNull Collection<WebhookClient> clients)
Adds the provided webhooks to the cluster.- Parameters:
clients
- The clients to add- Returns:
- WebhookCluster instance for chaining convenience
- Throws:
NullPointerException
- If provided with nullIllegalArgumentException
- If at least one of the clients is already shutdown
-
removeWebhooks
@NotNull public @NotNull WebhookCluster removeWebhooks(@NotNull @NotNull WebhookClient... clients)
Removes the provided webhooks from the cluster.- Parameters:
clients
- The clients to remove- Returns:
- WebhookCluster instance for chaining convenience
- Throws:
NullPointerException
- If provided with null
-
removeWebhooks
@NotNull public @NotNull WebhookCluster removeWebhooks(@NotNull @NotNull Collection<WebhookClient> clients)
Removes the provided webhooks from the cluster.- Parameters:
clients
- The clients to remove- Returns:
- WebhookCluster instance for chaining convenience
- Throws:
NullPointerException
- If provided with null
-
removeIf
@NotNull public @NotNull List<WebhookClient> removeIf(@NotNull @NotNull Predicate<WebhookClient> predicate)
Removes webhooks from the cluster based on the specified filter.- Parameters:
predicate
- The filter to decide whether to remove a client or not- Returns:
List
of the removed webhooks- Throws:
NullPointerException
- If provided with null
-
closeIf
@NotNull public @NotNull List<WebhookClient> closeIf(@NotNull @NotNull Predicate<WebhookClient> predicate)
Closes and removes webhook clients based on the specified filter.- Parameters:
predicate
- The filter to decide whether to close and remove the client- Returns:
List
of the removed webhooks- Throws:
NullPointerException
- If provided with null
-
getWebhooks
@NotNull public @NotNull List<WebhookClient> getWebhooks()
Unmodifiable list of currently registered clients- Returns:
- List of clients
-
multicast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> multicast(@NotNull @NotNull Predicate<WebhookClient> filter, @NotNull @NotNull WebhookMessage message)
Sends a message to a filtered set of clients.This will override the default
AllowedMentions
of the client!- Parameters:
filter
- The filter to decide whether a client should be targetedmessage
- The message to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull WebhookMessage message)
Sends a message to all registered clients.This will override the default
AllowedMentions
of the client!- Parameters:
message
- The message to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull WebhookEmbed first, @NotNull @NotNull WebhookEmbed... embeds)
Sends a message to all registered clients.- Parameters:
first
- The first embed to sendembeds
- Optional additional embeds to send, up to 10- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull Collection<WebhookEmbed> embeds)
Sends a message to all registered clients.- Parameters:
embeds
- The embeds to send, up to 10- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull String content)
Sends a message to all registered clients.- Parameters:
content
- The message to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull File file)
Sends a message to all registered clients.- Parameters:
file
- The file to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with nullUncheckedIOException
- If an I/O error occurs
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull String fileName, @NotNull @NotNull File file)
Sends a message to all registered clients.- Parameters:
fileName
- The alternative file name to usefile
- The file to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with nullUncheckedIOException
- If an I/O error occurs
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull String fileName, @NotNull @NotNull InputStream data)
Sends a message to all registered clients.- Parameters:
fileName
- The alternative file name to usedata
- The data to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with nullUncheckedIOException
- If an I/O error occurs
-
broadcast
@NotNull public @NotNull List<CompletableFuture<ReadonlyMessage>> broadcast(@NotNull @NotNull String fileName, @NotNull @org.jetbrains.annotations.NotNull byte[] data)
Sends a message to all registered clients.- Parameters:
fileName
- The alternative file name to usedata
- The data to send- Returns:
- List of futures for each client execution
- Throws:
NullPointerException
- If provided with null
-
close
public void close()
Performs cascade closing on current webhook clients, all clients will be closed and removed after this returns.
The cluster may still be used after calls to this method occurred.- Specified by:
close
in interfaceAutoCloseable
-
-