Interface Aggregate<T>
-
- Type Parameters:
T
- Message type.
public interface Aggregate<T>
Aggregate operation.This interface represents a request/response-style broadcast operation of a
MessagingChannel
. This operation submits the same request message to multiple remote nodes and aggregates their responses.Usage Example
Typical use of this interface is:
- Obtain an instance of this interface via the
MessagingChannel.newAggregate(Object)
method call - Set options (if needed):
- Execute this operation via the
submit()
method - Process results (synchronously or asynchronously)
MessagingChannel<String> channel = hekate.messaging().channel("example.channel", String.class); AggregateFuture<String> future = channel.newAggregate("some-message") // Some dummy message. .withAffinity("100500") // Some dummy affinity key for consistent routing. .withTimeout(3, TimeUnit.SECONDS) // Timeout. .withRetry(retry -> retry .whileError(err -> err.isCausedBy(IOException.class)) // Only if I/O error. .withFixedDelay(100) // Delay between retries. .maxAttempts(3) // Retry up to 3 times. ) .submit(); // Asynchronously execute the operation. // Await and print results. System.out.println("Results: " + future.results());
Shortcut Methods
MessagingChannel
interface provides a set of synchronous and asynchronous shortcut methods for common use cases:
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default AggregateResult<T>
get()
Synchronously executes this operation and returns the result.default Collection<T>
results()
Synchronously executes this operation and returns the result.AggregateFuture<T>
submit()
Asynchronously executes this operation.default void
submit(AggregateCallback<T> callback)
Asynchronously executes this operation and notifies the specified callback upon completion.Aggregate<T>
withAffinity(Object affinity)
Affinity key.Aggregate<T>
withRepeat(AggregateRepeatCondition<T> condition)
Condition to repeat for the wholeAggregate
operation.Aggregate<T>
withRetry(AggregateRetryConfigurer<T> retry)
Retry policy for each individual message of the aggregation operation.Aggregate<T>
withTimeout(long timeout, TimeUnit unit)
Overrides the channel's default timeout value for this operation.
-
-
-
Method Detail
-
withAffinity
Aggregate<T> withAffinity(Object affinity)
Affinity key.Specifying an affinity key ensures that all operation with the same key will always be transmitted over the same network connection and will always be processed by the same thread (if the cluster topology doesn't change).
- Parameters:
affinity
- Affinity key.- Returns:
- This instance.
-
withTimeout
Aggregate<T> withTimeout(long timeout, TimeUnit unit)
Overrides the channel's default timeout value for this operation.If this operation can not complete at the specified timeout then this operation will end up in a
MessageTimeoutException
.Specifying a negative or zero value disables the timeout check.
- Parameters:
timeout
- Timeout.unit
- Unit.- Returns:
- This instance.
- See Also:
MessagingChannelConfig.setMessagingTimeout(long)
-
withRetry
Aggregate<T> withRetry(AggregateRetryConfigurer<T> retry)
Retry policy for each individual message of the aggregation operation.This policy gets applied to each individual (per-node) message. For example, if aggregation should be performed over 3 nodes and an error happens while sending an aggregation message to the second node then this retry policy will be applied only to that particular failed message.
If retry logic should be implemented at the aggregation level (i.e. decide on whether to retry or not by looking at the whole
AggregateResult
) please see thewithRepeat(AggregateRepeatCondition)
method.- Parameters:
retry
- Retry policy.- Returns:
- This instance.
- See Also:
MessagingChannelConfig.setRetryPolicy(GenericRetryConfigurer)
,withRepeat(AggregateRepeatCondition)
-
withRepeat
Aggregate<T> withRepeat(AggregateRepeatCondition<T> condition)
Condition to repeat for the wholeAggregate
operation.If the specified condition evaluates to
true
then the wholeAggregate
operation will be repeated from scratch, except for the following cases:Aggregate
operation timed out (seewithTimeout(long, TimeUnit)
)MessagingChannel
's cluster topology is empty (i.e. no nodes to perform aggregation)Hekate
node is stopped
If one of the above is true then
Aggregate
will complete with whateverAggregateResult
it has.- Parameters:
condition
- Condition.- Returns:
- This instance.
- See Also:
withRetry(AggregateRetryConfigurer)
-
submit
AggregateFuture<T> submit()
Asynchronously executes this operation.- Returns:
- Future result of this operation.
-
submit
default void submit(AggregateCallback<T> callback)
Asynchronously executes this operation and notifies the specified callback upon completion.- Parameters:
callback
- Callback.
-
results
default Collection<T> results() throws HekateException
Synchronously executes this operation and returns the result.- Returns:
- Result (see
AggregateResult.results()
). - Throws:
HekateException
- If operations fails.
-
get
default AggregateResult<T> get() throws HekateException
Synchronously executes this operation and returns the result.- Returns:
- Result.
- Throws:
HekateException
- If operations fails.
-
-