Interface Request<T>

  • Type Parameters:
    T - Message type.

    public interface Request<T>
    Request operation.

    This interface represents a request/response operation of a MessagingChannel.

    Usage Example

    Typical use of this interface is:

    1. Obtain an instance of this interface via the MessagingChannel.newRequest(Object) method call
    2. Set options (if needed):
    3. Execute this operation via the submit() method
    4. Process the response (synchronously or asynchronously)

    
    MessagingChannel<String> channel = hekate.messaging().channel("example.channel", String.class);
    
    RequestFuture<String> future = channel.newRequest("some-message") // Some dummy message.
        .withTimeout(3, TimeUnit.SECONDS) // Timeout.
        .withAffinity("100500") // Some dummy affinity key.
        .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.
            .alwaysReRoute() // Retry on different nodes.
        )
        .submit(); // Asynchronously execute the operation.
    
    // Await and print the response.
    System.out.println("Response: " + future.result());
    

    Shortcut Methods

    MessagingChannel interface provides a set of synchronous and asynchronous shortcut methods for common use cases:

    • Method Detail

      • withAffinity

        Request<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).

        LoadBalancer can also make use of the affinity key in order to perform consistent routing of messages among the cluster node. For example, the default load balancer makes sure that all messages, having the same key, are always routed to the same node (unless the cluster topology doesn't change).

        Parameters:
        affinity - Affinity key.
        Returns:
        This instance.
      • withTimeout

        Request<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)
      • submit

        RequestFuture<T> submit()
        Asynchronously executes this operation.
        Returns:
        Future result of this operation.
      • submit

        default void submit​(RequestCallback<T> callback)
        Asynchronously executes this operation and notifies the specified callback upon completion.
        Parameters:
        callback - Callback.
      • response

        default T response()
        Synchronously executes this operation and returns the response's payload.
        Returns:
        Response's payload (see MessageBase.payload()).
        Throws:
        HekateException - if operations fails.
      • response

        default <R extends T> R response​(Class<R> responseType)
                                  throws HekateException
        Synchronously executes this operation and returns the response.
        Type Parameters:
        R - Response type.
        Parameters:
        responseType - Response type.
        Returns:
        Response.
        Throws:
        HekateException - if operations fails.