All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rabbitmq.client.amqp.impl.RpcSupport Maven / Gradle / Ivy

Go to download

The RabbitMQ AMQP 1.0 Java client library defines an API to access RabbitMQ with the AMQP 1.0 protocol.

There is a newer version: 0.1.0
Show newest version
// Copyright (c) 2024 Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// If you have any questions regarding licensing, please contact us at
// [email protected].
package com.rabbitmq.client.amqp.impl;

import com.rabbitmq.client.amqp.*;
import java.time.Duration;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

abstract class RpcSupport {

  private RpcSupport() {}

  static class AmqpRpcClientBuilder implements RpcClientBuilder {

    private static final Duration REQUEST_TIMEOUT_MIN = Duration.ofSeconds(1);

    private final AmqpConnection connection;

    private final DefaultRpcClientAddressBuilder requestAddressBuilder =
        new DefaultRpcClientAddressBuilder(this);
    private String replyToQueue;
    private Supplier correlationIdSupplier;
    private BiFunction requestPostProcessor;
    private Function correlationIdExtractor;
    private Duration requestTimeout = Duration.ofSeconds(30);

    AmqpRpcClientBuilder(AmqpConnection connection) {
      this.connection = connection;
    }

    @Override
    public RpcClientAddressBuilder requestAddress() {
      return this.requestAddressBuilder;
    }

    @Override
    public RpcClientBuilder replyToQueue(String replyToQueue) {
      this.replyToQueue = replyToQueue;
      return this;
    }

    @Override
    public RpcClientBuilder correlationIdSupplier(Supplier correlationIdSupplier) {
      this.correlationIdSupplier = correlationIdSupplier;
      return this;
    }

    @Override
    public RpcClientBuilder requestPostProcessor(
        BiFunction requestPostProcessor) {
      this.requestPostProcessor = requestPostProcessor;
      return this;
    }

    @Override
    public RpcClientBuilder correlationIdExtractor(
        Function correlationIdExtractor) {
      this.correlationIdExtractor = correlationIdExtractor;
      return this;
    }

    @Override
    public RpcClientBuilder requestTimeout(Duration timeout) {
      if (timeout == null) {
        throw new IllegalArgumentException("Request timeout cannot be null");
      }
      if (timeout.compareTo(REQUEST_TIMEOUT_MIN) < 0) {
        throw new IllegalArgumentException(
            "Request timeout cannot be less than " + REQUEST_TIMEOUT_MIN);
      }
      this.requestTimeout = timeout;
      return this;
    }

    Function correlationIdExtractor() {
      return correlationIdExtractor;
    }

    @Override
    public RpcClient build() {
      return this.connection.createRpcClient(this);
    }

    AmqpConnection connection() {
      return this.connection;
    }

    String replyToQueue() {
      return this.replyToQueue;
    }

    Supplier correlationIdSupplier() {
      return this.correlationIdSupplier;
    }

    BiFunction requestPostProcessor() {
      return this.requestPostProcessor;
    }

    Duration requestTimeout() {
      return this.requestTimeout;
    }
  }

  private static class DefaultRpcClientAddressBuilder
      extends DefaultAddressBuilder
      implements RpcClientBuilder.RpcClientAddressBuilder {

    private final AmqpRpcClientBuilder builder;

    private DefaultRpcClientAddressBuilder(AmqpRpcClientBuilder builder) {
      super(null);
      this.builder = builder;
    }

    @Override
    RpcClientBuilder.RpcClientAddressBuilder result() {
      return this;
    }

    @Override
    public RpcClientBuilder rpcClient() {
      return this.builder;
    }
  }

  static class AmqpRpcServerBuilder implements RpcServerBuilder {

    private final AmqpConnection connection;

    private String requestQueue;
    private RpcServer.Handler handler;
    private Function correlationIdExtractor;
    private BiFunction replyPostProcessor;

    AmqpRpcServerBuilder(AmqpConnection connection) {
      this.connection = connection;
    }

    @Override
    public RpcServerBuilder requestQueue(String requestQueue) {
      this.requestQueue = requestQueue;
      return this;
    }

    @Override
    public RpcServerBuilder handler(RpcServer.Handler handler) {
      this.handler = handler;
      return this;
    }

    @Override
    public RpcServerBuilder correlationIdExtractor(
        Function correlationIdExtractor) {
      this.correlationIdExtractor = correlationIdExtractor;
      return this;
    }

    @Override
    public RpcServerBuilder replyPostProcessor(
        BiFunction replyPostProcessor) {
      this.replyPostProcessor = replyPostProcessor;
      return this;
    }

    @Override
    public RpcServer build() {
      return this.connection.createRpcServer(this);
    }

    AmqpConnection connection() {
      return this.connection;
    }

    String requestQueue() {
      return this.requestQueue;
    }

    RpcServer.Handler handler() {
      return this.handler;
    }

    Function correlationIdExtractor() {
      return this.correlationIdExtractor;
    }

    BiFunction replyPostProcessor() {
      return this.replyPostProcessor;
    }
  }
}