java.lang.AutoCloseable
public class WebhookCluster
extends java.lang.Object
implements java.lang.AutoCloseable
WebhookClients
which allows to broadcast (broadcast(WebhookMessage)
) or multicast (multicast(Predicate, WebhookMessage)
)
to all registered clients (receivers).
addWebhooks(WebhookClient...)
or addWebhooks(Collection<WebhookClient>)
.
WebhookClient.close()
.
Note that you should always remove a WebhookClient from this cluster when you close it directly!
removeWebhooks(WebhookClient...)
, removeWebhooks(Collection<WebhookClient>)
and removeIf(Predicate<WebhookClient>)
.
It does not matter if the specified clients are actually registered to this cluster when using these methods.
Note that removing a WebhookClient from the cluster does not close it!
WebhookClientBuilder
when building via buildWebhooks(Webhook...)
and buildWebhooks(Collection<Webhook>)
.
setDefaultExecutorService(ScheduledExecutorService)
setDefaultHttpClientBuilder(OkHttpClient.Builder)
setDefaultHttpClient(OkHttpClient)
setDefaultThreadFactory(ThreadFactory)
setDefaultDaemon(boolean)
Note that when you provide your own ScheduledExecutorService
you are able to shut it down
outside of the clients which will cause them to fail.
Do not shutdown the pool before closing all clients!
Multicasting will send a message to all clients which meet a set filter.
The filter is specified using a Predicate
which is provided to multicast(Predicate, WebhookMessage)
.
The predicate decides whether the client should receive the message (returning true) or should be ignored (returning false).
WebhookClient
is a Closable
resource which means
it must be closed to free resources and enhance performance of the JVM.
close()
.
Calling close on the cluster means it will remove and close all currently registered webhooks.
Constructor | Description |
---|---|
WebhookCluster() |
Creates a new WebhookCluster with default initial capacity
and no registered
WebhookClients |
WebhookCluster(int initialCapacity) |
Creates a new WebhookCluster with the specified initial capacity.
|
WebhookCluster(java.util.Collection<? extends WebhookClient> initialClients) |
Creates a new WebhookCluster with the provided
WebhookClients as initial client cache. |
Modifier and Type | Method | Description |
---|---|---|
WebhookCluster |
addWebhooks(java.util.Collection<WebhookClient> clients) |
Adds the specified
WebhookClients
to this cluster's list of receivers. |
WebhookCluster |
addWebhooks(WebhookClient... clients) |
Adds the specified
WebhookClients
to this cluster's list of receivers. |
java.util.List<RequestFuture<?>> |
broadcast(byte[] data,
java.lang.String fileName) |
Sends the provided
byte[] data as an attachment
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(java.io.File file) |
Sends the provided
File
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(java.io.File file,
java.lang.String fileName) |
Sends the provided
File
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(java.io.InputStream data,
java.lang.String fileName) |
Sends the provided
InputStream as an attachment
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(java.lang.String content) |
Sends the provided text message
to all registered
WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(java.util.Collection<MessageEmbed> embeds) |
Sends the provided
MessageEmbeds
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(Message message) |
Sends the provided
Message
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(MessageEmbed[] embeds) |
Sends the provided
MessageEmbeds
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(MessageEmbed first,
MessageEmbed... embeds) |
Sends the provided
MessageEmbeds
to all registered WebhookClients . |
java.util.List<RequestFuture<?>> |
broadcast(WebhookMessage message) |
Sends the provided
WebhookMessage
to all registered WebhookClients . |
WebhookCluster |
buildWebhook(long id,
java.lang.String token) |
Creates new
WebhookClients and adds them
to this cluster. |
WebhookCluster |
buildWebhooks(java.util.Collection<Webhook> webhooks) |
Creates new
WebhookClients and adds them
to this cluster. |
WebhookCluster |
buildWebhooks(Webhook... webhooks) |
Creates new
WebhookClients and adds them
to this cluster. |
void |
close() |
Closes all registered
WebhookClients
and removes the from this cluster! |
java.util.List<WebhookClient> |
closeIf(java.util.function.Predicate<WebhookClient> predicate) |
Closes all
WebhookClients that meet
the specified filter. |
java.util.List<WebhookClient> |
getWebhooks() |
The current list of receivers for this WebhookCluster instance.
|
java.util.List<RequestFuture<?>> |
multicast(java.util.function.Predicate<WebhookClient> filter,
WebhookMessage message) |
Sends the provided
WebhookMessage
to all WebhookClients that meet the specified
filter. |
WebhookClientBuilder |
newBuilder(long id,
java.lang.String token) |
Creates a new
WebhookClientBuilder
with the defined default settings of this cluster. |
WebhookClientBuilder |
newBuilder(Webhook webhook) |
Creates a new
WebhookClientBuilder
with the defined default settings of this cluster. |
java.util.List<WebhookClient> |
removeIf(java.util.function.Predicate<WebhookClient> predicate) |
Removes the specified
WebhookClients
from this cluster's list of receivers under the conditions of the provided filter. |
WebhookCluster |
removeWebhooks(java.util.Collection<WebhookClient> clients) |
Removes the specified
WebhookClients
from this cluster's list of receivers. |
WebhookCluster |
removeWebhooks(WebhookClient... clients) |
Removes the specified
WebhookClients
from this cluster's list of receivers. |
WebhookCluster |
setDefaultDaemon(boolean isDaemon) |
Whether rate limit threads of created
WebhookClients
should be treated as Thread.isDaemon() or not. |
WebhookCluster |
setDefaultExecutorService(java.util.concurrent.ScheduledExecutorService executorService) |
Sets the default
ScheduledExecutorService that should be
used when building WebhookClients via
buildWebhooks(Webhook...) or buildWebhooks(Collection) . |
WebhookCluster |
setDefaultHttpClient(okhttp3.OkHttpClient defaultHttpClient) |
Sets the default
OkHttpClient that should be
used when building WebhookClients via
buildWebhooks(Webhook...) or buildWebhooks(Collection) . |
WebhookCluster |
setDefaultHttpClientBuilder(okhttp3.OkHttpClient.Builder builder) |
Sets the default
OkHttpClient.Builder that should be
used when building WebhookClients via
buildWebhooks(Webhook...) or buildWebhooks(Collection) . |
WebhookCluster |
setDefaultThreadFactory(java.util.concurrent.ThreadFactory factory) |
Factory that should be used by the default
ScheduledExecutorService
to create Threads for rate limitation handling of the created WebhookClient !
This allows changing thread information such as name without having to create your own executor. |
public WebhookCluster(@Nonnull java.util.Collection<? extends WebhookClient> initialClients)
WebhookClients
as initial client cache.initialClients
- Collection of WebhookClients that should be addedjava.lang.IllegalArgumentException
- If any of the provided clients is null
or closedpublic WebhookCluster(int initialCapacity)
ArrayList
.initialCapacity
- The initial capacity for this clusterjava.lang.IllegalArgumentException
- If the provided capacity is negativepublic WebhookCluster()
WebhookClients
public WebhookCluster setDefaultHttpClientBuilder(@Nullable okhttp3.OkHttpClient.Builder builder)
OkHttpClient.Builder
that should be
used when building WebhookClients
via
buildWebhooks(Webhook...)
or buildWebhooks(Collection)
.builder
- The default builder, null
to resetpublic WebhookCluster setDefaultHttpClient(@Nullable okhttp3.OkHttpClient defaultHttpClient)
OkHttpClient
that should be
used when building WebhookClients
via
buildWebhooks(Webhook...)
or buildWebhooks(Collection)
.defaultHttpClient
- The default client, null
to resetpublic WebhookCluster setDefaultExecutorService(@Nullable java.util.concurrent.ScheduledExecutorService executorService)
ScheduledExecutorService
that should be
used when building WebhookClients
via
buildWebhooks(Webhook...)
or buildWebhooks(Collection)
.executorService
- The default executor service, null
to resetpublic WebhookCluster setDefaultThreadFactory(@Nullable java.util.concurrent.ThreadFactory factory)
ScheduledExecutorService
to create Threads for rate limitation handling of the created WebhookClient
!
factory
- The ThreadFactory
that will
be used when no ScheduledExecutorService
has been set via setDefaultExecutorService(ScheduledExecutorService)
public WebhookCluster setDefaultDaemon(boolean isDaemon)
WebhookClients
should be treated as Thread.isDaemon()
or not.
This will not be used when the default thread pool has been set via setDefaultExecutorService(ScheduledExecutorService)
!
isDaemon
- True, if the threads should be daemonpublic WebhookCluster buildWebhooks(Webhook... webhooks)
WebhookClients
and adds them
to this cluster.
WebhookClientBuilders
will be supplied with the default settings of this cluster.webhooks
- Webhooks to target (duplicates will not be filtered)java.lang.IllegalArgumentException
- If the provided array or any of the contained
webhooks is null
buildWebhooks(Webhook...)
,
newBuilder(Webhook)
public WebhookCluster buildWebhooks(java.util.Collection<Webhook> webhooks)
WebhookClients
and adds them
to this cluster.
WebhookClientBuilders
will be supplied with the default settings of this cluster.webhooks
- Webhooks to target (duplicates will not be filtered)java.lang.IllegalArgumentException
- If the provided collection or any of the contained
webhooks is null
buildWebhooks(Webhook...)
,
newBuilder(Webhook)
public WebhookCluster buildWebhook(long id, java.lang.String token)
WebhookClients
and adds them
to this cluster.
WebhookClientBuilders
will be supplied with the default settings of this cluster.id
- The id for the webhooktoken
- The token for the webhookjava.lang.IllegalArgumentException
- If the provided webhooks token is null
or contains whitespacenewBuilder(long, String)
public WebhookClientBuilder newBuilder(long id, java.lang.String token)
WebhookClientBuilder
with the defined default settings of this cluster.id
- The webhook idtoken
- The webhook tokenjava.lang.IllegalArgumentException
- If the token is null
, empty or contains blanksnew WebhookClientBuilder(long, String)
public WebhookClientBuilder newBuilder(Webhook webhook)
WebhookClientBuilder
with the defined default settings of this cluster.webhook
- The target webhookjava.lang.IllegalArgumentException
- If the webhook is null
new WebhookClientBuilder(Webhook)
public WebhookCluster addWebhooks(WebhookClient... clients)
WebhookClients
to this cluster's list of receivers.
clients
- WebhookClients to addjava.lang.IllegalArgumentException
- If the provided array or any of the contained
clients is null
or closedpublic WebhookCluster addWebhooks(java.util.Collection<WebhookClient> clients)
WebhookClients
to this cluster's list of receivers.
clients
- WebhookClients to addjava.lang.IllegalArgumentException
- If the provided collection or any of the contained
clients is null
or closedpublic WebhookCluster removeWebhooks(WebhookClient... clients)
WebhookClients
from this cluster's list of receivers.
Note that the removed clients are not closed by this operation!
clients
- WebhookClients to removejava.lang.IllegalArgumentException
- If the provided array is null
public WebhookCluster removeWebhooks(java.util.Collection<WebhookClient> clients)
WebhookClients
from this cluster's list of receivers.
Note that the removed clients are not closed by this operation!
clients
- WebhookClients to removejava.lang.IllegalArgumentException
- If the provided collection is null
public java.util.List<WebhookClient> removeIf(java.util.function.Predicate<WebhookClient> predicate)
WebhookClients
from this cluster's list of receivers under the conditions of the provided filter.
true
to remove provided clients and false
to retain them.
Note that the removed clients are not closed by this operation!
predicate
- The filterjava.lang.IllegalArgumentException
- If the provided filter is null
public java.util.List<WebhookClient> closeIf(java.util.function.Predicate<WebhookClient> predicate)
WebhookClients
that meet
the specified filter.
true
for all clients that should be removed and closed.predicate
- The filter to decide which clients to removejava.lang.IllegalArgumentException
- If the provided filter is null
public java.util.List<WebhookClient> getWebhooks()
WebhookClients
.public java.util.List<RequestFuture<?>> multicast(java.util.function.Predicate<WebhookClient> filter, WebhookMessage message)
WebhookMessage
to all WebhookClients
that meet the specified
filter.
true
for all clients that should receive the message.
Hint: Use WebhookMessageBuilder
to
create a WebhookMessage
instance!
filter
- The filter that decides what clients receive the messagemessage
- The message that should be sent to the filtered clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If any of the provided arguments is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(WebhookMessage message)
WebhookMessage
to all registered WebhookClients
.
Hint: Use WebhookMessageBuilder
to
create a WebhookMessage
instance!
message
- The message that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If any of the provided arguments is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(Message message)
Message
to all registered WebhookClients
.
Hint: Use MessageBuilder
to
create a Message
instance!
message
- The message that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided message is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(MessageEmbed[] embeds)
MessageEmbeds
to all registered WebhookClients
.
You can send up to 10 embeds per message! If more are sent they will not be displayed.
Hint: Use EmbedBuilder
to
create a MessageEmbeds
instance!
embeds
- The embeds that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If any of the provided arguments is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(MessageEmbed first, MessageEmbed... embeds)
MessageEmbeds
to all registered WebhookClients
.
You can send up to 10 embeds per message! If more are sent they will not be displayed.
Hint: Use EmbedBuilder
to
create a MessageEmbeds
instance!
first
- The first embed to send to the clientsembeds
- The other embeds that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If any of the provided arguments is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(java.util.Collection<MessageEmbed> embeds)
MessageEmbeds
to all registered WebhookClients
.
You can send up to 10 embeds per message! If more are sent they will not be displayed.
Hint: Use EmbedBuilder
to
create a MessageEmbeds
instance!
embeds
- The embeds that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If any of the provided arguments is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(java.lang.String content)
WebhookClients
.content
- The text that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided content is null
or blankjava.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(java.io.File file)
File
to all registered WebhookClients
.
WebhookMessage.files(String, Object, Object...)
to send up to 10 files!
The provided data should not exceed 8MB in size!
file
- The file that should be sent to the clientsFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided file is null
, does not exist or ist not readablejava.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(java.io.File file, java.lang.String fileName)
File
to all registered WebhookClients
.
WebhookMessage.files(String, Object, Object...)
to send up to 10 files!
The provided data should not exceed 8MB in size!
file
- The file that should be sent to the clientsfileName
- The name that should be given to the fileFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided file is null
, does not exist or ist not readablejava.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(java.io.InputStream data, java.lang.String fileName)
InputStream
as an attachment
to all registered WebhookClients
.
WebhookMessage.files(String, Object, Object...)
to send up to 10 files!
The provided data should not exceed 8MB in size!
data
- The data that should be sent to the clientsfileName
- The name that should be given to the attachmentFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided data is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic java.util.List<RequestFuture<?>> broadcast(byte[] data, java.lang.String fileName)
byte[]
data as an attachment
to all registered WebhookClients
.
WebhookMessage.files(String, Object, Object...)
to send up to 10 files!
The provided data should not exceed 8MB in size!
data
- The data that should be sent to the clientsfileName
- The name that should be given to the attachmentFuture
instances
representing all message tasks.java.lang.IllegalArgumentException
- If the provided data is null
java.util.concurrent.RejectedExecutionException
- If any of the receivers has been shutdownpublic void close()
WebhookClients
and removes the from this cluster!close
in interface java.lang.AutoCloseable